2022-11-19 14:21:26 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
// next
|
2022-12-06 04:41:56 +00:00
|
|
|
import Image from "next/image";
|
2022-11-19 14:21:26 +00:00
|
|
|
import type { NextPage } from "next";
|
2022-12-06 04:41:56 +00:00
|
|
|
import { useRouter } from "next/router";
|
2022-11-19 14:21:26 +00:00
|
|
|
// swr
|
|
|
|
import useSWR from "swr";
|
|
|
|
// headless ui
|
|
|
|
import { Menu } from "@headlessui/react";
|
|
|
|
// services
|
|
|
|
import projectService from "lib/services/project.service";
|
|
|
|
// hooks
|
|
|
|
import useUser from "lib/hooks/useUser";
|
2022-12-06 04:41:56 +00:00
|
|
|
import useToast from "lib/hooks/useToast";
|
2022-11-19 14:21:26 +00:00
|
|
|
// fetching keys
|
|
|
|
import { PROJECT_MEMBERS, PROJECT_INVITATIONS } from "constants/fetch-keys";
|
|
|
|
// layouts
|
2022-11-24 13:48:18 +00:00
|
|
|
import AdminLayout from "layouts/AdminLayout";
|
2022-11-19 14:21:26 +00:00
|
|
|
// components
|
|
|
|
import SendProjectInvitationModal from "components/project/SendProjectInvitationModal";
|
2022-12-06 04:41:56 +00:00
|
|
|
import ConfirmProjectMemberRemove from "components/project/ConfirmProjectMemberRemove";
|
2022-11-19 14:21:26 +00:00
|
|
|
// ui
|
2022-12-06 04:41:56 +00:00
|
|
|
import { Spinner, CustomListbox } from "ui";
|
2022-11-19 14:21:26 +00:00
|
|
|
// icons
|
|
|
|
import { PlusIcon, EllipsisHorizontalIcon } from "@heroicons/react/20/solid";
|
|
|
|
import HeaderButton from "ui/HeaderButton";
|
|
|
|
import { BreadcrumbItem, Breadcrumbs } from "ui/Breadcrumbs";
|
|
|
|
|
|
|
|
const ROLE = {
|
|
|
|
5: "Guest",
|
|
|
|
10: "Viewer",
|
|
|
|
15: "Member",
|
|
|
|
20: "Admin",
|
|
|
|
};
|
|
|
|
|
|
|
|
const ProjectMembers: NextPage = () => {
|
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
2022-12-06 04:41:56 +00:00
|
|
|
|
|
|
|
const [selectedMember, setSelectedMember] = useState<string | null>(null);
|
|
|
|
|
|
|
|
const [selectedRemoveMember, setSelectedRemoveMember] = useState<string | null>(null);
|
|
|
|
const [selectedInviteRemoveMember, setSelectedInviteRemoveMember] = useState<string | null>(null);
|
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
const { activeWorkspace, activeProject } = useUser();
|
|
|
|
|
2022-12-06 04:41:56 +00:00
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const { projectId } = router.query;
|
|
|
|
|
|
|
|
const { data: projectMembers, mutate: mutateMembers } = useSWR(
|
|
|
|
activeWorkspace && projectId ? PROJECT_MEMBERS(projectId as string) : null,
|
|
|
|
activeWorkspace && projectId
|
|
|
|
? () => projectService.projectMembers(activeWorkspace.slug, projectId as any)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
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,
|
2022-12-01 14:29:21 +00:00
|
|
|
avatar: item.member?.avatar,
|
|
|
|
first_name: item.member?.first_name,
|
2022-11-19 14:21:26 +00:00
|
|
|
email: item.member?.email,
|
|
|
|
role: item.role,
|
|
|
|
status: true,
|
|
|
|
member: true,
|
|
|
|
})) || []),
|
|
|
|
...(projectInvitations?.map((item: any) => ({
|
|
|
|
id: item.id,
|
2022-12-01 14:29:21 +00:00
|
|
|
avatar: item.avatar ?? "",
|
|
|
|
first_name: item.first_name ?? item.email,
|
2022-11-19 14:21:26 +00:00
|
|
|
email: item.email,
|
|
|
|
role: item.role,
|
|
|
|
status: item.accepted,
|
|
|
|
member: false,
|
|
|
|
})) || []),
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
2022-11-24 13:48:18 +00:00
|
|
|
<AdminLayout>
|
2022-12-06 04:41:56 +00:00
|
|
|
<ConfirmProjectMemberRemove
|
|
|
|
isOpen={Boolean(selectedRemoveMember) || Boolean(selectedInviteRemoveMember)}
|
|
|
|
onClose={() => {
|
|
|
|
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: any[]) =>
|
|
|
|
prevData?.filter((item: any) => item.id !== selectedRemoveMember),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (selectedInviteRemoveMember) {
|
|
|
|
await projectService.deleteProjectInvitation(
|
|
|
|
activeWorkspace.slug,
|
|
|
|
projectId as string,
|
|
|
|
selectedInviteRemoveMember
|
|
|
|
);
|
|
|
|
mutateInvitations(
|
|
|
|
(prevData: any[]) =>
|
|
|
|
prevData?.filter((item: any) => item.id !== selectedInviteRemoveMember),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
message: "Member removed successfully",
|
|
|
|
title: "Success",
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
2022-11-19 14:21:26 +00:00
|
|
|
<SendProjectInvitationModal isOpen={isOpen} setIsOpen={setIsOpen} members={members} />
|
2022-11-24 13:48:18 +00:00
|
|
|
{!projectMembers || !projectInvitations ? (
|
|
|
|
<div className="h-full w-full grid place-items-center px-4 sm:px-0">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="h-full w-full space-y-5">
|
|
|
|
<Breadcrumbs>
|
|
|
|
<BreadcrumbItem title="Projects" link="/projects" />
|
|
|
|
<BreadcrumbItem title={`${activeProject?.name ?? "Project"} Members`} />
|
|
|
|
</Breadcrumbs>
|
|
|
|
<div className="flex items-center justify-between cursor-pointer w-full">
|
|
|
|
<h2 className="text-2xl font-medium">Invite Members</h2>
|
|
|
|
<HeaderButton Icon={PlusIcon} label="Add Member" onClick={() => setIsOpen(true)} />
|
2022-11-19 14:21:26 +00:00
|
|
|
</div>
|
2022-11-24 13:48:18 +00:00
|
|
|
{members && members.length === 0 ? null : (
|
|
|
|
<table className="min-w-full table-fixed border border-gray-300 md:rounded-lg divide-y divide-gray-300">
|
|
|
|
<thead className="bg-gray-50">
|
|
|
|
<tr>
|
|
|
|
<th
|
|
|
|
scope="col"
|
|
|
|
className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6"
|
|
|
|
>
|
|
|
|
Name
|
|
|
|
</th>
|
|
|
|
<th
|
|
|
|
scope="col"
|
|
|
|
className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6"
|
|
|
|
>
|
|
|
|
Role
|
|
|
|
</th>
|
|
|
|
<th
|
|
|
|
scope="col"
|
|
|
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 sm:pl-6"
|
|
|
|
>
|
|
|
|
Status
|
|
|
|
</th>
|
|
|
|
<th scope="col" className="relative py-3.5 pl-3 pr-4 sm:pr-6 w-10">
|
|
|
|
<span className="sr-only">Edit</span>
|
|
|
|
</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody className="divide-y divide-gray-200 bg-white">
|
|
|
|
{members?.map((member: any) => (
|
|
|
|
<tr key={member.id}>
|
2022-12-01 14:29:21 +00:00
|
|
|
<td className="whitespace-nowrap flex items-center gap-2 py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6">
|
|
|
|
{member.avatar && member.avatar !== "" ? (
|
|
|
|
<Image
|
|
|
|
src={member.avatar}
|
|
|
|
height={20}
|
|
|
|
width={20}
|
|
|
|
className="rounded-full"
|
|
|
|
alt={member.first_name}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<span className="h-5 w-5 capitalize bg-gray-700 text-white grid place-items-center rounded-full">
|
|
|
|
{member.first_name.charAt(0)}
|
|
|
|
</span>
|
|
|
|
)}
|
2022-11-24 13:48:18 +00:00
|
|
|
{member.email ?? "No email has been added."}
|
|
|
|
</td>
|
|
|
|
<td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6">
|
2022-12-06 04:41:56 +00:00
|
|
|
{selectedMember === member.id ? (
|
|
|
|
<CustomListbox
|
|
|
|
options={Object.keys(ROLE).map((key) => ({
|
|
|
|
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"
|
|
|
|
)}
|
2022-11-24 13:48:18 +00:00
|
|
|
</td>
|
|
|
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 sm:pl-6">
|
2022-12-07 04:45:57 +00:00
|
|
|
{member.member ? (
|
2022-11-24 13:48:18 +00:00
|
|
|
<span className="p-0.5 px-2 text-sm bg-green-700 text-white rounded-full">
|
2022-12-06 04:41:56 +00:00
|
|
|
Active
|
2022-11-24 13:48:18 +00:00
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<span className="p-0.5 px-2 text-sm bg-yellow-400 text-black rounded-full">
|
|
|
|
Pending
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
|
|
|
|
<Menu>
|
|
|
|
<Menu.Button>
|
|
|
|
<EllipsisHorizontalIcon
|
|
|
|
width="16"
|
|
|
|
height="16"
|
|
|
|
className="inline text-gray-500"
|
|
|
|
/>
|
|
|
|
</Menu.Button>
|
|
|
|
<Menu.Items className="absolute z-50 w-28 bg-white rounded border cursor-pointer -left-20 top-9">
|
|
|
|
<Menu.Item>
|
|
|
|
<div className="hover:bg-gray-100 border-b last:border-0">
|
|
|
|
<button
|
|
|
|
className="w-full text-left py-2 pl-2"
|
|
|
|
type="button"
|
2022-12-06 04:41:56 +00:00
|
|
|
onClick={() => {
|
2022-12-07 04:45:57 +00:00
|
|
|
if (!member.member) {
|
2022-12-06 04:41:56 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
message: "You can't edit a pending invitation.",
|
|
|
|
title: "Error",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setSelectedMember(member.id);
|
|
|
|
}
|
|
|
|
}}
|
2022-11-24 13:48:18 +00:00
|
|
|
>
|
|
|
|
Edit
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Menu.Item>
|
|
|
|
<Menu.Item>
|
|
|
|
<div className="hover:bg-gray-100 border-b last:border-0">
|
|
|
|
<button
|
|
|
|
className="w-full text-left py-2 pl-2"
|
|
|
|
type="button"
|
2022-12-06 04:41:56 +00:00
|
|
|
onClick={() => {
|
2022-12-07 04:45:57 +00:00
|
|
|
if (member.member) {
|
2022-12-06 04:41:56 +00:00
|
|
|
setSelectedRemoveMember(member.id);
|
|
|
|
} else {
|
|
|
|
setSelectedInviteRemoveMember(member.id);
|
|
|
|
}
|
2022-11-24 13:48:18 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
Remove
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu.Items>
|
|
|
|
</Menu>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</AdminLayout>
|
2022-11-19 14:21:26 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ProjectMembers;
|