import { useState, useEffect } from "react"; import { useRouter } from "next/router"; import Link from "next/link"; import useSWR, { mutate } from "swr"; // services import projectService from "services/project.service"; import workspaceService from "services/workspace.service"; // hooks import useToast from "hooks/use-toast"; import useUser from "hooks/use-user"; import useProjectMembers from "hooks/use-project-members"; import useProjectDetails from "hooks/use-project-details"; import { Controller, useForm } from "react-hook-form"; // layouts import { ProjectAuthorizationWrapper } from "layouts/auth-layout"; // components import ConfirmProjectMemberRemove from "components/project/confirm-project-member-remove"; import SendProjectInvitationModal from "components/project/send-project-invitation-modal"; import { MemberSelect, SettingsSidebar } from "components/project"; // ui import { CustomMenu, CustomSearchSelect, CustomSelect, Icon, Loader, PrimaryButton, SecondaryButton, } from "components/ui"; import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs"; // icons import { PlusIcon, XMarkIcon } from "@heroicons/react/24/outline"; // types import type { NextPage } from "next"; import { IProject, IUserLite, IWorkspace } from "types"; // fetch-keys import { PROJECTS_LIST, PROJECT_DETAILS, PROJECT_INVITATIONS_WITH_EMAIL, PROJECT_MEMBERS, PROJECT_MEMBERS_WITH_EMAIL, USER_PROJECT_VIEW, WORKSPACE_DETAILS, } from "constants/fetch-keys"; // constants import { ROLE } from "constants/workspace"; // helper import { truncateText } from "helpers/string.helper"; const defaultValues: Partial = { project_lead: null, default_assignee: null, }; const MembersSettings: NextPage = () => { const [inviteModal, setInviteModal] = useState(false); const [selectedRemoveMember, setSelectedRemoveMember] = useState(null); const [selectedInviteRemoveMember, setSelectedInviteRemoveMember] = useState(null); const { setToastAlert } = useToast(); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { user } = useUser(); const { projectDetails } = useProjectDetails(); const { isOwner } = useProjectMembers( workspaceSlug?.toString(), projectId?.toString(), Boolean(workspaceSlug && projectId) ); const { handleSubmit, reset, control, formState: { isSubmitting }, } = useForm({ defaultValues }); const { data: activeWorkspace } = useSWR( workspaceSlug ? WORKSPACE_DETAILS(workspaceSlug as string) : null, () => (workspaceSlug ? workspaceService.getWorkspace(workspaceSlug as string) : null) ); const { data: people } = useSWR( workspaceSlug && projectId ? PROJECT_MEMBERS(projectId as string) : null, workspaceSlug && projectId ? () => projectService.projectMembers(workspaceSlug as string, projectId as string) : null ); const { data: projectMembers, mutate: mutateMembers } = useSWR( workspaceSlug && projectId ? PROJECT_MEMBERS_WITH_EMAIL(workspaceSlug.toString(), projectId.toString()) : null, workspaceSlug && projectId ? () => projectService.projectMembersWithEmail(workspaceSlug as string, projectId as string) : null ); const { data: projectInvitations, mutate: mutateInvitations } = useSWR( workspaceSlug && projectId ? PROJECT_INVITATIONS_WITH_EMAIL(workspaceSlug.toString(), projectId.toString()) : null, workspaceSlug && projectId ? () => projectService.projectInvitationsWithEmail(workspaceSlug as string, projectId as string) : null ); const { data: memberDetails } = useSWR( workspaceSlug && projectId ? USER_PROJECT_VIEW(projectId.toString()) : null, workspaceSlug && projectId ? () => projectService.projectMemberMe(workspaceSlug.toString(), projectId.toString()) : null ); const members = [ ...(projectMembers?.map((item) => ({ id: item.id, memberId: item.member?.id, avatar: item.member?.avatar, first_name: item.member?.first_name, last_name: item.member?.last_name, email: item.member?.email, display_name: item.member?.display_name, role: item.role, status: true, member: true, })) || []), ...(projectInvitations?.map((item: any) => ({ id: item.id, memberId: item.id, avatar: item.avatar ?? "", first_name: item.first_name ?? item.email, last_name: item.last_name ?? "", email: item.email, display_name: item.email, role: item.role, status: item.accepted, member: false, })) || []), ]; const currentUser = projectMembers?.find((item) => item.member.id === user?.id); const handleProjectInvitationSuccess = () => {}; const onSubmit = async (formData: IProject) => { if (!workspaceSlug || !projectId || !projectDetails) return; const payload: Partial = { default_assignee: formData.default_assignee, project_lead: formData.project_lead === "none" ? null : formData.project_lead, }; await projectService .updateProject(workspaceSlug as string, projectId as string, payload, user) .then((res) => { mutate(PROJECT_DETAILS(projectId as string)); mutate( PROJECTS_LIST(workspaceSlug as string, { is_favorite: "all", }) ); setToastAlert({ title: "Success", type: "success", message: "Project updated successfully", }); }) .catch((err) => { console.log(err); }); }; useEffect(() => { if (projectDetails) reset({ ...projectDetails, default_assignee: projectDetails.default_assignee?.id ?? projectDetails.default_assignee, project_lead: (projectDetails.project_lead as IUserLite)?.id ?? projectDetails.project_lead, workspace: (projectDetails.workspace as IWorkspace).id, }); }, [projectDetails, reset]); const submitChanges = async (formData: Partial) => { if (!workspaceSlug || !projectId) return; const payload: Partial = { default_assignee: formData.default_assignee === "none" ? null : formData.default_assignee, project_lead: formData.project_lead === "none" ? null : formData.project_lead, }; await projectService .updateProject(workspaceSlug as string, projectId as string, payload, user) .then((res) => { mutate(PROJECT_DETAILS(projectId as string)); mutate( PROJECTS_LIST(workspaceSlug as string, { is_favorite: "all", }) ); setToastAlert({ title: "Success", type: "success", message: "Project updated successfully", }); }) .catch((err) => { console.log(err); }); }; const isAdmin = memberDetails?.role === 20; return ( } > { setSelectedRemoveMember(null); setSelectedInviteRemoveMember(null); }} data={members.find( (item) => item.id === selectedRemoveMember || item.id === selectedInviteRemoveMember )} handleDelete={async () => { if (!activeWorkspace || !projectDetails) return; if (selectedRemoveMember) { await projectService.deleteProjectMember( activeWorkspace.slug, projectDetails.id, selectedRemoveMember ); mutateMembers( (prevData: any) => prevData?.filter((item: any) => item.id !== selectedRemoveMember), false ); } if (selectedInviteRemoveMember) { await projectService.deleteProjectInvitation( activeWorkspace.slug, projectDetails.id, selectedInviteRemoveMember ); mutateInvitations( (prevData: any) => prevData?.filter((item: any) => item.id !== selectedInviteRemoveMember), false ); } setToastAlert({ type: "success", message: "Member removed successfully", title: "Success", }); }} /> mutateMembers()} />

