import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { Controller, useForm } from "react-hook-form"; import { TwitterPicker } from "react-color"; import { Dialog, Popover, Transition } from "@headlessui/react"; // store import { observer } from "mobx-react-lite"; import { useMobxStore } from "lib/mobx/store-provider"; // ui import { Button, Input } from "@plane/ui"; // icons import { ChevronDown } from "lucide-react"; // types import type { IIssueLabels, IState } from "types"; // constants import { LABEL_COLOR_OPTIONS, getRandomLabelColor } from "constants/label"; import useToast from "hooks/use-toast"; // types type Props = { isOpen: boolean; projectId: string; handleClose: () => void; onSuccess?: (response: IIssueLabels) => void; }; const defaultValues: Partial = { name: "", color: "rgb(var(--color-text-200))", }; export const CreateLabelModal: React.FC = observer((props) => { const { isOpen, projectId, handleClose, onSuccess } = props; const router = useRouter(); const { workspaceSlug } = router.query; // store const { projectLabel: projectLabelStore } = useMobxStore(); 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 { setToastAlert } = useToast(); const onSubmit = async (formData: IIssueLabels) => { if (!workspaceSlug) return; await projectLabelStore .createLabel(workspaceSlug.toString(), projectId.toString(), formData) .then((res) => { onClose(); if (onSuccess) onSuccess(res); }) .catch((error) => { setToastAlert({ title: "Oops!", type: "error", message: error?.error ?? "Error while adding the label", }); reset(formData); }); }; return (
Create Label
{({ open, close }) => ( <> {watch("color") && watch("color") !== "" && ( )} ( { onChange(value.hex); close(); }} /> )} /> )}
( )} />
); });