import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // react-hook-form import { Controller, SubmitHandler, useForm } from "react-hook-form"; // react-color import { TwitterPicker } from "react-color"; // headless ui import { Popover, Transition } from "@headlessui/react"; // services import projectService from "services/project.service"; import workspaceService from "services/workspace.service"; import issuesService from "services/issues.service"; // lib import { requiredAdmin } from "lib/auth"; // layouts import AppLayout from "layouts/app-layout"; // components import SingleLabel from "components/project/settings/single-label"; // ui import { Button, Input, Loader } from "components/ui"; import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs"; // icons import { PlusIcon } from "@heroicons/react/24/outline"; // types import { IIssueLabels } from "types"; import type { NextPageContext, NextPage } from "next"; // fetch-keys import { PROJECT_DETAILS, PROJECT_ISSUE_LABELS, WORKSPACE_DETAILS } from "constants/fetch-keys"; const defaultValues: Partial = { name: "", color: "#ff0000", }; type TLabelSettingsProps = { isMember: boolean; isOwner: boolean; isViewer: boolean; isGuest: boolean; }; const LabelsSettings: NextPage = (props) => { const { isMember, isOwner, isViewer, isGuest } = props; const [newLabelForm, setNewLabelForm] = useState(false); const [isUpdating, setIsUpdating] = useState(false); const [labelIdForUpdate, setLabelIdForUpdate] = useState(null); const { query: { workspaceSlug, projectId }, } = useRouter(); const { data: activeWorkspace } = useSWR( workspaceSlug ? WORKSPACE_DETAILS(workspaceSlug as string) : null, () => (workspaceSlug ? workspaceService.getWorkspace(workspaceSlug as string) : 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 { register, handleSubmit, reset, control, setValue, formState: { errors, isSubmitting }, watch, } = useForm({ defaultValues }); 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 handleNewLabel: SubmitHandler = async (formData) => { if (!activeWorkspace || !projectDetails || isSubmitting) return; await issuesService .createIssueLabel(activeWorkspace.slug, projectDetails.id, formData) .then((res) => { reset(defaultValues); mutate((prevData) => [...(prevData ?? []), res], false); setNewLabelForm(false); }); }; const editLabel = (label: IIssueLabels) => { setNewLabelForm(true); setValue("color", label.color); setValue("name", label.name); setIsUpdating(true); setLabelIdForUpdate(label.id); }; const handleLabelUpdate: SubmitHandler = async (formData) => { if (!activeWorkspace || !projectDetails || isSubmitting) return; await issuesService .patchIssueLabel(activeWorkspace.slug, projectDetails.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 && projectDetails) { mutate((prevData) => prevData?.filter((p) => p.id !== labelId), false); issuesService .deleteIssueLabel(activeWorkspace.slug, projectDetails.id, labelId) .then((res) => { console.log(res); }) .catch((e) => { console.log(e); }); } }; return ( } >

Labels

Manage the labels of this project.

Manage labels

{({ open }) => ( <> {watch("color") && watch("color") !== "" && ( )} ( onChange(value.hex)} /> )} /> )}
{isUpdating ? ( ) : ( )}
<> {issueLabels ? ( issueLabels.map((label) => ( )) ) : ( )}
); }; export const getServerSideProps = async (ctx: NextPageContext) => { const projectId = ctx.query.projectId as string; const workspaceSlug = ctx.query.workspaceSlug as string; const memberDetail = await requiredAdmin(workspaceSlug, projectId, ctx.req?.headers.cookie); return { props: { isOwner: memberDetail?.role === 20, isMember: memberDetail?.role === 15, isViewer: memberDetail?.role === 10, isGuest: memberDetail?.role === 5, }, }; }; export default LabelsSettings;