import { useEffect, useState } from "react"; import { useRouter } from "next/router"; // mobx import { observer } from "mobx-react-lite"; import { useMobxStore } from "lib/mobx/store-provider"; // headless ui import { Disclosure } from "@headlessui/react"; // react-hook-form import { Control, Controller, UseFormWatch, useForm } from "react-hook-form"; // components import { CheckboxAttributeForm, DateTimeAttributeForm, EmailAttributeForm, FileAttributeForm, NumberAttributeForm, RelationAttributeForm, SelectAttributeForm, TextAttributeForm, UrlAttributeForm, } from "components/custom-attributes"; // ui import { PrimaryButton, SecondaryButton, ToggleSwitch } from "components/ui"; // icons import { ChevronDown } from "lucide-react"; // types import { ICustomAttribute, TCustomAttributeTypes } from "types"; // constants import { CUSTOM_ATTRIBUTES_LIST } from "constants/custom-attributes"; type Props = { attributeDetails: Partial; objectId: string; type: TCustomAttributeTypes; }; export type FormComponentProps = { control: Control, any>; objectId: string; watch: UseFormWatch>; }; const RenderForm: React.FC<{ type: TCustomAttributeTypes } & FormComponentProps> = ({ control, objectId, type, watch, }) => { let FormToRender: any = <>; if (type === "checkbox") FormToRender = ; else if (type === "datetime") FormToRender = ; else if (type === "email") FormToRender = ; else if (type === "file") FormToRender = ; else if (type === "multi_select") FormToRender = ( ); else if (type === "number") FormToRender = ; else if (type === "relation") FormToRender = ; else if (type === "select") FormToRender = ; else if (type === "text") FormToRender = ; else if (type === "url") FormToRender = ; return FormToRender; }; export const AttributeForm: React.FC = observer(({ attributeDetails, objectId, type }) => { const [isRemoving, setIsRemoving] = useState(false); const router = useRouter(); const { workspaceSlug } = router.query; const typeMetaData = CUSTOM_ATTRIBUTES_LIST[type]; const { customAttributes: customAttributesStore } = useMobxStore(); const { deleteEntityAttribute, updateEntityAttribute } = customAttributesStore; const { control, formState: { isSubmitting }, handleSubmit, reset, watch, } = useForm({ defaultValues: typeMetaData.defaultFormValues }); const handleUpdateAttribute = async (data: Partial) => { if (!workspaceSlug || !attributeDetails.id || !objectId) return; await updateEntityAttribute(workspaceSlug.toString(), objectId, attributeDetails.id, data); }; const handleDeleteAttribute = async () => { if (!workspaceSlug || !attributeDetails.id || !objectId) return; setIsRemoving(true); await deleteEntityAttribute(workspaceSlug.toString(), objectId, attributeDetails.id).finally( () => setIsRemoving(false) ); }; useEffect(() => { if (!attributeDetails) return; reset({ ...typeMetaData.defaultFormValues, ...attributeDetails, }); }, [attributeDetails, reset, typeMetaData.defaultFormValues]); return ( {({ open }) => ( <>
{attributeDetails.display_name ?? typeMetaData.label}
{attributeDetails.type && ( )}
{attributeDetails.type !== "checkbox" && ( <> ( )} /> Mandatory field )}
{isRemoving ? "Removing..." : "Remove"} {isSubmitting ? "Saving..." : "Save"}
)}
); });