import React, { useEffect, useState } from "react"; import { observer } from "mobx-react-lite"; import { Controller, useForm } from "react-hook-form"; import { Disclosure } from "@headlessui/react"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; // components import { Input, OptionForm, SelectOption } from "components/custom-attributes"; // ui import { PrimaryButton, SecondaryButton, ToggleSwitch } from "components/ui"; // icons import { ChevronDown } from "lucide-react"; // types import { ICustomAttribute } from "types"; // constants import { CUSTOM_ATTRIBUTES_LIST } from "constants/custom-attributes"; type Props = { attributeDetails: ICustomAttribute; handleDeleteAttribute: () => Promise; handleUpdateAttribute: (data: Partial) => Promise; }; const typeMetaData = CUSTOM_ATTRIBUTES_LIST.select; export const SelectAttributeForm: React.FC = observer((props) => { const { attributeDetails, handleDeleteAttribute, handleUpdateAttribute, multiple = false, } = props; const [optionToEdit, setOptionToEdit] = useState(null); const [isRemoving, setIsRemoving] = useState(false); const { customAttributes } = useMobxStore(); const { control, formState: { isSubmitting }, handleSubmit, reset, watch, } = useForm({ defaultValues: typeMetaData.defaultFormValues }); const options = customAttributes.objectAttributes?.[attributeDetails.parent ?? ""]?.[watch("id") ?? ""] ?.children; const handleDelete = async () => { setIsRemoving(true); await handleDeleteAttribute().finally(() => setIsRemoving(false)); }; useEffect(() => { if (!attributeDetails) return; reset({ ...typeMetaData.defaultFormValues, ...attributeDetails, }); }, [attributeDetails, reset]); return ( {({ open }) => ( <>
{attributeDetails.display_name ?? typeMetaData.label}
( )} />

Options

{options?.map((option) => ( setOptionToEdit(option)} objectId={attributeDetails.parent ?? ""} option={option} /> ))}
setOptionToEdit(null)} parentId={watch("id") ?? ""} />
( )} /> Mandatory field
{isRemoving ? "Removing..." : "Remove"} {isSubmitting ? "Saving..." : "Save"}
)}
); });