git-subtree-dir: apps/web git-subtree-mainline:eaa6d2276agit-subtree-split:459af9d46a
177 lines
5.7 KiB
React
177 lines
5.7 KiB
React
import { useMemo, useRef, useState, useCallback } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
import { useLibrary } from "@/contexts/AdminLibraryContext";
|
|
import { useAuth } from "@/contexts/AuthContext";
|
|
|
|
import DataTable from "@/components/generic/Table/DataTable";
|
|
import { FilterSheet } from "@/components/generic/Sheet/FilterSheet";
|
|
import { RestoreDialog } from "@/components/generic/Dialogs/RestoreDialog";
|
|
import { PermanentDeleteDialog } from "@/components/generic/Dialogs/PermanentDeleteDialog";
|
|
|
|
import { buildDataColumns, columnPinning } from "../../config/library/lessons/columns.config";
|
|
import { buildArchivedToolbarActions } from "../../config/library/lessons/toolbar.config";
|
|
import { buildArchivedSelectionActions } from "../../config/library/lessons/selection.config";
|
|
import { buildArchivedRowActions } from "../../config/library/lessons/rowActions.config";
|
|
|
|
import { getTimestamp } from "@/utils/timestamp.util";
|
|
import { formatGeneratedBy } from "@/utils/generatedBy.util";
|
|
|
|
export default function ArchivedLessonLibraryTable() {
|
|
const [restoreTarget, setRestoreTarget] = useState(null);
|
|
const [restoreIds, setRestoreIds] = useState(null);
|
|
const [deleteTarget, setDeleteTarget] = useState(null);
|
|
const [deleteIds, setDeleteIds] = useState(null);
|
|
|
|
const tableRefsRef = useRef({
|
|
getFilters: () => [],
|
|
getSort: () => [],
|
|
resetSelection: () => { },
|
|
setFilters: () => { },
|
|
});
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const { user: currentUser } = useAuth();
|
|
|
|
const {
|
|
lessons, attributes, pagination, setPagination, loading,
|
|
fetchArchivedLessons, restoreLesson, restoreLessons,
|
|
permanentlyDeleteLesson, permanentlyDeleteLessons,
|
|
fetchLessonFieldValues, fetchLessonPermanentDeleteImpact,
|
|
} = useLibrary();
|
|
|
|
const handleRefsReady = (refs) => {
|
|
tableRefsRef.current = refs;
|
|
};
|
|
|
|
const exportConfig = {
|
|
allData: lessons,
|
|
attributes,
|
|
filename: `${getTimestamp()}_ArchivedLessonLibrary`,
|
|
sheetName: "Archived Lessons",
|
|
generatedBy: formatGeneratedBy(currentUser),
|
|
};
|
|
|
|
const handleFetch = useCallback((params) => fetchArchivedLessons(params), [fetchArchivedLessons]);
|
|
|
|
const rowActions = useMemo(() => buildArchivedRowActions({
|
|
onRestore: (row) => setRestoreTarget(row),
|
|
onDelete: (row) => setDeleteTarget(row),
|
|
}), []);
|
|
|
|
const toolbarActions = buildArchivedToolbarActions({
|
|
fetchArchivedLessons,
|
|
pagination,
|
|
exportConfig,
|
|
navigate,
|
|
getFilters: () => tableRefsRef.current.getFilters(),
|
|
getSort: () => tableRefsRef.current.getSort(),
|
|
getTableInstance: () => tableRefsRef.current.tableInstance,
|
|
});
|
|
|
|
const selectionActions = buildArchivedSelectionActions({
|
|
exportConfig,
|
|
onRestore: (row) => setRestoreTarget(row),
|
|
onRestoreMany: (ids) => setRestoreIds(ids),
|
|
onDelete: (row) => setDeleteTarget(row),
|
|
onDeleteMany: (ids) => setDeleteIds(ids),
|
|
getTableInstance: () => tableRefsRef.current.tableInstance,
|
|
});
|
|
|
|
const columns = useMemo(
|
|
() => buildDataColumns(attributes, rowActions),
|
|
[attributes, rowActions]
|
|
);
|
|
|
|
const refresh = () => {
|
|
setRestoreTarget(null);
|
|
setRestoreIds(null);
|
|
setDeleteTarget(null);
|
|
setDeleteIds(null);
|
|
tableRefsRef.current.resetSelection?.();
|
|
fetchArchivedLessons({ page: 1, limit: pagination?.limit ?? 10 });
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<DataTable
|
|
title="Archived Lessons"
|
|
data={lessons}
|
|
columns={columns}
|
|
attributes={attributes}
|
|
pagination={pagination}
|
|
setPagination={setPagination}
|
|
loading={loading}
|
|
onFetch={handleFetch}
|
|
onFetchFilterData={(field) => fetchLessonFieldValues(field)}
|
|
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="lesson"
|
|
emptyMessage="No archived lessons."
|
|
/>
|
|
|
|
{/* ── Restore ── */}
|
|
<RestoreDialog
|
|
open={!!restoreTarget}
|
|
onOpenChange={(v) => !v && setRestoreTarget(null)}
|
|
entity={restoreTarget}
|
|
entityLabel="Lesson"
|
|
getName={(l) => l?.title}
|
|
onRestore={(l) => restoreLesson(l?.lesson_id)}
|
|
loading={loading}
|
|
onSuccess={refresh}
|
|
/>
|
|
<RestoreDialog
|
|
open={!!restoreIds}
|
|
onOpenChange={(v) => !v && setRestoreIds(null)}
|
|
ids={restoreIds ?? []}
|
|
entityLabel="Lesson"
|
|
onRestore={(ids) => restoreLessons(ids)}
|
|
loading={loading}
|
|
onSuccess={refresh}
|
|
/>
|
|
|
|
{/* ── Permanent delete ── */}
|
|
<PermanentDeleteDialog
|
|
open={!!deleteTarget}
|
|
onOpenChange={(v) => !v && setDeleteTarget(null)}
|
|
entity={deleteTarget}
|
|
entityLabel="Lesson"
|
|
getName={(l) => l?.title}
|
|
onDelete={(l) => permanentlyDeleteLesson(l?.lesson_id)}
|
|
loading={loading}
|
|
onSuccess={refresh}
|
|
onImpactCheck={async () => {
|
|
const impact = await fetchLessonPermanentDeleteImpact(deleteTarget?.lesson_id);
|
|
const { unitCount } = impact?.data ?? impact ?? {};
|
|
return [
|
|
{ label: "unit attachment(s) will be removed", count: unitCount ?? 0 },
|
|
];
|
|
}}
|
|
/>
|
|
<PermanentDeleteDialog
|
|
open={!!deleteIds}
|
|
onOpenChange={(v) => !v && setDeleteIds(null)}
|
|
ids={deleteIds ?? []}
|
|
entityLabel="Lesson"
|
|
onDelete={(ids) => permanentlyDeleteLessons(ids)}
|
|
loading={loading}
|
|
onSuccess={refresh}
|
|
/>
|
|
</>
|
|
);
|
|
}
|