// react import React, { useState } from "react"; // swr import useSWR from "swr"; // react-hook-form import { Controller, SubmitHandler, useForm } from "react-hook-form"; // react-color import { TwitterPicker } from "react-color"; // services import issuesServices from "lib/services/issues.service"; // hooks import useUser from "lib/hooks/useUser"; // headless ui import { Popover, Transition, Menu } from "@headlessui/react"; // ui import { Button, Input, Spinner } from "ui"; // icons import { ChevronDownIcon, EllipsisHorizontalIcon, PencilIcon, PlusIcon, RectangleGroupIcon, } from "@heroicons/react/24/outline"; // types import { IIssueLabels } from "types"; // fetch-keys import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys"; const defaultValues: Partial = { name: "", colour: "#ff0000", }; const LabelsSettings: React.FC = () => { const [newLabelForm, setNewLabelForm] = useState(false); const [isUpdating, setIsUpdating] = useState(false); const [labelIdForUpdate, setLabelidForUpdate] = useState(null); const { activeWorkspace, activeProject } = useUser(); const { register, handleSubmit, reset, control, setValue, formState: { errors, isSubmitting }, watch, setError, } = useForm({ defaultValues }); const { data: issueLabels, mutate } = useSWR( activeProject && activeWorkspace ? PROJECT_ISSUE_LABELS(activeProject.id) : null, activeProject && activeWorkspace ? () => issuesServices.getIssueLabels(activeWorkspace.slug, activeProject.id) : null ); const handleNewLabel: SubmitHandler = (formData) => { if (!activeWorkspace || !activeProject || isSubmitting) return; issuesServices .createIssueLabel(activeWorkspace.slug, activeProject.id, formData) .then((res) => { console.log(res); reset(defaultValues); mutate((prevData) => [...(prevData ?? []), res], false); setNewLabelForm(false); }); }; const handleLabelUpdate: SubmitHandler = (formData) => { if (!activeWorkspace || !activeProject || isSubmitting) return; issuesServices .patchIssueLabel(activeWorkspace.slug, activeProject.id, labelIdForUpdate ?? "", formData) .then((res) => { console.log(res); reset(defaultValues); mutate( (prevData) => prevData?.map((p) => (p.id === labelIdForUpdate ? { ...p, ...formData } : p)), false ); setNewLabelForm(false); }); }; const handleLabelDelete = (labelId: string) => { if (activeWorkspace && activeProject) { mutate((prevData) => prevData?.filter((p) => p.id !== labelId), false); issuesServices .deleteIssueLabel(activeWorkspace.slug, activeProject.id, labelId) .then((res) => { console.log(res); }) .catch((e) => { console.log(e); }); } }; const getLabelChildren = (labelId: string) => { return issueLabels?.filter((l) => l.parent === labelId); }; return ( <>

Labels

Manage the labels of this project.

{({ open }) => ( <> {watch("colour") && watch("colour") !== "" && ( )} ( onChange(value.hex)} /> )} /> )}
{isUpdating ? ( ) : ( )}
{issueLabels ? ( issueLabels.map((label) => { const children = getLabelChildren(label.id); return ( {children && children.length === 0 ? (

{label.name}

) : (

This is the label group title

This is the label title
)}
); }) ) : (
)}
); }; export default LabelsSettings;