import React, { useState } from "react"; import Image from "next/image"; import { useRouter } from "next/router"; import type { NextPage, NextPageContext } from "next"; import useSWR from "swr"; // services import projectService from "lib/services/project.service"; import workspaceService from "lib/services/workspace.service"; // hooks import useToast from "lib/hooks/useToast"; // lib import { requiredAuth } from "lib/auth"; // constants import { ROLE } from "constants/"; // layouts import AppLayout from "layouts/app-layout"; // components import ConfirmProjectMemberRemove from "components/project/confirm-project-member-remove"; import SendProjectInvitationModal from "components/project/send-project-invitation-modal"; // headless ui import { Menu } from "@headlessui/react"; // ui import { Spinner, CustomListbox, BreadcrumbItem, Breadcrumbs, HeaderButton } from "ui"; // icons import { PlusIcon, EllipsisHorizontalIcon } from "@heroicons/react/20/solid"; // fetch-keys import { PROJECT_MEMBERS, PROJECT_INVITATIONS, WORKSPACE_DETAILS, PROJECT_DETAILS, } from "constants/fetch-keys"; const ProjectMembers: NextPage = () => { const [isOpen, setIsOpen] = useState(false); const [selectedMember, setSelectedMember] = useState(null); const [selectedRemoveMember, setSelectedRemoveMember] = useState(null); const [selectedInviteRemoveMember, setSelectedInviteRemoveMember] = useState(null); const { query: { workspaceSlug, projectId }, } = useRouter(); const { setToastAlert } = useToast(); const { data: activeWorkspace } = useSWR( workspaceSlug ? WORKSPACE_DETAILS(workspaceSlug as string) : null, () => (workspaceSlug ? workspaceService.getWorkspace(workspaceSlug as string) : null) ); const { data: activeProject } = useSWR( activeWorkspace && projectId ? PROJECT_DETAILS(projectId as string) : null, activeWorkspace && projectId ? () => projectService.getProject(activeWorkspace.slug, projectId as string) : null ); const { data: projectMembers, mutate: mutateMembers } = useSWR( activeWorkspace && projectId ? PROJECT_MEMBERS(projectId as string) : null, activeWorkspace && projectId ? () => projectService.projectMembers(activeWorkspace.slug, projectId as any) : null, { onErrorRetry(err, _, __, revalidate, revalidateOpts) { if (err?.status === 403) return; setTimeout(() => revalidate(revalidateOpts), 5000); }, } ); const { data: projectInvitations, mutate: mutateInvitations } = useSWR( activeWorkspace && projectId ? PROJECT_INVITATIONS : null, activeWorkspace && projectId ? () => projectService.projectInvitations(activeWorkspace.slug, projectId as any) : null ); let members = [ ...(projectMembers?.map((item: any) => ({ id: item.id, avatar: item.member?.avatar, first_name: item.member?.first_name, email: item.member?.email, role: item.role, status: true, member: true, })) || []), ...(projectInvitations?.map((item: any) => ({ id: item.id, avatar: item.avatar ?? "", first_name: item.first_name ?? item.email, email: item.email, role: item.role, status: item.accepted, member: false, })) || []), ]; return ( } right={ setIsOpen(true)} />} > { setSelectedRemoveMember(null); setSelectedInviteRemoveMember(null); }} data={members.find( (item) => item.id === selectedRemoveMember || item.id === selectedInviteRemoveMember )} handleDelete={async () => { if (!activeWorkspace || !projectId) return; if (selectedRemoveMember) { await projectService.deleteProjectMember( activeWorkspace.slug, projectId as string, selectedRemoveMember ); mutateMembers( (prevData) => prevData?.filter((item: any) => item.id !== selectedRemoveMember), false ); } if (selectedInviteRemoveMember) { await projectService.deleteProjectInvitation( activeWorkspace.slug, projectId as string, selectedInviteRemoveMember ); mutateInvitations( (prevData) => prevData?.filter((item: any) => item.id !== selectedInviteRemoveMember), false ); } setToastAlert({ type: "success", message: "Member removed successfully", title: "Success", }); }} /> {!projectMembers || !projectInvitations ? (
) : (
{members && members.length === 0 ? null : ( {members?.map((member: any) => ( ))}
Name Role Status Edit
{member.avatar && member.avatar !== "" ? ( {member.first_name} ) : ( {member.first_name.charAt(0)} )} {member.email ?? "No email has been added."} {selectedMember === member.id ? ( ({ value: key, display: ROLE[parseInt(key) as keyof typeof ROLE], }))} title={ROLE[member.role as keyof typeof ROLE] ?? "Select Role"} value={member.role} onChange={(value) => { if (!activeWorkspace || !projectId) return; projectService .updateProjectMember( activeWorkspace.slug, projectId as string, member.id, { role: value, } ) .then((res) => { setToastAlert({ type: "success", message: "Member role updated successfully.", title: "Success", }); mutateMembers( (prevData: any) => prevData.map((m: any) => { return m.id === selectedMember ? { ...m, ...res, role: value } : m; }), false ); setSelectedMember(null); }) .catch((err) => { console.log(err); }); }} /> ) : ( ROLE[member.role as keyof typeof ROLE] ?? "None" )} {member.member ? ( Active ) : ( Pending )}
)}
)}
); }; export const getServerSideProps = async (ctx: NextPageContext) => { const user = await requiredAuth(ctx.req?.headers.cookie); const redirectAfterSignIn = ctx.req?.url; if (!user) { return { redirect: { destination: `/signin?next=${redirectAfterSignIn}`, permanent: false, }, }; } return { props: { user, }, }; }; export default ProjectMembers;