import React, { useState, useRef } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // services import projectService from "services/project.service"; import issuesService from "services/issues.service"; // lib import { requiredAdmin } from "lib/auth"; // layouts import { ProjectAuthorizationWrapper } from "layouts/auth-layout"; // components import { CreateUpdateLabelInline, LabelsListModal, SingleLabel, SingleLabelGroup, } from "components/labels"; // ui import { Loader, PrimaryButton } from "components/ui"; import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs"; // icons import { PlusIcon } from "@heroicons/react/24/outline"; // types import { IIssueLabels, UserAuth } from "types"; import type { GetServerSidePropsContext, NextPage } from "next"; // fetch-keys import { PROJECT_DETAILS, PROJECT_ISSUE_LABELS } from "constants/fetch-keys"; const LabelsSettings: NextPage = () => { // create/edit label form const [labelForm, setLabelForm] = useState(false); // edit label const [isUpdating, setIsUpdating] = useState(false); const [labelToUpdate, setLabelToUpdate] = useState(null); // labels list modal const [labelsListModal, setLabelsListModal] = useState(false); const [parentLabel, setParentLabel] = useState(undefined); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const scrollToRef = useRef(null); const { data: projectDetails } = useSWR( workspaceSlug && projectId ? PROJECT_DETAILS(projectId as string) : null, workspaceSlug && projectId ? () => projectService.getProject(workspaceSlug as string, projectId as string) : null ); const { data: issueLabels, mutate } = useSWR( workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(projectId as string) : null, workspaceSlug && projectId ? () => issuesService.getIssueLabels(workspaceSlug as string, projectId as string) : null ); const newLabel = () => { setIsUpdating(false); setLabelForm(true); }; const addLabelToGroup = (parentLabel: IIssueLabels) => { setLabelsListModal(true); setParentLabel(parentLabel); }; const editLabel = (label: IIssueLabels) => { setLabelForm(true); setIsUpdating(true); setLabelToUpdate(label); }; const handleLabelDelete = (labelId: string) => { if (workspaceSlug && projectDetails) { mutate((prevData) => prevData?.filter((p) => p.id !== labelId), false); issuesService .deleteIssueLabel(workspaceSlug as string, projectDetails.id, labelId) .then((res) => { console.log(res); }) .catch((e) => { console.log(e); }); } }; return ( <> setLabelsListModal(false)} parent={parentLabel} /> } >

Labels

Manage the labels of this project.

New label
{labelForm && ( )} <> {issueLabels ? ( issueLabels.map((label) => { const children = issueLabels?.filter((l) => l.parent === label.id); if (children && children.length === 0) { if (!label.parent) return ( addLabelToGroup(label)} editLabel={(label) => { editLabel(label); scrollToRef.current?.scrollIntoView({ behavior: "smooth", }); }} handleLabelDelete={handleLabelDelete} /> ); } else return ( { editLabel(label); scrollToRef.current?.scrollIntoView({ behavior: "smooth", }); }} handleLabelDelete={handleLabelDelete} /> ); }) ) : ( )}
); }; export default LabelsSettings;