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"; import { renderEmoji } from "helpers/emoji.helper"; import EmojiIconPicker from "components/emoji-icon-picker"; 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 [isUpdatingObject, setIsUpdatingObject] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { customAttributes } = useMobxStore(); const handleClose = () => { onClose(); setTimeout(() => { setObject({ display_name: "", description: "" }); }, 300); }; const handleCreateObject = async () => { if (!workspaceSlug || !projectId) return; setIsCreatingObject(true); const payload: Partial = { description: object.description ?? "", display_name: object.display_name ?? "", icon: object.icon ?? "", project: projectId.toString(), type: "entity", }; await customAttributes .createEntity(workspaceSlug.toString(), payload) .then((res) => { setObject((prevData) => ({ ...prevData, ...res })); if (onSubmit) onSubmit(); }) .finally(() => setIsCreatingObject(false)); }; const handleUpdateObject = async () => { if (!workspaceSlug || !object || !object.id) return; setIsUpdatingObject(true); const payload: Partial = { description: object.description ?? "", display_name: object.display_name ?? "", icon: object.icon ?? "", }; await customAttributes .updateEntity(workspaceSlug.toString(), object.id, payload) .finally(() => setIsUpdatingObject(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 customAttributes.createEntityAttribute(workspaceSlug.toString(), { ...payload, parent: object.id, }); }; // fetch the object details if object state has id useEffect(() => { if (!object.id || object.id === "") return; if (!customAttributes.entityAttributes[object.id]) { if (!workspaceSlug) return; customAttributes.fetchEntityDetails(workspaceSlug.toString(), object.id).then((res) => { setObject((prev) => ({ ...prev, ...res })); }); } else { setObject((prev) => ({ ...prev, ...customAttributes.entities?.find((e) => e.id === object.id), })); } }, [customAttributes, object.id, workspaceSlug]); // update the object state if objectIdToEdit is present useEffect(() => { if (!objectIdToEdit) return; setObject((prev) => ({ ...prev, id: objectIdToEdit, })); }, [objectIdToEdit]); return (

New Object

{ if (typeof icon === "string") setObject((prevData) => ({ ...prevData, icon })); }} value={object.icon} showIconPicker={false} />
setObject((prevData) => ({ ...prevData, display_name: e.target.value })) } />