import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // react-color import { TwitterPicker } from "react-color"; // headless ui import { Dialog, Popover, Transition } from "@headlessui/react"; // services import issuesService from "services/issues.service"; // ui import { Input, PrimaryButton, SecondaryButton } from "components/ui"; // icons import { ChevronDownIcon } from "@heroicons/react/24/outline"; // types import type { ICurrentUserResponse, IIssueLabels, IState } from "types"; // constants import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys"; // types type Props = { isOpen: boolean; projectId: string; handleClose: () => void; onSuccess?: (response: IIssueLabels) => void; user: ICurrentUserResponse | undefined; }; const defaultValues: Partial = { name: "", color: "rgb(var(--color-text-200))", }; export const CreateLabelModal: React.FC = ({ isOpen, projectId, handleClose, user, onSuccess, }) => { const router = useRouter(); const { workspaceSlug } = router.query; const { register, formState: { errors, isSubmitting }, handleSubmit, watch, control, reset, } = useForm({ defaultValues, }); const onClose = () => { handleClose(); reset(defaultValues); }; const onSubmit = async (formData: IIssueLabels) => { if (!workspaceSlug) return; await issuesService .createIssueLabel(workspaceSlug as string, projectId as string, formData, user) .then((res) => { mutate( PROJECT_ISSUE_LABELS(projectId), (prevData) => [res, ...(prevData ?? [])], false ); onClose(); if (onSuccess) onSuccess(res); }) .catch((error) => { console.log(error); }); }; return (
Create Label
{({ open, close }) => ( <> {watch("color") && watch("color") !== "" && ( )} ( { onChange(value.hex); close(); }} /> )} /> )}
Cancel {isSubmitting ? "Creating Label..." : "Create Label"}
); };