import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; import { Controller, useForm } from "react-hook-form"; import { TwitterPicker } from "react-color"; import { Dialog, Popover, Transition } from "@headlessui/react"; // services import { IssueLabelService } from "services/issue"; // ui import { Button, Input } from "@plane/ui"; // icons import { ChevronDownIcon } from "@heroicons/react/24/outline"; // types import type { IUser, IIssueLabels, IState } from "types"; // constants import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys"; import { LABEL_COLOR_OPTIONS, getRandomLabelColor } from "constants/label"; // types type Props = { isOpen: boolean; projectId: string; handleClose: () => void; onSuccess?: (response: IIssueLabels) => void; user: IUser | undefined; }; const defaultValues: Partial = { name: "", color: "rgb(var(--color-text-200))", }; const issueLabelService = new IssueLabelService(); export const CreateLabelModal: React.FC = ({ isOpen, projectId, handleClose, user, onSuccess }) => { const router = useRouter(); const { workspaceSlug } = router.query; const { formState: { errors, isSubmitting }, handleSubmit, watch, control, reset, setValue, } = useForm({ defaultValues, }); useEffect(() => { if (isOpen) setValue("color", getRandomLabelColor()); }, [setValue, isOpen]); const onClose = () => { handleClose(); reset(defaultValues); }; const onSubmit = async (formData: IIssueLabels) => { if (!workspaceSlug) return; await issueLabelService .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(); }} /> )} /> )}
( )} />
); };