import { useNavigation } from "react-router"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import FormGroup from "~/components/ui/forms/FormGroup"; import InputCheckboxWithDescription from "~/components/ui/input/InputCheckboxWithDescription"; import InputRadioGroup from "~/components/ui/input/InputRadioGroup"; import InputSelector from "~/components/ui/input/InputSelector"; import InputText from "~/components/ui/input/InputText"; import { EntityWithDetails } from "~/utils/db/entities/entities.db.server"; import { EntityRelationshipWithDetails } from "~/utils/db/entities/entityRelationships.db.server"; interface Props { entity: EntityWithDetails; entities: EntityWithDetails[]; item?: EntityRelationshipWithDetails; } export default function EntityRelationshipForm({ entity, entities, item }: Props) { const { t } = useTranslation(); const navigation = useNavigation(); const [type, setType] = useState(!item ? "child" : item.parentId === entity?.id ? "parent" : "child"); const [parentId, setParentId] = useState(item?.parentId); const [childId, setChildId] = useState(item?.childId); const [child, setChild] = useState(); const [parent, setParent] = useState(); const [title, setTitle] = useState(item?.title ?? ""); const [relationshipType, setRelationshipType] = useState(item?.type ?? "one-to-many"); const [required, setRequired] = useState(item ? item.required : false); const [cascade, setCascade] = useState(item ? item.cascade : false); const [readOnly, setReadOnly] = useState(item ? item.readOnly : false); const [hiddenIfEmpty, setHiddenIfEmpty] = useState(item ? item.hiddenIfEmpty : false); const [childEntityViewId, setChildEntityViewId] = useState(item?.childEntityViewId ?? undefined); const [parentEntityViewId, setParentEntityViewId] = useState(item?.parentEntityViewId ?? undefined); useEffect(() => { if (type === "parent") { setParentId(entity?.id); setChildId(undefined); } else { setParentId(undefined); setChildId(entity?.id); } }, [entity, type, entities]); useEffect(() => { setChild(entities.find((i) => i.id === childId)); }, [childId, entities]); useEffect(() => { setParent(entities.find((i) => i.id === parentId)); }, [parentId, entities]); return (
{/*

{t(entity.title)} relationship

*/}
setType(e?.toString() ?? "")} options={[ { name: t("models.relationship.form.entityIsParent", { entity: t(entity.title) }), value: "parent", }, { name: t("models.relationship.form.entityIsChild", { entity: t(entity.title) }), value: "child", }, ]} /> {type === "child" && ( setParentId(e?.toString() ?? "")} options={entities.map((i) => { return { value: i.id, name: t(i.title), }; })} required /> )} {type === "parent" && ( setChildId(e?.toString() ?? "")} options={entities.map((i) => { return { value: i.id, name: t(i.title), }; })} required /> )} setRelationshipType(e?.toString() ?? "one-to-many")} options={[ { name: t("models.relationship.types.oneToMany"), value: "one-to-many" }, { name: t("models.relationship.types.oneToOne"), value: "one-to-one" }, { name: t("models.relationship.types.manyToOne"), value: "many-to-one" }, { name: t("models.relationship.types.manyToMany"), value: "many-to-many" }, ]} required /> f.isSystem).length === 0} withSearch={false} className="col-span-2" name="childEntityViewId" title={t("models.relationship.form.childrenView")} value={childEntityViewId} setValue={(e) => setChildEntityViewId(e?.toString() ?? undefined)} options={ child?.views .filter((f) => f.isSystem) .map((f) => { return { value: f.id, name: `${f.title} (${f.layout})`, }; }) ?? [] } hint={ <> {childEntityViewId !== undefined && ( )} } /> f.isSystem).length === 0} withSearch={false} className="col-span-2" name="parentEntityViewId" title={t("models.relationship.form.parentView")} value={parentEntityViewId} setValue={(e) => setParentEntityViewId(e?.toString() ?? undefined)} options={ parent?.views .filter((f) => f.isSystem) .map((f) => { return { value: f.id, name: `${f.title} (${f.layout})`, }; }) ?? [] } hint={ <> {childEntityViewId !== undefined && ( )} } /> setRequired(Boolean(e))} /> setCascade(Boolean(e))} /> setReadOnly(Boolean(e))} /> setHiddenIfEmpty(Boolean(e))} /> setTitle(e)} />
); }