git-subtree-dir: apps/web git-subtree-mainline:eaa6d2276agit-subtree-split:459af9d46a
281 lines
11 KiB
React
281 lines
11 KiB
React
// modules/admin/components/users/UsersTable.jsx
|
|
|
|
import { useMemo, useRef, useState, useEffect } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
import { useUsers } from "@/contexts/AdminUserContext";
|
|
import { useDashboard } from "@/contexts/AdminDashboardContext";
|
|
import { useAuth } from "@/contexts/AuthContext";
|
|
|
|
import DataTable from "@/components/generic/Table/DataTable";
|
|
import { FilterSheet } from "@/components/generic/Sheet/FilterSheet";
|
|
import { ArchiveDialog } from "@/components/generic/Dialogs/ArchiveDialog";
|
|
import { BanUserDialog } from "@/components/generic/Dialogs/BanUserDialog";
|
|
import { UnbanDialog } from "@/components/generic/Dialogs/UnbanDialog";
|
|
import { MakeAdminDialog } from "@/components/generic/Dialogs/MakeAdminDialog";
|
|
import { DemoteAdminDialog } from "@/components/generic/Dialogs/DemoteAdminDialog";
|
|
import { TableDashboard } from "@/components/generic/Dashboard/TableDashboard";
|
|
|
|
import { buildDataColumns, columnPinning } from "../../config/users/columns.config";
|
|
import { buildToolbarActions } from "../../config/users/toolbar.config";
|
|
import { buildSelectionActions } from "../../config/users/selection.config";
|
|
import { buildRowActions } from "../../config/users/rowActions.config";
|
|
|
|
import { USER_STAT_MAP } from "@/data/adminDashboard.data";
|
|
import { getTimestamp } from "@/utils/timestamp.util";
|
|
import { formatGeneratedBy } from "@/utils/generatedBy.util";
|
|
|
|
export default function UsersTable() {
|
|
const [archiveTarget, setArchiveTarget] = useState(null);
|
|
const [archiveIds, setArchiveIds] = useState(null);
|
|
const [banTarget, setBanTarget] = useState(null);
|
|
const [banIds, setBanIds] = useState(null);
|
|
const [unbanTarget, setUnbanTarget] = useState(null);
|
|
const [unbanIds, setUnbanIds] = useState(null);
|
|
const [makeAdminTarget, setMakeAdminTarget] = useState(null);
|
|
const [demoteAdminTarget, setDemoteAdminTarget] = useState(null);
|
|
|
|
const tableRefsRef = useRef({
|
|
getFilters: () => [],
|
|
getSort: () => [],
|
|
resetSelection: () => { },
|
|
setFilters: () => { },
|
|
});
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const { user: currentUser } = useAuth();
|
|
|
|
const {
|
|
users, attributes, pagination, setPagination, loading,
|
|
fetchUsers, fetchUserFieldValues, deactivateUser, deactivateUsers,
|
|
banUser, unbanUser, bulkBanUsers, bulkUnbanUsers, makeAdmin, demoteAdmin,
|
|
} = useUsers();
|
|
|
|
const { usersDashboard, fetchUsersDashboard } = useDashboard();
|
|
|
|
useEffect(() => {
|
|
fetchUsersDashboard();
|
|
}, []);
|
|
|
|
// ─── Keep activeFilters in sync so TableDashboard can highlight active ────
|
|
const handleRefsReady = (refs) => {
|
|
tableRefsRef.current = refs; // ← just store directly, no override needed
|
|
};
|
|
|
|
const exportConfig = {
|
|
allData: users,
|
|
attributes,
|
|
filename: `${getTimestamp()}_Users`,
|
|
sheetName: "Users",
|
|
title: "Users",
|
|
generatedBy: formatGeneratedBy(currentUser),
|
|
};
|
|
|
|
const rowActions = buildRowActions({
|
|
navigate,
|
|
onArchive: (row) => setArchiveTarget(row),
|
|
onBan: (row) => setBanTarget(row),
|
|
onUnban: (row) => setUnbanTarget(row),
|
|
onMakeAdmin: (row) => setMakeAdminTarget(row),
|
|
onDemoteAdmin: (row) => setDemoteAdminTarget(row),
|
|
currentUserId: currentUser?.user_id,
|
|
});
|
|
const toolbarActions = buildToolbarActions({
|
|
fetchUsers, pagination, exportConfig, navigate,
|
|
getFilters: () => tableRefsRef.current.getFilters(),
|
|
getSort: () => tableRefsRef.current.getSort(),
|
|
getTableInstance: () => tableRefsRef.current.tableInstance,
|
|
});
|
|
const selectionActions = buildSelectionActions({
|
|
exportConfig,
|
|
archiveUser: (row) => setArchiveTarget(row),
|
|
archiveUsers: (ids) => setArchiveIds(ids),
|
|
banUsers: (ids) => setBanIds(ids),
|
|
unbanUsers: (ids) => setUnbanIds(ids),
|
|
getTableInstance: () => tableRefsRef.current.tableInstance,
|
|
});
|
|
|
|
const columns = useMemo(() => buildDataColumns(attributes, rowActions), [attributes]);
|
|
|
|
const handleArchiveSuccess = () => {
|
|
setArchiveTarget(null);
|
|
setArchiveIds(null);
|
|
tableRefsRef.current.resetSelection?.();
|
|
fetchUsers({ page: 1, limit: pagination.limit });
|
|
fetchUsersDashboard();
|
|
};
|
|
|
|
const handleBanSuccess = () => {
|
|
setBanTarget(null);
|
|
setBanIds(null);
|
|
tableRefsRef.current.resetSelection?.();
|
|
fetchUsers({ page: 1, limit: pagination.limit });
|
|
};
|
|
|
|
const handleUnbanSuccess = () => {
|
|
setUnbanTarget(null);
|
|
setUnbanIds(null);
|
|
tableRefsRef.current.resetSelection?.();
|
|
fetchUsers({ page: 1, limit: pagination.limit });
|
|
};
|
|
|
|
const handleMakeAdminSuccess = () => {
|
|
setMakeAdminTarget(null);
|
|
fetchUsers({ page: 1, limit: pagination.limit });
|
|
};
|
|
|
|
const handleDemoteAdminSuccess = () => {
|
|
setDemoteAdminTarget(null);
|
|
fetchUsers({ page: 1, limit: pagination.limit });
|
|
};
|
|
|
|
// ─── Attach filterId/filterValue (or onClick) to each stat ────────────────
|
|
const statsWithFilter = (usersDashboard?.stats ?? []).map((s) => ({
|
|
...s,
|
|
filterId: s.key === "active" ? "is_active" : null,
|
|
filterValue: s.key === "active" ? ["true"] : null,
|
|
onClick: s.key === "inactive" ? () => navigate("/admin/users/archived") : undefined,
|
|
}));
|
|
|
|
// ─── Attach filterId to each breakdown so clicking a slice filters ────────
|
|
const dashboardBreakdowns = (usersDashboard?.breakdowns ?? []).map((b) => ({
|
|
...b,
|
|
filterId: b.key === "acc_type" ? "acc_type"
|
|
: b.key === "reg_type" ? "reg_type"
|
|
: null,
|
|
}));
|
|
|
|
return (
|
|
<>
|
|
{/* ── Dashboard strip ── */}
|
|
{usersDashboard && (
|
|
<TableDashboard
|
|
stats={statsWithFilter}
|
|
breakdowns={dashboardBreakdowns}
|
|
statMap={USER_STAT_MAP}
|
|
tableRefsRef={tableRefsRef}
|
|
/>
|
|
)}
|
|
|
|
{/* ── Data table ── */}
|
|
<DataTable
|
|
title="Users"
|
|
data={users}
|
|
columns={columns}
|
|
attributes={attributes}
|
|
pagination={pagination}
|
|
setPagination={setPagination}
|
|
loading={loading}
|
|
onFetch={fetchUsers}
|
|
onFetchFilterData={fetchUserFieldValues}
|
|
onRefsReady={handleRefsReady}
|
|
renderFilterSheet={({ open, onOpenChange, column, attr, data, loading }) => (
|
|
<FilterSheet
|
|
open={open}
|
|
onOpenChange={onOpenChange}
|
|
column={column}
|
|
attr={attr}
|
|
data={data}
|
|
loading={loading}
|
|
/>
|
|
)}
|
|
columnPinning={columnPinning}
|
|
toolbarActions={toolbarActions}
|
|
selectionActions={selectionActions}
|
|
recordLabel="user"
|
|
emptyMessage="No users match the current filters."
|
|
enableRowSelection={(row) => row.original.user_id !== currentUser?.user_id}
|
|
/>
|
|
|
|
{/* Single archive */}
|
|
<ArchiveDialog
|
|
open={!!archiveTarget}
|
|
onOpenChange={(v) => !v && setArchiveTarget(null)}
|
|
entity={archiveTarget}
|
|
entityLabel="User"
|
|
getName={(u) => u?.personal_info?.name?.full_name ?? u?.email}
|
|
onArchive={(u) => deactivateUser(u?.user_id)}
|
|
loading={loading}
|
|
onSuccess={handleArchiveSuccess}
|
|
/>
|
|
|
|
{/* Bulk archive */}
|
|
<ArchiveDialog
|
|
open={!!archiveIds}
|
|
onOpenChange={(v) => !v && setArchiveIds(null)}
|
|
ids={archiveIds ?? []}
|
|
entityLabel="User"
|
|
onArchive={deactivateUsers}
|
|
loading={loading}
|
|
onSuccess={handleArchiveSuccess}
|
|
/>
|
|
|
|
{/* Single ban */}
|
|
<BanUserDialog
|
|
open={!!banTarget}
|
|
onOpenChange={(v) => !v && setBanTarget(null)}
|
|
entity={banTarget}
|
|
entityLabel="User"
|
|
getName={(u) => u?.personal_info?.name?.full_name ?? u?.email}
|
|
onBan={(payload) => banUser(banTarget?.user_id, payload)}
|
|
loading={loading}
|
|
onSuccess={handleBanSuccess}
|
|
/>
|
|
|
|
{/* Bulk ban */}
|
|
<BanUserDialog
|
|
open={!!banIds}
|
|
onOpenChange={(v) => !v && setBanIds(null)}
|
|
ids={banIds ?? []}
|
|
entityLabel="User"
|
|
onBan={(payload) => bulkBanUsers({ ids: banIds, ...payload })}
|
|
loading={loading}
|
|
onSuccess={handleBanSuccess}
|
|
/>
|
|
|
|
{/* Single unban */}
|
|
<UnbanDialog
|
|
open={!!unbanTarget}
|
|
onOpenChange={(v) => !v && setUnbanTarget(null)}
|
|
entity={unbanTarget}
|
|
entityLabel="User"
|
|
getName={(u) => u?.personal_info?.name?.full_name ?? u?.email}
|
|
onUnban={(payload) => unbanUser(unbanTarget?.user_id, payload)}
|
|
loading={loading}
|
|
onSuccess={handleUnbanSuccess}
|
|
/>
|
|
|
|
{/* Bulk unban */}
|
|
<UnbanDialog
|
|
open={!!unbanIds}
|
|
onOpenChange={(v) => !v && setUnbanIds(null)}
|
|
ids={unbanIds ?? []}
|
|
entityLabel="User"
|
|
onUnban={(payload) => bulkUnbanUsers({ ids: unbanIds, ...payload })}
|
|
loading={loading}
|
|
onSuccess={handleUnbanSuccess}
|
|
/>
|
|
|
|
{/* Make admin */}
|
|
<MakeAdminDialog
|
|
open={!!makeAdminTarget}
|
|
onOpenChange={(v) => !v && setMakeAdminTarget(null)}
|
|
entity={makeAdminTarget}
|
|
onMakeAdmin={(u) => makeAdmin(u?.user_id)}
|
|
loading={loading}
|
|
onSuccess={handleMakeAdminSuccess}
|
|
/>
|
|
|
|
{/* Demote admin */}
|
|
<DemoteAdminDialog
|
|
open={!!demoteAdminTarget}
|
|
onOpenChange={(v) => !v && setDemoteAdminTarget(null)}
|
|
entity={demoteAdminTarget}
|
|
onDemoteAdmin={(u) => demoteAdmin(u?.user_id)}
|
|
loading={loading}
|
|
onSuccess={handleDemoteAdminSuccess}
|
|
/>
|
|
</>
|
|
);
|
|
} |