import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { Controller, useForm } from "react-hook-form"; import { Dialog, Transition } from "@headlessui/react"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; // components import { Input, TypesDropdown, AttributeForm } from "components/custom-attributes"; import EmojiIconPicker from "components/emoji-icon-picker"; // ui import { Loader, PrimaryButton, SecondaryButton } from "components/ui"; // helpers import { renderEmoji } from "helpers/emoji.helper"; // types import { ICustomAttribute, TCustomAttributeTypes } from "types"; // constants import { CUSTOM_ATTRIBUTES_LIST } from "constants/custom-attributes"; type Props = { data?: ICustomAttribute; isOpen: boolean; onClose: () => void; onSubmit?: () => Promise; }; const defaultValues: Partial = { display_name: "", description: "", icon: "", }; export const ObjectModal: React.FC = observer((props) => { const { data, isOpen, onClose, onSubmit } = props; const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { control, formState: { isSubmitting }, handleSubmit, reset, setValue, watch, } = useForm({ defaultValues }); const objectId = watch("id") && watch("id") !== "" ? watch("id") : null; const { customAttributes } = useMobxStore(); const handleClose = () => { onClose(); setTimeout(() => { reset({ ...defaultValues }); }, 300); }; const handleCreateObject = async (formData: Partial) => { if (!workspaceSlug || !projectId) return; const payload: Partial = { description: formData.description ?? "", display_name: formData.display_name ?? "", icon: formData.icon ?? "", project: projectId.toString(), type: "entity", }; await customAttributes .createObject(workspaceSlug.toString(), payload) .then((res) => setValue("id", res?.id ?? "")); }; const handleUpdateObject = async (formData: Partial) => { if (!workspaceSlug || !data || !data.id) return; const payload: Partial = { description: formData.description ?? "", display_name: formData.display_name ?? "", icon: formData.icon ?? "", }; await customAttributes.updateObject(workspaceSlug.toString(), data.id, payload); }; const handleObjectFormSubmit = async (formData: Partial) => { if (data) await handleUpdateObject(formData); else await handleCreateObject(formData); if (onSubmit) onSubmit(); }; const handleCreateObjectAttribute = async (type: TCustomAttributeTypes) => { if (!workspaceSlug || !objectId) return; const typeMetaData = CUSTOM_ATTRIBUTES_LIST[type]; const payload: Partial = { display_name: typeMetaData.label, type, ...typeMetaData.initialPayload, }; await customAttributes.createObjectAttribute(workspaceSlug.toString(), { ...payload, parent: objectId, }); }; // fetch the object details if object state has id useEffect(() => { if (!objectId) return; if (!customAttributes.objectAttributes[objectId]) { if (!workspaceSlug) return; customAttributes.fetchObjectDetails(workspaceSlug.toString(), objectId).then((res) => { reset({ ...res }); }); } else { reset({ ...customAttributes.objects?.find((e) => e.id === objectId), }); } }, [customAttributes, objectId, reset, workspaceSlug]); // update the form if data is present useEffect(() => { if (!data) return; reset({ ...defaultValues, ...data, }); }, [data, reset]); return (

New Object

( { if (typeof icon === "string") onChange(icon); }} value={value} showIconPicker={false} /> )} />
( onChange(e.target.value)} /> )} />
(