import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; import { useForm, Controller } from "react-hook-form"; import type { Control } from "react-hook-form"; // services import issuesServices from "lib/services/issues.service"; // fetching keys import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys"; // icons import { PlusIcon, XMarkIcon } from "@heroicons/react/20/solid"; // ui import { Input, CustomListbox } from "ui"; // icons import { TagIcon } from "@heroicons/react/24/outline"; // types import type { IIssue, IIssueLabels } from "types"; type Props = { control: Control; }; const defaultValues: Partial = { name: "", }; const SelectLabels: React.FC = ({ control }) => { const router = useRouter(); const { workspaceSlug, projectId } = router.query; const [isOpen, setIsOpen] = useState(false); const { data: issueLabels, mutate: issueLabelsMutate } = useSWR( workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(workspaceSlug as string) : null, workspaceSlug && projectId ? () => issuesServices.getIssueLabels(workspaceSlug as string, projectId as string) : null ); const onSubmit = async (data: IIssueLabels) => { if (!projectId || !workspaceSlug || isSubmitting) return; await issuesServices .createIssueLabel(workspaceSlug as string, projectId as string, data) .then((response) => { issueLabelsMutate((prevData) => [...(prevData ?? []), response], false); setIsOpen(false); reset(defaultValues); }) .catch((error) => { console.log(error); }); }; const { register, handleSubmit, formState: { isSubmitting }, setFocus, reset, } = useForm({ defaultValues }); useEffect(() => { isOpen && setFocus("name"); }, [isOpen, setFocus]); return ( ( { return { value: label.id, display: label.name, color: label.colour }; })} value={value} optionsFontsize="sm" onChange={onChange} icon={} footerOption={
{isOpen ? (
) : ( )}
} /> )} /> ); }; export default SelectLabels;