import { useEffect, useState } from "react"; import { useRouter } from "next/router"; // mobx import { useMobxStore } from "lib/mobx/store-provider"; import { observer } from "mobx-react-lite"; // headless ui import { Disclosure } from "@headlessui/react"; // components import { CustomCheckboxAttribute, CustomDateTimeAttribute, CustomFileAttribute, CustomRelationAttribute, CustomSelectAttribute, } from "components/custom-attributes"; // ui import { Loader, ToggleSwitch } from "components/ui"; // icons import { ChevronDown } from "lucide-react"; // types import { TCustomAttributeTypes } from "types"; type Props = { entityId: string; issueId: string; onChange: (attributeId: string, val: string | string[] | undefined) => void; projectId: string; values: { [key: string]: string[] }; }; const DESCRIPTION_FIELDS: TCustomAttributeTypes[] = ["email", "number", "text", "url"]; export const IssueModalCustomAttributesList: React.FC = observer((props) => { const { entityId, issueId, onChange, projectId, values } = props; const [hideOptionalFields, setHideOptionalFields] = useState(false); const router = useRouter(); const { workspaceSlug } = router.query; const { customAttributes: customAttributesStore } = useMobxStore(); const { entityAttributes, fetchEntityDetails, fetchEntityDetailsLoader } = customAttributesStore; const attributes = entityAttributes[entityId] ?? {}; // fetch entity details useEffect(() => { if (!entityAttributes[entityId]) { if (!workspaceSlug) return; fetchEntityDetails(workspaceSlug.toString(), entityId); } }, [entityAttributes, entityId, fetchEntityDetails, workspaceSlug]); const descriptionFields = Object.values(attributes).filter((a) => DESCRIPTION_FIELDS.includes(a.type) ); const nonDescriptionFields = Object.values(attributes).filter( (a) => !DESCRIPTION_FIELDS.includes(a.type) ); return ( <> {fetchEntityDetailsLoader ? ( ) : ( <> {({ open }) => ( <>
Description Fields
Hide optional fields setHideOptionalFields((prev) => !prev)} />
{Object.entries(descriptionFields).map(([attributeId, attribute]) => (
onChange(attribute.id, e.target.value)} required={attribute.is_required} /> {attribute.type === "number" && attribute.extra_settings?.representation !== "numerical" && ( Maximum value: {attribute.extra_settings?.divided_by} )}
))}
)}
{Object.entries(nonDescriptionFields).map(([attributeId, attribute]) => (
{attribute.type === "checkbox" && ( onChange(attribute.id, [`${val}`])} projectId={projectId} value={values[attribute.id]?.[0] === "true" ? true : false} /> )} {attribute.type === "datetime" && ( onChange(attribute.id, val ? [val.toISOString()] : undefined) } projectId={projectId} value={ values[attribute.id]?.[0] ? new Date(values[attribute.id]?.[0]) : undefined } /> )} {attribute.type === "file" && ( onChange(attribute.id, val)} projectId={projectId} value={values[attribute.id]?.[0]} /> )} {attribute.type === "multi_select" && ( onChange(attribute.id, val)} projectId={projectId} value={values[attribute.id] ?? []} multiple /> )} {attribute.type === "relation" && ( onChange(attribute.id, val)} projectId={projectId} value={values[attribute.id]?.[0]} /> )} {attribute.type === "select" && ( onChange(attribute.id, val)} projectId={projectId} value={values[attribute.id]?.[0]} multiple={false} /> )}
))}
)} ); });