Defaults

Project Lead

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

Default Assignee

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

Members

setInviteModal(true)}>Add Member
{!projectMembers || !projectInvitations ? ( ) : (
{members.length > 0 ? members.map((member) => (
{member.avatar && member.avatar !== "" ? (
{member.display_name}
) : member.display_name || member.email ? (
{(member.display_name || member.email)?.charAt(0)}
) : (
?
)}
{member.member ? ( {member.first_name} {member.last_name} ({member.display_name}) ) : (

{member.display_name || member.email}

)} {isOwner && (

{member.email}

)}
{!member.member && (
Pending
)} {ROLE[member.role as keyof typeof ROLE]} {member.memberId !== user?.id && ( )} } value={member.role} onChange={(value: 5 | 10 | 15 | 20 | undefined) => { if (!activeWorkspace || !projectDetails) return; mutateMembers( (prevData: any) => prevData.map((m: any) => m.id === member.id ? { ...m, role: value } : m ), false ); projectService .updateProjectMember( activeWorkspace.slug, projectDetails.id, member.id, { role: value, } ) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "An error occurred while updating member role. Please try again.", }); }); }} position="right" disabled={ member.memberId === user?.id || !member.member || (currentUser && currentUser.role !== 20 && currentUser.role < member.role) } > {Object.keys(ROLE).map((key) => { if ( currentUser && currentUser.role !== 20 && currentUser.role < parseInt(key) ) return null; return ( <>{ROLE[parseInt(key) as keyof typeof ROLE]} ); })} { if (member.member) setSelectedRemoveMember(member.id); else setSelectedInviteRemoveMember(member.id); }} > {" "} {member.memberId !== user?.id ? "Remove member" : "Leave project"}
)) : null}
)}
); }; export default MembersSettings;