// react import { useCallback, useEffect } from "react"; // swr import useSWR, { mutate } from "swr"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // layouts import SettingsLayout from "layouts/settings-layout"; // services import projectService from "lib/services/project.service"; // hooks import useUser from "lib/hooks/useUser"; import useToast from "lib/hooks/useToast"; // ui import { BreadcrumbItem, Breadcrumbs, Button, EmojiIconPicker, Input, Select, TextArea } from "ui"; // types import { IProject, IWorkspace } from "types"; // fetch-keys import { PROJECTS_LIST, PROJECT_DETAILS } from "constants/fetch-keys"; // common import { debounce } from "constants/common"; const NETWORK_CHOICES = { "0": "Secret", "2": "Public" }; const defaultValues: Partial = { name: "", description: "", identifier: "", network: 0, }; const GeneralSettings = () => { const { activeWorkspace, activeProject } = useUser(); const { setToastAlert } = useToast(); const { data: projectDetails } = useSWR( activeWorkspace && activeProject ? PROJECT_DETAILS(activeProject.id) : null, activeWorkspace && activeProject ? () => projectService.getProject(activeWorkspace.slug, activeProject.id) : null ); const { register, handleSubmit, reset, control, setError, formState: { errors, isSubmitting }, } = useForm({ defaultValues, }); const onSubmit = async (formData: IProject) => { if (!activeWorkspace || !activeProject) 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, icon: formData.icon, }; await projectService .updateProject(activeWorkspace.slug, activeProject.id, payload) .then((res) => { mutate( PROJECT_DETAILS(activeProject.id), (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) => { projectService.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), []); useEffect(() => { projectDetails && reset({ ...projectDetails, default_assignee: projectDetails.default_assignee?.id, project_lead: projectDetails.project_lead?.id, workspace: (projectDetails.workspace as IWorkspace).id, }); }, [projectDetails, reset]); return ( } >

General

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

Icon & Name

Select an icon and a name for the project.

( )} />

Identifier

Create a 1-6 characters{"'"} identifier for 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", }, }} />

Description

Give a description to the project.