Updated the Group List to show the name of the corporate

This commit is contained in:
Tiago Ribeiro
2024-03-26 14:03:58 +00:00
parent 1086e78936
commit bf6c805487

View File

@@ -3,400 +3,308 @@ import Input from "@/components/Low/Input";
import Modal from "@/components/Modal"; import Modal from "@/components/Modal";
import useGroups from "@/hooks/useGroups"; import useGroups from "@/hooks/useGroups";
import useUsers from "@/hooks/useUsers"; import useUsers from "@/hooks/useUsers";
import { Group, User } from "@/interfaces/user"; import {CorporateUser, Group, User} from "@/interfaces/user";
import { import {createColumnHelper, flexRender, getCoreRowModel, useReactTable} from "@tanstack/react-table";
createColumnHelper,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
import axios from "axios"; import axios from "axios";
import { capitalize, uniq } from "lodash"; import {capitalize, uniq} from "lodash";
import { useEffect, useState } from "react"; import {useEffect, useState} from "react";
import { BsPencil, BsQuestionCircleFill, BsTrash } from "react-icons/bs"; import {BsPencil, BsQuestionCircleFill, BsTrash} from "react-icons/bs";
import Select from "react-select"; import Select from "react-select";
import { toast } from "react-toastify"; import {toast} from "react-toastify";
import readXlsxFile from "read-excel-file"; import readXlsxFile from "read-excel-file";
import { useFilePicker } from "use-file-picker"; import {useFilePicker} from "use-file-picker";
const columnHelper = createColumnHelper<Group>(); const columnHelper = createColumnHelper<Group>();
const EMAIL_REGEX = new RegExp( const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/);
/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/,
);
interface CreateDialogProps { interface CreateDialogProps {
user: User; user: User;
users: User[]; users: User[];
group?: Group; group?: Group;
onClose: () => void; onClose: () => void;
} }
const CreatePanel = ({ user, users, group, onClose }: CreateDialogProps) => { const CreatePanel = ({user, users, group, onClose}: CreateDialogProps) => {
const [name, setName] = useState<string | undefined>( const [name, setName] = useState<string | undefined>(group?.name || undefined);
group?.name || undefined, const [admin, setAdmin] = useState<string>(group?.admin || user.id);
); const [participants, setParticipants] = useState<string[]>(group?.participants || []);
const [admin, setAdmin] = useState<string>(group?.admin || user.id); const [isLoading, setIsLoading] = useState(false);
const [participants, setParticipants] = useState<string[]>(
group?.participants || [],
);
const [isLoading, setIsLoading] = useState(false);
const { openFilePicker, filesContent, clear } = useFilePicker({ const {openFilePicker, filesContent, clear} = useFilePicker({
accept: ".xlsx", accept: ".xlsx",
multiple: false, multiple: false,
readAs: "ArrayBuffer", readAs: "ArrayBuffer",
}); });
useEffect(() => { useEffect(() => {
if (filesContent.length > 0) { if (filesContent.length > 0) {
setIsLoading(true); setIsLoading(true);
const file = filesContent[0]; const file = filesContent[0];
readXlsxFile(file.content).then((rows) => { readXlsxFile(file.content).then((rows) => {
const emails = uniq( const emails = uniq(
rows rows
.map((row) => { .map((row) => {
const [email] = row as string[]; const [email] = row as string[];
return EMAIL_REGEX.test(email) && return EMAIL_REGEX.test(email) && !users.map((u) => u.email).includes(email) ? email.toString().trim() : undefined;
!users.map((u) => u.email).includes(email) })
? email.toString().trim() .filter((x) => !!x),
: undefined; );
})
.filter((x) => !!x),
);
if (emails.length === 0) { if (emails.length === 0) {
toast.error("Please upload an Excel file containing e-mails!"); toast.error("Please upload an Excel file containing e-mails!");
clear(); clear();
setIsLoading(false); setIsLoading(false);
return; return;
} }
const emailUsers = [...new Set(emails)] const emailUsers = [...new Set(emails)].map((x) => users.find((y) => y.email.toLowerCase() === x)).filter((x) => x !== undefined);
.map((x) => users.find((y) => y.email.toLowerCase() === x)) const filteredUsers = emailUsers.filter(
.filter((x) => x !== undefined); (x) =>
const filteredUsers = emailUsers.filter( ((user.type === "developer" || user.type === "admin" || user.type === "corporate") &&
(x) => (x?.type === "student" || x?.type === "teacher")) ||
((user.type === "developer" || (user.type === "teacher" && x?.type === "student"),
user.type === "admin" || );
user.type === "corporate") &&
(x?.type === "student" || x?.type === "teacher")) ||
(user.type === "teacher" && x?.type === "student"),
);
setParticipants(filteredUsers.filter((x) => !!x).map((x) => x!.id)); setParticipants(filteredUsers.filter((x) => !!x).map((x) => x!.id));
toast.success( toast.success(
user.type !== "teacher" user.type !== "teacher"
? "Added all teachers and students found in the file you've provided!" ? "Added all teachers and students found in the file you've provided!"
: "Added all students found in the file you've provided!", : "Added all students found in the file you've provided!",
{ toastId: "upload-success" }, {toastId: "upload-success"},
); );
setIsLoading(false); setIsLoading(false);
}); });
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [filesContent, user.type, users]); }, [filesContent, user.type, users]);
const submit = () => { const submit = () => {
setIsLoading(true); setIsLoading(true);
if (name !== group?.name && (name === "Students" || name === "Teachers")) { if (name !== group?.name && (name === "Students" || name === "Teachers")) {
toast.error( toast.error("That group name is reserved and cannot be used, please enter another one.");
"That group name is reserved and cannot be used, please enter another one.", setIsLoading(false);
); return;
setIsLoading(false); }
return;
}
(group ? axios.patch : axios.post)( (group ? axios.patch : axios.post)(group ? `/api/groups/${group.id}` : "/api/groups", {name, admin, participants})
group ? `/api/groups/${group.id}` : "/api/groups", .then(() => {
{ name, admin, participants }, toast.success(`Group "${name}" ${group ? "edited" : "created"} successfully`);
) return true;
.then(() => { })
toast.success( .catch(() => {
`Group "${name}" ${group ? "edited" : "created"} successfully`, toast.error("Something went wrong, please try again later!");
); return false;
return true; })
}) .finally(() => {
.catch(() => { setIsLoading(false);
toast.error("Something went wrong, please try again later!"); onClose();
return false; });
}) };
.finally(() => {
setIsLoading(false);
onClose();
});
};
return ( return (
<div className="mt-4 flex w-full flex-col gap-12 px-4 py-2"> <div className="mt-4 flex w-full flex-col gap-12 px-4 py-2">
<div className="flex flex-col gap-8"> <div className="flex flex-col gap-8">
<Input <Input name="name" type="text" label="Name" defaultValue={name} onChange={setName} required disabled={group?.disableEditing} />
name="name" <div className="flex w-full flex-col gap-3">
type="text" <div className="flex items-center gap-2">
label="Name" <label className="text-mti-gray-dim text-base font-normal">Participants</label>
defaultValue={name} <div className="tooltip" data-tip="The Excel file should only include a column with the desired e-mails.">
onChange={setName} <BsQuestionCircleFill />
required </div>
disabled={group?.disableEditing} </div>
/> <div className="flex w-full gap-8">
<div className="flex w-full flex-col gap-3"> <Select
<div className="flex items-center gap-2"> className="w-full"
<label className="text-mti-gray-dim text-base font-normal"> value={participants.map((x) => ({
Participants value: x,
</label> label: `${users.find((y) => y.id === x)?.email} - ${users.find((y) => y.id === x)?.name}`,
<div }))}
className="tooltip" placeholder="Participants..."
data-tip="The Excel file should only include a column with the desired e-mails." defaultValue={participants.map((x) => ({
> value: x,
<BsQuestionCircleFill /> label: `${users.find((y) => y.id === x)?.email} - ${users.find((y) => y.id === x)?.name}`,
</div> }))}
</div> options={users
<div className="flex w-full gap-8"> .filter((x) => (user.type === "teacher" ? x.type === "student" : x.type === "student" || x.type === "teacher"))
<Select .map((x) => ({value: x.id, label: `${x.email} - ${x.name}`}))}
className="w-full" onChange={(value) => setParticipants(value.map((x) => x.value))}
value={participants.map((x) => ({ isMulti
value: x, isSearchable
label: `${users.find((y) => y.id === x)?.email} - ${users.find((y) => y.id === x)?.name}`, menuPortalTarget={document?.body}
}))} styles={{
placeholder="Participants..." menuPortal: (base) => ({...base, zIndex: 9999}),
defaultValue={participants.map((x) => ({ control: (styles) => ({
value: x, ...styles,
label: `${users.find((y) => y.id === x)?.email} - ${users.find((y) => y.id === x)?.name}`, backgroundColor: "white",
}))} borderRadius: "999px",
options={users padding: "1rem 1.5rem",
.filter((x) => zIndex: "40",
user.type === "teacher" }),
? x.type === "student" }}
: x.type === "student" || x.type === "teacher", />
) {user.type !== "teacher" && (
.map((x) => ({ value: x.id, label: `${x.email} - ${x.name}` }))} <Button className="w-full max-w-[300px]" onClick={openFilePicker} isLoading={isLoading} variant="outline">
onChange={(value) => setParticipants(value.map((x) => x.value))} {filesContent.length === 0 ? "Upload participants Excel file" : filesContent[0].name}
isMulti </Button>
isSearchable )}
menuPortalTarget={document?.body} </div>
styles={{ </div>
menuPortal: (base) => ({ ...base, zIndex: 9999 }), </div>
control: (styles) => ({ <div className="mt-8 flex w-full items-center justify-end gap-8">
...styles, <Button variant="outline" color="red" className="w-full max-w-[200px]" isLoading={isLoading} onClick={onClose}>
backgroundColor: "white", Cancel
borderRadius: "999px", </Button>
padding: "1rem 1.5rem", <Button className="w-full max-w-[200px]" onClick={submit} isLoading={isLoading} disabled={!name}>
zIndex: "40", Submit
}), </Button>
}} </div>
/> </div>
{user.type !== "teacher" && ( );
<Button
className="w-full max-w-[300px]"
onClick={openFilePicker}
isLoading={isLoading}
variant="outline"
>
{filesContent.length === 0
? "Upload participants Excel file"
: filesContent[0].name}
</Button>
)}
</div>
</div>
</div>
<div className="mt-8 flex w-full items-center justify-end gap-8">
<Button
variant="outline"
color="red"
className="w-full max-w-[200px]"
isLoading={isLoading}
onClick={onClose}
>
Cancel
</Button>
<Button
className="w-full max-w-[200px]"
onClick={submit}
isLoading={isLoading}
disabled={!name}
>
Submit
</Button>
</div>
</div>
);
}; };
const filterTypes = ["corporate", "teacher"]; const filterTypes = ["corporate", "teacher"];
export default function GroupList({ user }: { user: User }) { export default function GroupList({user}: {user: User}) {
const [isCreating, setIsCreating] = useState(false); const [isCreating, setIsCreating] = useState(false);
const [editingGroup, setEditingGroup] = useState<Group>(); const [editingGroup, setEditingGroup] = useState<Group>();
const [filterByUser, setFilterByUser] = useState(false); const [filterByUser, setFilterByUser] = useState(false);
const { users } = useUsers(); const {users} = useUsers();
const { groups, reload } = useGroups( const {groups, reload} = useGroups(user && filterTypes.includes(user?.type) ? user.id : undefined);
user && filterTypes.includes(user?.type) ? user.id : undefined,
);
useEffect(() => { useEffect(() => {
if (user && (user.type === "corporate" || user.type === "teacher")) { if (user && (user.type === "corporate" || user.type === "teacher")) {
setFilterByUser(true); setFilterByUser(true);
} }
}, [user]); }, [user]);
const deleteGroup = (group: Group) => { const deleteGroup = (group: Group) => {
if (!confirm(`Are you sure you want to delete "${group.name}"?`)) return; if (!confirm(`Are you sure you want to delete "${group.name}"?`)) return;
axios axios
.delete<{ ok: boolean }>(`/api/groups/${group.id}`) .delete<{ok: boolean}>(`/api/groups/${group.id}`)
.then(() => toast.success(`Group "${group.name}" deleted successfully`)) .then(() => toast.success(`Group "${group.name}" deleted successfully`))
.catch(() => toast.error("Something went wrong, please try again later!")) .catch(() => toast.error("Something went wrong, please try again later!"))
.finally(reload); .finally(reload);
}; };
const defaultColumns = [ const defaultColumns = [
columnHelper.accessor("id", { columnHelper.accessor("id", {
header: "ID", header: "ID",
cell: (info) => info.getValue(), cell: (info) => info.getValue(),
}), }),
columnHelper.accessor("name", { columnHelper.accessor("name", {
header: "Name", header: "Name",
cell: (info) => info.getValue(), cell: (info) => info.getValue(),
}), }),
columnHelper.accessor("admin", { columnHelper.accessor("admin", {
header: "Admin", header: "Admin",
cell: (info) => ( cell: (info) => (
<div <div className="tooltip" data-tip={capitalize(users.find((x) => x.id === info.getValue())?.type)}>
className="tooltip" {users.find((x) => x.id === info.getValue())?.type === "corporate"
data-tip={capitalize( ? (users.find((x) => x.id === info.getValue()) as CorporateUser)?.corporateInformation?.companyInformation?.name
users.find((x) => x.id === info.getValue())?.type, : users.find((x) => x.id === info.getValue())?.name}
)} </div>
> ),
{users.find((x) => x.id === info.getValue())?.name} }),
</div> columnHelper.accessor("participants", {
), header: "Participants",
}), cell: (info) =>
columnHelper.accessor("participants", { info
header: "Participants", .getValue()
cell: (info) => .map((x) => users.find((y) => y.id === x)?.name)
info .join(", "),
.getValue() }),
.map((x) => users.find((y) => y.id === x)?.name) {
.join(", "), header: "",
}), id: "actions",
{ cell: ({row}: {row: {original: Group}}) => {
header: "", return (
id: "actions", <>
cell: ({ row }: { row: { original: Group } }) => { {user && (user.type === "developer" || user.type === "admin" || user.id === row.original.admin) && (
return ( <div className="flex gap-2">
<> {(!row.original.disableEditing || ["developer", "admin"].includes(user.type)) && (
{user && <div data-tip="Edit" className="tooltip cursor-pointer" onClick={() => setEditingGroup(row.original)}>
(user.type === "developer" || <BsPencil className="hover:text-mti-purple-light transition duration-300 ease-in-out" />
user.type === "admin" || </div>
user.id === row.original.admin) && ( )}
<div className="flex gap-2"> {(!row.original.disableEditing || ["developer", "admin"].includes(user.type)) && (
{(!row.original.disableEditing || <div data-tip="Delete" className="tooltip cursor-pointer" onClick={() => deleteGroup(row.original)}>
["developer", "admin"].includes(user.type)) && ( <BsTrash className="hover:text-mti-purple-light transition duration-300 ease-in-out" />
<div </div>
data-tip="Edit" )}
className="tooltip cursor-pointer" </div>
onClick={() => setEditingGroup(row.original)} )}
> </>
<BsPencil className="hover:text-mti-purple-light transition duration-300 ease-in-out" /> );
</div> },
)} },
{(!row.original.disableEditing || ];
["developer", "admin"].includes(user.type)) && (
<div
data-tip="Delete"
className="tooltip cursor-pointer"
onClick={() => deleteGroup(row.original)}
>
<BsTrash className="hover:text-mti-purple-light transition duration-300 ease-in-out" />
</div>
)}
</div>
)}
</>
);
},
},
];
const table = useReactTable({ const table = useReactTable({
data: groups, data: groups,
columns: defaultColumns, columns: defaultColumns,
getCoreRowModel: getCoreRowModel(), getCoreRowModel: getCoreRowModel(),
}); });
const closeModal = () => { const closeModal = () => {
setIsCreating(false); setIsCreating(false);
setEditingGroup(undefined); setEditingGroup(undefined);
reload(); reload();
}; };
return ( return (
<div className="h-full w-full rounded-xl"> <div className="h-full w-full rounded-xl">
<Modal <Modal isOpen={isCreating || !!editingGroup} onClose={closeModal} title={editingGroup ? `Editing ${editingGroup.name}` : "New Group"}>
isOpen={isCreating || !!editingGroup} <CreatePanel
onClose={closeModal} group={editingGroup}
title={editingGroup ? `Editing ${editingGroup.name}` : "New Group"} user={user}
> onClose={closeModal}
<CreatePanel users={
group={editingGroup} user?.type === "corporate" || user?.type === "teacher"
user={user} ? users.filter(
onClose={closeModal} (u) =>
users={ groups
user?.type === "corporate" || user?.type === "teacher" .filter((g) => g.admin === user.id)
? users.filter( .flatMap((g) => g.participants)
(u) => .includes(u.id) || groups.flatMap((g) => g.participants).includes(u.id),
groups )
.filter((g) => g.admin === user.id) : users
.flatMap((g) => g.participants) }
.includes(u.id) || />
groups.flatMap((g) => g.participants).includes(u.id), </Modal>
) <table className="bg-mti-purple-ultralight/40 w-full rounded-xl">
: users <thead>
} {table.getHeaderGroups().map((headerGroup) => (
/> <tr key={headerGroup.id}>
</Modal> {headerGroup.headers.map((header) => (
<table className="bg-mti-purple-ultralight/40 w-full rounded-xl"> <th className="py-4" key={header.id}>
<thead> {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
{table.getHeaderGroups().map((headerGroup) => ( </th>
<tr key={headerGroup.id}> ))}
{headerGroup.headers.map((header) => ( </tr>
<th className="py-4" key={header.id}> ))}
{header.isPlaceholder </thead>
? null <tbody className="px-2">
: flexRender( {table.getRowModel().rows.map((row) => (
header.column.columnDef.header, <tr className="even:bg-mti-purple-ultralight/40 rounded-lg py-2 odd:bg-white" key={row.id}>
header.getContext(), {row.getVisibleCells().map((cell) => (
)} <td className="px-4 py-2" key={cell.id}>
</th> {flexRender(cell.column.columnDef.cell, cell.getContext())}
))} </td>
</tr> ))}
))} </tr>
</thead> ))}
<tbody className="px-2"> </tbody>
{table.getRowModel().rows.map((row) => ( </table>
<tr
className="even:bg-mti-purple-ultralight/40 rounded-lg py-2 odd:bg-white"
key={row.id}
>
{row.getVisibleCells().map((cell) => (
<td className="px-4 py-2" key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
<button <button
onClick={() => setIsCreating(true)} onClick={() => setIsCreating(true)}
className="bg-mti-purple-light hover:bg-mti-purple w-full py-2 text-white transition duration-300 ease-in-out" className="bg-mti-purple-light hover:bg-mti-purple w-full py-2 text-white transition duration-300 ease-in-out">
> New Group
New Group </button>
</button> </div>
</div> );
);
} }