import { useEffect } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; import { observer } from "mobx-react-lite"; // hooks import { useProject, useUser } from "hooks/store"; import useToast from "hooks/use-toast"; import { Controller, useForm } from "react-hook-form"; import { MemberSelect } from "components/project"; // ui import { Loader } from "@plane/ui"; // types import { IProject, IUserLite, IWorkspace } from "@plane/types"; // fetch-keys import { PROJECT_MEMBERS } from "constants/fetch-keys"; // constants import { EUserProjectRoles } from "constants/project"; const defaultValues: Partial = { project_lead: null, default_assignee: null, }; export const ProjectSettingsMemberDefaults: React.FC = observer(() => { // router const router = useRouter(); const { workspaceSlug, projectId } = router.query; // store hooks const { membership: { currentProjectRole }, } = useUser(); const { currentProjectDetails, fetchProjectDetails, updateProject } = useProject(); // derived values const isAdmin = currentProjectRole === EUserProjectRoles.ADMIN; // hooks const { setToastAlert } = useToast(); // form info const { reset, control } = useForm({ defaultValues }); // fetching user members useSWR( workspaceSlug && projectId ? PROJECT_MEMBERS(projectId.toString()) : null, workspaceSlug && projectId ? () => fetchProjectDetails(workspaceSlug.toString(), projectId.toString()) : null ); useEffect(() => { if (!currentProjectDetails) return; reset({ ...currentProjectDetails, default_assignee: (currentProjectDetails.default_assignee as IUserLite)?.id ?? currentProjectDetails.default_assignee, project_lead: (currentProjectDetails.project_lead as IUserLite)?.id ?? currentProjectDetails.project_lead, workspace: (currentProjectDetails.workspace as IWorkspace).id, }); }, [currentProjectDetails, reset]); const submitChanges = async (formData: Partial) => { if (!workspaceSlug || !projectId) return; reset({ ...currentProjectDetails, default_assignee: (currentProjectDetails?.default_assignee as IUserLite)?.id ?? currentProjectDetails?.default_assignee, project_lead: (currentProjectDetails?.project_lead as IUserLite)?.id ?? currentProjectDetails?.project_lead, ...formData, }); await updateProject(workspaceSlug.toString(), projectId.toString(), { default_assignee: formData.default_assignee === "none" ? null : formData.default_assignee, project_lead: formData.project_lead === "none" ? null : formData.project_lead, }) .then(() => { fetchProjectDetails(workspaceSlug.toString(), projectId.toString()); setToastAlert({ title: "Success", type: "success", message: "Project updated successfully", }); }) .catch((err) => { console.error(err); }); }; return ( <>

Defaults

Project Lead

{currentProjectDetails ? ( ( { submitChanges({ project_lead: val }); }} isDisabled={!isAdmin} /> )} /> ) : ( )}

Default Assignee

{currentProjectDetails ? ( ( { submitChanges({ default_assignee: val }); }} isDisabled={!isAdmin} /> )} /> ) : ( )}
); });