import { EntityTemplate } from "@toolctrl/db/prisma-browser"; import { useSubmit } from "react-router"; import { lazy, Suspense, useState } from "react"; import { useTranslation } from "react-i18next"; import { PropertyAttributeName } from "~/application/enums/entities/PropertyAttributeName"; import { PropertyType } from "~/application/enums/entities/PropertyType"; import FormGroup from "~/components/ui/forms/FormGroup"; import InputCheckbox from "~/components/ui/input/InputCheckbox"; import InputDate from "~/components/ui/input/InputDate"; import InputNumber from "~/components/ui/input/InputNumber"; import InputSelector from "~/components/ui/input/InputSelector"; import InputText from "~/components/ui/input/InputText"; import { EntityWithDetails } from "~/utils/db/entities/entities.db.server"; import PropertyAttributeHelper from "~/utils/helpers/PropertyAttributeHelper"; const NovelEditor = lazy(() => import("~/modules/novel/ui/editor")); interface Props { entity: EntityWithDetails; item?: EntityTemplate; } export default function EntityTemplateForm({ entity, item }: Props) { const { t } = useTranslation(); const submit = useSubmit(); const [title, setTitle] = useState(item?.title ?? ""); const [config, setConfig] = useState<{ [key: string]: any }>(item?.config ? JSON.parse(item?.config) : {}); function onSubmit(formData: FormData) { formData.set("config", JSON.stringify(config)); submit(formData, { method: "post", }); } return ( {entity.properties .filter((f) => !f.isDefault) .sort((a, b) => a.order - b.order) .map((property) => { return (
{property.type === PropertyType.TEXT ? ( <> {PropertyAttributeHelper.getPropertyAttributeValue_String(property, PropertyAttributeName.Editor) === "wysiwyg" ? (
{ setConfig({ ...config, [property.name]: e.html }); }} />
) : ( setConfig({ ...config, [property.name]: e })} /> )} ) : property.type === PropertyType.BOOLEAN ? ( setConfig({ ...config, [property.name]: e })} /> ) : property.type === PropertyType.DATE ? ( setConfig({ ...config, [property.name]: e })} /> ) : property.type === PropertyType.NUMBER ? ( setConfig({ ...config, [property.name]: e })} /> ) : property.type === PropertyType.SELECT ? ( setConfig({ ...config, [property.name]: e })} options={property.options} /> ) : (
{t("models.entityTemplate.errors.unsupportedPropertyType", { type: PropertyType[property.type] })}
)}
); })}
); }