-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTableContainer.tsx
More file actions
168 lines (147 loc) · 4.51 KB
/
Copy pathTableContainer.tsx
File metadata and controls
168 lines (147 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { Suspense, useState } from 'react';
import CircularProgress from '@mui/material/CircularProgress';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import MuiTableContainer from '@mui/material/TableContainer';
import TablePagination from '@mui/material/TablePagination';
import TableRow from '@mui/material/TableRow';
import { invalidateQuery, useMutation } from '@blitzjs/rpc';
import TableCore from './TableCore';
import TableHead from './TableHead';
import { useTableProps } from './TablePropsProvider';
import TableToolbar from './TableToolbar';
type TableProps = {
title: string;
columns: any[];
defaultSort?: { name: string; order: 'asc' | 'desc' };
snackbar: any;
queryKey: string;
getQuery: any;
queryArgs?: any;
deleteQuery: any;
allowCopy?: boolean;
onExport?: (rowData: any) => void;
FormComponent?: (props: unknown) => JSX.Element;
actions?: any[];
globalActions?: any[];
onAdd: () => void;
onEdit?: (values) => void;
};
export default function TableContainer(props: TableProps) {
const {
title,
columns,
snackbar,
getQuery,
queryKey,
deleteQuery,
allowCopy,
onExport,
FormComponent,
actions = [],
globalActions,
onAdd,
onEdit
} = props;
const [data, setData] = useState({});
const rows = data[queryKey] ?? [];
const count = data['count'] ?? 0;
const { page, order, orderBy, rowsPerPage } = useTableProps();
const colSpan = columns.length + (actions?.length ?? 0) + Number(Boolean(allowCopy)) + Number(Boolean(onEdit)) + 1;
const [deleteMutation] = useMutation(deleteQuery);
const [selected, setSelected] = useState<string[]>([]);
const onSuccess = (data) => {
setData(data);
};
const onPageChange = (_, newPage) => page.set(newPage);
const handleCustomAction = (onClick) => async (e) => {
e.stopPropagation();
await onClick();
setSelected([]);
};
const handleDeleteAllClick = async () => {
try {
await deleteMutation({ where: { id: { in: selected } } } as any);
setSelected([]);
snackbar.onShow('success', 'Supprimé(s)');
await invalidateQuery(getQuery);
} catch (err) {
snackbar.onShow('error', err.message);
}
};
const handleExportAllClick = onExport
? async () => selected.forEach((x) => onExport(rows.find((r: any) => r.id === x)))
: undefined;
const handleRequestSort = (_, property) => {
const isAsc = orderBy.value === property && order.value === 'asc';
orderBy.set(property);
order.set(isAsc ? 'desc' : 'asc');
};
const handleSelectAllClick = (event) => {
if (event.target.checked) {
const newSelecteds = rows.map((n: any) => n.id);
setSelected(newSelecteds);
return;
}
setSelected([]);
};
const FallbackComponent = (
<>
<TableRow style={{ height: 81 }}>
<TableCell className="text-center" colSpan={colSpan}>
<CircularProgress size={25} color="inherit" />
</TableCell>
</TableRow>
{[...Array(rowsPerPage - 1).keys()].map((x) => (
<TableRow key={x} style={{ height: 81 }}>
<TableCell colSpan={colSpan} />
</TableRow>
))}
</>
);
return (
<>
<TableToolbar
title={title}
numSelected={selected.length}
onAdd={FormComponent ? onAdd : undefined}
onDelete={handleDeleteAllClick}
onExport={handleExportAllClick}
actions={globalActions}
/>
<MuiTableContainer className="text-center">
<Table aria-labelledby="tableTitle" aria-label="Table de données">
<TableHead
numSelected={selected.length}
order={order.value}
orderBy={orderBy.value}
onSelectAllClick={handleSelectAllClick}
onRequestSort={handleRequestSort}
rowCount={count}
{...props}
/>
<TableBody>
<Suspense fallback={FallbackComponent}>
<TableCore
rows={rows}
selected={{ value: selected, set: setSelected }}
handleCustomAction={handleCustomAction}
onSuccess={onSuccess}
{...props}
/>
</Suspense>
</TableBody>
</Table>
</MuiTableContainer>
<TablePagination
rowsPerPageOptions={[]}
component="div"
count={count}
rowsPerPage={rowsPerPage}
page={page.value}
onPageChange={onPageChange}
/>
</>
);
}