import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // headless ui import { Combobox, Transition } from "@headlessui/react"; // services import cyclesService from "services/cycles.service"; import modulesService from "services/modules.service"; // icons import { Search } from "lucide-react"; // types import { Props } from "./types"; // fetch-keys import { CYCLES_LIST, MODULE_LIST } from "constants/fetch-keys"; export const CustomRelationAttribute: React.FC = ({ attributeDetails, onChange, projectId, value, }) => { const router = useRouter(); const { workspaceSlug } = router.query; const [query, setQuery] = useState(""); const { data: cycles } = useSWR( workspaceSlug && projectId && attributeDetails.unit === "cycle" ? CYCLES_LIST(projectId.toString()) : null, workspaceSlug && projectId && attributeDetails.unit === "cycle" ? () => cyclesService.getCyclesWithParams(workspaceSlug.toString(), projectId.toString(), "all") : null ); const { data: modules } = useSWR( workspaceSlug && projectId && attributeDetails.unit === "module" ? MODULE_LIST(projectId as string) : null, workspaceSlug && projectId && attributeDetails.unit === "module" ? () => modulesService.getModules(workspaceSlug as string, projectId as string) : null ); const optionsList = attributeDetails.unit === "cycle" ? cycles?.map((c) => ({ id: c.id, name: c.name })) : attributeDetails.unit === "module" ? modules?.map((m) => ({ id: m.id, name: m.name })) : []; const selectedOption = (optionsList ?? []).find((option) => option.id === value); const options = (optionsList ?? []).filter((option) => option.name.toLowerCase().includes(query.toLowerCase()) ); return ( onChange(val)} className="relative flex-shrink-0 text-left" > {({ open }: { open: boolean }) => ( <> {selectedOption?.name ?? `Select ${attributeDetails.unit}`}
setQuery(e.target.value)} placeholder="Type to search..." displayValue={(assigned: any) => assigned?.name} />
{options ? ( options.length > 0 ? ( options.map((option) => ( {option.name} )) ) : (

No {attributeDetails.unit}s found

) ) : (

Loading...

)}
)}
); };