2023-11-01 08:12:51 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import useSWR from "swr";
|
|
|
|
// store
|
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
|
|
|
// hooks
|
|
|
|
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 "types";
|
|
|
|
// fetch-keys
|
|
|
|
import { PROJECT_MEMBERS } from "constants/fetch-keys";
|
2023-12-04 14:33:23 +00:00
|
|
|
import { EUserWorkspaceRoles } from "constants/workspace";
|
2023-11-01 08:12:51 +00:00
|
|
|
|
|
|
|
const defaultValues: Partial<IProject> = {
|
|
|
|
project_lead: null,
|
|
|
|
default_assignee: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ProjectSettingsMemberDefaults: React.FC = observer(() => {
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
// store
|
|
|
|
const { user: userStore, project: projectStore } = useMobxStore();
|
2023-11-01 13:52:10 +00:00
|
|
|
const { currentProjectDetails } = projectStore;
|
|
|
|
const { currentProjectRole } = userStore;
|
2023-12-04 14:33:23 +00:00
|
|
|
const isAdmin = currentProjectRole === EUserWorkspaceRoles.ADMIN;
|
2023-11-01 08:12:51 +00:00
|
|
|
// hooks
|
|
|
|
const { setToastAlert } = useToast();
|
2023-11-01 13:52:10 +00:00
|
|
|
// form info
|
2023-11-01 08:12:51 +00:00
|
|
|
const { reset, control } = useForm<IProject>({ defaultValues });
|
2023-11-01 13:52:10 +00:00
|
|
|
// fetching user members
|
2023-11-01 08:12:51 +00:00
|
|
|
useSWR(
|
|
|
|
workspaceSlug && projectId ? PROJECT_MEMBERS(projectId.toString()) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => projectStore.fetchProjectDetails(workspaceSlug.toString(), projectId.toString())
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-11-01 13:52:10 +00:00
|
|
|
if (!currentProjectDetails) return;
|
2023-11-01 08:12:51 +00:00
|
|
|
|
|
|
|
reset({
|
2023-11-01 13:52:10 +00:00
|
|
|
...currentProjectDetails,
|
|
|
|
default_assignee: currentProjectDetails.default_assignee?.id ?? currentProjectDetails.default_assignee,
|
|
|
|
project_lead: (currentProjectDetails.project_lead as IUserLite)?.id ?? currentProjectDetails.project_lead,
|
|
|
|
workspace: (currentProjectDetails.workspace as IWorkspace).id,
|
2023-11-01 08:12:51 +00:00
|
|
|
});
|
2023-11-01 13:52:10 +00:00
|
|
|
}, [currentProjectDetails, reset]);
|
2023-11-01 08:12:51 +00:00
|
|
|
|
|
|
|
const submitChanges = async (formData: Partial<IProject>) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
reset({
|
2023-11-01 13:52:10 +00:00
|
|
|
...currentProjectDetails,
|
|
|
|
default_assignee: currentProjectDetails?.default_assignee?.id ?? currentProjectDetails?.default_assignee,
|
|
|
|
project_lead: (currentProjectDetails?.project_lead as IUserLite)?.id ?? currentProjectDetails?.project_lead,
|
2023-11-01 08:12:51 +00:00
|
|
|
...formData,
|
|
|
|
});
|
|
|
|
|
|
|
|
await projectStore
|
|
|
|
.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(() => {
|
|
|
|
projectStore.fetchProjectDetails(workspaceSlug.toString(), projectId.toString());
|
|
|
|
setToastAlert({
|
|
|
|
title: "Success",
|
|
|
|
type: "success",
|
|
|
|
message: "Project updated successfully",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="flex items-center border-b border-custom-border-100 py-3.5">
|
2023-11-01 08:12:51 +00:00
|
|
|
<h3 className="text-xl font-medium">Defaults</h3>
|
|
|
|
</div>
|
|
|
|
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="flex w-full flex-col gap-2 pb-4">
|
|
|
|
<div className="flex w-full items-center gap-4 py-8">
|
|
|
|
<div className="flex w-1/2 flex-col gap-2">
|
2023-11-01 08:12:51 +00:00
|
|
|
<h4 className="text-sm">Project Lead</h4>
|
|
|
|
<div className="">
|
2023-11-01 13:52:10 +00:00
|
|
|
{currentProjectDetails ? (
|
2023-11-01 08:12:51 +00:00
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="project_lead"
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<MemberSelect
|
|
|
|
value={value}
|
|
|
|
onChange={(val: string) => {
|
|
|
|
submitChanges({ project_lead: val });
|
|
|
|
}}
|
|
|
|
isDisabled={!isAdmin}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Loader className="h-9 w-full">
|
|
|
|
<Loader.Item width="100%" height="100%" />
|
|
|
|
</Loader>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="flex w-1/2 flex-col gap-2">
|
2023-11-01 08:12:51 +00:00
|
|
|
<h4 className="text-sm">Default Assignee</h4>
|
|
|
|
<div className="">
|
2023-11-01 13:52:10 +00:00
|
|
|
{currentProjectDetails ? (
|
2023-11-01 08:12:51 +00:00
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
name="default_assignee"
|
|
|
|
render={({ field: { value } }) => (
|
|
|
|
<MemberSelect
|
|
|
|
value={value}
|
|
|
|
onChange={(val: string) => {
|
|
|
|
submitChanges({ default_assignee: val });
|
|
|
|
}}
|
|
|
|
isDisabled={!isAdmin}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Loader className="h-9 w-full">
|
|
|
|
<Loader.Item width="100%" height="100%" />
|
|
|
|
</Loader>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|