import React, { 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 { Dialog, Transition } from "@headlessui/react"; // components import { Input, TypesDropdown, AttributeForm } from "components/custom-attributes"; // ui import { Loader, PrimaryButton, SecondaryButton } from "components/ui"; // types import { ICustomAttribute, TCustomAttributeTypes } from "types"; // constants import { CUSTOM_ATTRIBUTES_LIST } from "constants/custom-attributes"; type Props = { objectIdToEdit?: string | null; isOpen: boolean; onClose: () => void; onSubmit?: () => Promise; }; export const ObjectModal: React.FC = observer( ({ objectIdToEdit, isOpen, onClose, onSubmit }) => { const [object, setObject] = useState>({ display_name: "", description: "", }); const [isCreatingObject, setIsCreatingObject] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { customAttributes: customAttributesStore } = useMobxStore(); const { createEntity, createEntityAttribute, createEntityAttributeLoader, deleteEntityAttribute, entityAttributes, fetchEntityDetails, fetchEntityDetailsLoader, updateEntityAttribute, } = customAttributesStore; const handleClose = () => { onClose(); setTimeout(() => { setObject({ display_name: "", description: "" }); }, 300); }; const handleCreateEntity = async () => { if (!workspaceSlug || !projectId) return; setIsCreatingObject(true); const payload: Partial = { description: object.description ?? "", display_name: object.display_name ?? "", project: projectId.toString(), type: "entity", }; await createEntity(workspaceSlug.toString(), payload) .then((res) => { setObject((prevData) => ({ ...prevData, ...res })); if (onSubmit) onSubmit(); }) .finally(() => setIsCreatingObject(false)); }; const handleCreateEntityAttribute = async (type: TCustomAttributeTypes) => { if (!workspaceSlug || !object || !object.id) return; const typeMetaData = CUSTOM_ATTRIBUTES_LIST[type]; const payload: Partial = { display_name: typeMetaData.label, type, ...typeMetaData.initialPayload, }; await createEntityAttribute(workspaceSlug.toString(), { ...payload, parent: object.id }); }; const handleUpdateAttribute = async (attributeId: string, data: Partial) => { if (!workspaceSlug || !object || !object.id) return; await updateEntityAttribute(workspaceSlug.toString(), object.id, attributeId, data); }; const handleDeleteAttribute = async (attributeId: string) => { if (!workspaceSlug || !object || !object.id) return; await deleteEntityAttribute(workspaceSlug.toString(), object.id, attributeId); }; // fetch the object details if object state has id useEffect(() => { if (!object.id || object.id === "") return; if (!entityAttributes[object.id]) { if (!workspaceSlug) return; fetchEntityDetails(workspaceSlug.toString(), object.id).then((res) => { setObject({ ...res }); }); } }, [object.id, workspaceSlug, fetchEntityDetails, entityAttributes]); // update the object state if objectIdToEdit is present useEffect(() => { if (!objectIdToEdit) return; setObject((prevData) => ({ ...prevData, id: objectIdToEdit, })); }, [objectIdToEdit]); return (

New Object

🚀
setObject((prevData) => ({ ...prevData, display_name: e.target.value })) } />