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/labels"; // 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, UserAuth } 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", }; const LabelsSettings: NextPage = (props) => { const { isMember, isOwner, isViewer, isGuest } = props; const [labelForm, setLabelForm] = useState(false); const [isUpdating, setIsUpdating] = useState(false); const [labelIdForUpdate, setLabelIdForUpdate] = useState(null); const router = useRouter(); const { workspaceSlug, projectId } = router.query; 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 { register, handleSubmit, reset, control, setValue, formState: { errors, isSubmitting }, watch, } = useForm({ defaultValues }); const newLabel = () => { reset(); setIsUpdating(false); setLabelForm(true); }; const editLabel = (label: IIssueLabels) => { setLabelForm(true); setValue("color", label.color); setValue("name", label.name); setIsUpdating(true); setLabelIdForUpdate(label.id); }; const handleLabelCreate: SubmitHandler = async (formData) => { if (!workspaceSlug || !projectDetails || isSubmitting) return; await issuesService .createIssueLabel(workspaceSlug as string, projectDetails.id, formData) .then((res) => { mutate((prevData) => [res, ...(prevData ?? [])], false); reset(defaultValues); setLabelForm(false); }); }; const handleLabelUpdate: SubmitHandler = async (formData) => { if (!workspaceSlug || !projectDetails || isSubmitting) return; await issuesService .patchIssueLabel(workspaceSlug as string, projectDetails.id, labelIdForUpdate ?? "", formData) .then((res) => { console.log(res); reset(defaultValues); mutate( (prevData) => prevData?.map((p) => (p.id === labelIdForUpdate ? { ...p, ...formData } : p)), false ); setLabelForm(false); }); }; 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 ( } >

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;