import Button from "@/components/Low/Button"; import {PERMISSIONS} from "@/constants/userPermissions"; import useUsers from "@/hooks/useUsers"; import {Type, User} from "@/interfaces/user"; import {Popover, Transition} from "@headlessui/react"; import {createColumnHelper, flexRender, getCoreRowModel, useReactTable} from "@tanstack/react-table"; import axios from "axios"; import clsx from "clsx"; import {capitalize} from "lodash"; import {Fragment} from "react"; import {BsCheck, BsCheckCircle, BsFillExclamationOctagonFill, BsPerson, BsStop, BsTrash} from "react-icons/bs"; import {toast} from "react-toastify"; const columnHelper = createColumnHelper(); export default function UserList({user}: {user: User}) { const {users, reload} = useUsers(); const deleteAccount = (user: User) => { if (!confirm(`Are you sure you want to delete ${user.name}'s account?`)) return; axios .delete<{ok: boolean}>(`/api/user?id=${user.id}`) .then(() => { toast.success("User deleted successfully!"); reload(); }) .catch(() => { toast.error("Something went wrong!", {toastId: "delete-error"}); }); }; const updateAccountType = (user: User, type: Type) => { if (!confirm(`Are you sure you want to update ${user.name}'s account from ${capitalize(user.type)} to ${capitalize(type)}?`)) return; axios .post<{user?: User; ok?: boolean}>(`/api/users/update?id=${user.id}`, {...user, type}) .then(() => { toast.success("User type updated successfully!"); reload(); }) .catch(() => { toast.error("Something went wrong!", {toastId: "update-error"}); }); }; const verifyAccount = (user: User) => { axios .post<{user?: User; ok?: boolean}>(`/api/users/update?id=${user.id}`, {...user, isVerified: true}) .then(() => { toast.success("User verified successfully!"); reload(); }) .catch(() => { toast.error("Something went wrong!", {toastId: "update-error"}); }); }; const toggleDisableAccount = (user: User) => { if ( !confirm( `Are you sure you want to ${user.isDisabled ? "enable" : "disable"} ${ user.name }'s account? This change is usually related to their payment state.`, ) ) return; axios .post<{user?: User; ok?: boolean}>(`/api/users/update?id=${user.id}`, {...user, isDisabled: !user.isDisabled}) .then(() => { toast.success(`User ${user.isDisabled ? "enabled" : "disabled"} successfully!`); reload(); }) .catch(() => { toast.error("Something went wrong!", {toastId: "update-error"}); }); }; const defaultColumns = [ columnHelper.accessor("name", { header: "Name", cell: (info) => info.getValue(), enableSorting: true, }), columnHelper.accessor("email", { header: "E-mail", cell: (info) => info.getValue(), }), columnHelper.accessor("type", { header: "Type", cell: (info) => capitalize(info.getValue()), }), columnHelper.accessor("isVerified", { header: "Verification", cell: (info) => (
), }), { header: "", id: "actions", cell: ({row}: {row: {original: User}}) => { return (
{PERMISSIONS.updateUser[row.original.type].includes(user.type) && (
)} {!row.original.isVerified && PERMISSIONS.updateUser[row.original.type].includes(user.type) && (
verifyAccount(row.original)}>
)} {PERMISSIONS.updateUser[row.original.type].includes(user.type) && (
toggleDisableAccount(row.original)}> {row.original.isDisabled ? ( ) : ( )}
)} {PERMISSIONS.deleteUser[row.original.type].includes(user.type) && (
deleteAccount(row.original)}>
)}
); }, }, ]; const table = useReactTable({ data: users, columns: defaultColumns, getCoreRowModel: getCoreRowModel(), }); return ( {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( ))} ))} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( ))} ))}
{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
); }