import React, { useEffect, useCallback, useState } from "react"; // swr import { mutate } from "swr"; // next import type { NextPage } from "next"; import { useRouter } from "next/router"; // swr import useSWR from "swr"; // react hook form import { useForm, Controller } from "react-hook-form"; // headless ui import { Listbox, Transition } from "@headlessui/react"; // layouts import AdminLayout from "layouts/AdminLayout"; // service import projectServices from "lib/services/project.service"; import workspaceService from "lib/services/workspace.service"; // hooks import useUser from "lib/hooks/useUser"; import useToast from "lib/hooks/useToast"; // fetch keys import { PROJECT_DETAILS, PROJECTS_LIST, WORKSPACE_MEMBERS } from "constants/fetch-keys"; // commons import { addSpaceIfCamelCase, debounce } from "constants/common"; // components import CreateUpdateStateModal from "components/project/issues/BoardView/state/CreateUpdateStateModal"; // ui import { Spinner, Button, Input, TextArea, Select } from "ui"; import { Breadcrumbs, BreadcrumbItem } from "ui/Breadcrumbs"; // icons import { ChevronDownIcon, CheckIcon, PlusIcon } from "@heroicons/react/24/outline"; // types import type { IProject, IWorkspace, WorkspaceMember } from "types"; const defaultValues: Partial = { name: "", description: "", }; const NETWORK_CHOICES = { "0": "Secret", "2": "Public" }; const ProjectSettings: NextPage = () => { const { register, handleSubmit, reset, control, setError, formState: { errors, isSubmitting }, } = useForm({ defaultValues, }); const [isCreateStateModalOpen, setIsCreateStateModalOpen] = useState(false); const router = useRouter(); const { projectId } = router.query; const { activeWorkspace, activeProject, states } = useUser(); const { setToastAlert } = useToast(); const { data: projectDetails } = useSWR( activeWorkspace && projectId ? PROJECT_DETAILS : null, activeWorkspace ? () => projectServices.getProject(activeWorkspace.slug, projectId as string) : null ); const { data: people } = useSWR( activeWorkspace ? WORKSPACE_MEMBERS : null, activeWorkspace ? () => workspaceService.workspaceMembers(activeWorkspace.slug) : null ); useEffect(() => { projectDetails && reset({ ...projectDetails, default_assignee: projectDetails.default_assignee?.id, project_lead: projectDetails.project_lead?.id, workspace: (projectDetails.workspace as IWorkspace).id, }); }, [projectDetails, reset]); const onSubmit = async (formData: IProject) => { if (!activeWorkspace) return; const payload: Partial = { name: formData.name, network: formData.network, identifier: formData.identifier, description: formData.description, default_assignee: formData.default_assignee, project_lead: formData.project_lead, }; await projectServices .updateProject(activeWorkspace.slug, projectId as string, payload) .then((res) => { mutate(PROJECT_DETAILS, (prevData) => ({ ...prevData, ...res }), false); mutate( PROJECTS_LIST(activeWorkspace.slug), (prevData) => { const newData = prevData?.map((item) => { if (item.id === res.id) { return res; } return item; }); return newData; }, false ); setToastAlert({ title: "Success", type: "success", message: "Project updated successfully", }); }) .catch((err) => { console.log(err); }); }; const checkIdentifier = (slug: string, value: string) => { projectServices.checkProjectIdentifierAvailability(slug, value).then((response) => { console.log(response); if (response.exists) setError("identifier", { message: "Identifier already exists" }); }); }; // eslint-disable-next-line react-hooks/exhaustive-deps const checkIdentifierAvailability = useCallback(debounce(checkIdentifier, 1500), []); return (
{projectDetails ? (

General

This information will be displayed to every member of the project.

{ if (!activeWorkspace || !e.target.value) return; checkIdentifierAvailability(activeWorkspace.slug, e.target.value); }} validations={{ required: "Identifier is required", minLength: { value: 1, message: "Identifier must at least be of 1 character", }, maxLength: { value: 9, message: "Identifier must at most be of 9 characters", }, }} />