trithuc-mvc-react 3.1.5 → 3.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -14,6 +14,8 @@ import { usePermission } from "../../hooks";
|
|
|
14
14
|
import { TableRowRender } from "./TableRowRender";
|
|
15
15
|
import TableToolbar from "./TableToolbar";
|
|
16
16
|
import { useDataTable } from "./hooks";
|
|
17
|
+
import DeleteConfirmationDialog from "./DeleteConfirmationDialog";
|
|
18
|
+
import DeleteMultipleConfirmationDialog from "./DeleteMultipleConfirmationDialog";
|
|
17
19
|
const defaultQueryOptions = {
|
|
18
20
|
staleTime: 1000 * 60 * 1, // Thời gian dữ liệu có thể sử dụng lại trước khi gọi lại API
|
|
19
21
|
cacheTime: 1000 * 60 * 30, // Thời gian dữ liệu được lưu trong cache trước khi bị xóa
|
|
@@ -38,9 +40,12 @@ const DataTable = ({ multipleActions = [], page, setPage = () => {}, disableEdit
|
|
|
38
40
|
const { set: setPermission } = usePermission(tableName);
|
|
39
41
|
const queryClient = useQueryClient();
|
|
40
42
|
const confirm = useConfirm();
|
|
43
|
+
const [openDeleteMultipleDialog, setOpenDeleteMultipleDialog] = useState(false);
|
|
41
44
|
const [selectedItems, setSelectedItems] = useState([]);
|
|
42
45
|
// const [page, setPage] = useState(0);
|
|
43
46
|
const [rowsPerPage, setRowsPerPage] = useState(defaultRowsPerPage);
|
|
47
|
+
const [openDialog, setOpenDialog] = useState(false);
|
|
48
|
+
const [deleteId, setDeleteId] = useState(null);
|
|
44
49
|
|
|
45
50
|
const { data, isLoading } = useQuery({
|
|
46
51
|
queryKey: [tableName, page, rowsPerPage, dataSearch],
|
|
@@ -131,22 +136,46 @@ const DataTable = ({ multipleActions = [], page, setPage = () => {}, disableEdit
|
|
|
131
136
|
}
|
|
132
137
|
}
|
|
133
138
|
});
|
|
134
|
-
|
|
139
|
+
// Hàm gọi khi người dùng muốn xóa một bản ghi
|
|
135
140
|
const handleDelete = (id) => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
141
|
+
// console.log("id", id);
|
|
142
|
+
setDeleteId(id); // Lưu lại ID bản ghi muốn xóa
|
|
143
|
+
setOpenDialog(true); // Mở dialog xác nhận xóa
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// Hàm xử lý khi đóng dialog (cả xác nhận và hủy)
|
|
147
|
+
const handleDialogClose = (confirm) => {
|
|
148
|
+
if (confirm) {
|
|
149
|
+
// console.log("Đã xác nhận xóa", deleteId);
|
|
150
|
+
// Gọi API hoặc mutation để xóa
|
|
151
|
+
deleteMutation.mutate({
|
|
152
|
+
id: deleteId,
|
|
153
|
+
tableName
|
|
154
|
+
});
|
|
155
|
+
} else {
|
|
156
|
+
toast.info("Đã hủy thao tác xóa.");
|
|
157
|
+
}
|
|
158
|
+
setOpenDialog(false); // Đóng dialog sau khi chọn
|
|
149
159
|
};
|
|
160
|
+
// const handleDelete = (id) => {
|
|
161
|
+
// confirm({
|
|
162
|
+
// description: "Bạn có chắc chắn muốn xóa bản ghi này không?",
|
|
163
|
+
// title: "Xác nhận",
|
|
164
|
+
// cancellationText: "Hủy",
|
|
165
|
+
// confirmationText: "Xóa"
|
|
166
|
+
// })
|
|
167
|
+
// .then(() => {
|
|
168
|
+
// deleteMutation.mutate({
|
|
169
|
+
// id,
|
|
170
|
+
// tableName
|
|
171
|
+
// });
|
|
172
|
+
// })
|
|
173
|
+
// .finally(() => {
|
|
174
|
+
// // Đảm bảo phần hủy thao tác được xử lý trong finally
|
|
175
|
+
// toast.info("Đã hủy thao tác xóa.");
|
|
176
|
+
// });
|
|
177
|
+
// };
|
|
178
|
+
|
|
150
179
|
const handleChangeStatus = (Id) => {
|
|
151
180
|
changeStatusMutation.mutate({
|
|
152
181
|
tableName,
|
|
@@ -218,21 +247,42 @@ const DataTable = ({ multipleActions = [], page, setPage = () => {}, disableEdit
|
|
|
218
247
|
setRowsPerPage(newRowsPerPage);
|
|
219
248
|
setPage(0);
|
|
220
249
|
};
|
|
250
|
+
// Hàm gọi khi người dùng muốn xóa nhiều bản ghi
|
|
221
251
|
const handleDeleteMultiple = () => {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
252
|
+
setOpenDeleteMultipleDialog(true); // Mở Dialog để xác nhận xóa
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const handleDeleteMultipleDialogClose = (confirm) => {
|
|
256
|
+
if (confirm) {
|
|
257
|
+
// console.log("Đã xác nhận xóa các bản ghi", selectedItems);
|
|
258
|
+
// Gọi API hoặc mutation để xóa các bản ghi
|
|
259
|
+
deleteMultipleMutation.mutate({
|
|
260
|
+
tableName,
|
|
261
|
+
ids: selectedItems
|
|
262
|
+
});
|
|
263
|
+
// toast.success("Đã xóa các bản ghi.");
|
|
264
|
+
} else {
|
|
265
|
+
toast.info("Đã hủy thao tác xóa.");
|
|
266
|
+
}
|
|
267
|
+
setOpenDeleteMultipleDialog(false); // Đóng Dialog sau khi chọn
|
|
235
268
|
};
|
|
269
|
+
// const handleDeleteMultiple = () => {
|
|
270
|
+
// confirm({
|
|
271
|
+
// description: `Bạn có chắc chắn muốn xóa ${selectedItems?.length} bản ghi này không?`,
|
|
272
|
+
// title: "Xác nhận",
|
|
273
|
+
// cancellationText: "Hủy",
|
|
274
|
+
// confirmationText: "Đồng ý"
|
|
275
|
+
// })
|
|
276
|
+
// .then(() => {
|
|
277
|
+
// deleteMultipleMutation.mutate({
|
|
278
|
+
// tableName,
|
|
279
|
+
// ids: selectedItems
|
|
280
|
+
// });
|
|
281
|
+
// })
|
|
282
|
+
// .catch(() => {
|
|
283
|
+
// toast.info("Đã hủy thao tác xóa.");
|
|
284
|
+
// });
|
|
285
|
+
// };
|
|
236
286
|
const theme = useTheme();
|
|
237
287
|
const downXL = useMediaQuery(theme.breakpoints.down("xl"));
|
|
238
288
|
|
|
@@ -310,6 +360,13 @@ const DataTable = ({ multipleActions = [], page, setPage = () => {}, disableEdit
|
|
|
310
360
|
/>
|
|
311
361
|
</Grid>
|
|
312
362
|
</Grid>
|
|
363
|
+
<DeleteConfirmationDialog open={openDialog} onClose={handleDialogClose} onConfirm={handleDialogClose} id={deleteId} />
|
|
364
|
+
<DeleteMultipleConfirmationDialog
|
|
365
|
+
open={openDeleteMultipleDialog}
|
|
366
|
+
onClose={handleDeleteMultipleDialogClose}
|
|
367
|
+
onConfirm={handleDeleteMultipleDialogClose}
|
|
368
|
+
selectedItems={selectedItems}
|
|
369
|
+
/>
|
|
313
370
|
</>
|
|
314
371
|
);
|
|
315
372
|
};
|
|
@@ -13,6 +13,7 @@ import { usePermission } from "../../hooks";
|
|
|
13
13
|
import { TableRowRenderSM } from "./TableRowRenderSM";
|
|
14
14
|
import TableToolbar from "./TableToolbar";
|
|
15
15
|
import { useDataTable } from "./hooks";
|
|
16
|
+
import DeleteConfirmationDialog from "./DeleteConfirmationDialog";
|
|
16
17
|
const defaultQueryOptions = {
|
|
17
18
|
staleTime: 1000 * 60 * 1, // Thời gian dữ liệu có thể sử dụng lại trước khi gọi lại API
|
|
18
19
|
cacheTime: 1000 * 60 * 30, // Thời gian dữ liệu được lưu trong cache trước khi bị xóa
|
|
@@ -37,9 +38,12 @@ const DataTableSM = ({ multipleActions = [], page, setPage = () => {}, disableEd
|
|
|
37
38
|
const { set: setPermission } = usePermission(tableName);
|
|
38
39
|
const queryClient = useQueryClient();
|
|
39
40
|
const confirm = useConfirm();
|
|
41
|
+
const [openDeleteMultipleDialog, setOpenDeleteMultipleDialog] = useState(false);
|
|
40
42
|
const [selectedItems, setSelectedItems] = useState([]);
|
|
41
43
|
// const [page, setPage] = useState(0);
|
|
42
44
|
const [rowsPerPage, setRowsPerPage] = useState(defaultRowsPerPage);
|
|
45
|
+
const [openDialog, setOpenDialog] = useState(false);
|
|
46
|
+
const [deleteId, setDeleteId] = useState(null);
|
|
43
47
|
|
|
44
48
|
const { data, isLoading } = useQuery({
|
|
45
49
|
queryKey: [tableName, page, rowsPerPage, dataSearch],
|
|
@@ -142,22 +146,45 @@ const DataTableSM = ({ multipleActions = [], page, setPage = () => {}, disableEd
|
|
|
142
146
|
}
|
|
143
147
|
}
|
|
144
148
|
});
|
|
145
|
-
|
|
149
|
+
// Hàm gọi khi người dùng muốn xóa một bản ghi
|
|
146
150
|
const handleDelete = (id) => {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
151
|
+
// console.log("id", id);
|
|
152
|
+
setDeleteId(id); // Lưu lại ID bản ghi muốn xóa
|
|
153
|
+
setOpenDialog(true); // Mở dialog xác nhận xóa
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// Hàm xử lý khi đóng dialog (cả xác nhận và hủy)
|
|
157
|
+
const handleDialogClose = (confirm) => {
|
|
158
|
+
if (confirm) {
|
|
159
|
+
// console.log("Đã xác nhận xóa", deleteId);
|
|
160
|
+
// Gọi API hoặc mutation để xóa
|
|
161
|
+
deleteMutation.mutate({
|
|
162
|
+
id: deleteId,
|
|
163
|
+
tableName
|
|
164
|
+
});
|
|
165
|
+
} else {
|
|
166
|
+
toast.info("Đã hủy thao tác xóa.");
|
|
167
|
+
}
|
|
168
|
+
setOpenDialog(false); // Đóng dialog sau khi chọn
|
|
160
169
|
};
|
|
170
|
+
// const handleDelete = (id) => {
|
|
171
|
+
// confirm({
|
|
172
|
+
// description: "Bạn có chắc chắn muốn xóa bản ghi này không?",
|
|
173
|
+
// title: "Xác nhận",
|
|
174
|
+
// cancellationText: "Hủy",
|
|
175
|
+
// confirmationText: "Xóa"
|
|
176
|
+
// })
|
|
177
|
+
// .then(() => {
|
|
178
|
+
// deleteMutation.mutate({
|
|
179
|
+
// id,
|
|
180
|
+
// tableName
|
|
181
|
+
// });
|
|
182
|
+
// })
|
|
183
|
+
// .finally(() => {
|
|
184
|
+
// // Đảm bảo phần hủy thao tác được xử lý trong finally
|
|
185
|
+
// toast.info("Đã hủy thao tác xóa.");
|
|
186
|
+
// });
|
|
187
|
+
// };
|
|
161
188
|
const handleChangeStatus = (Id) => {
|
|
162
189
|
changeStatusMutation.mutate({
|
|
163
190
|
tableName,
|
|
@@ -229,21 +256,42 @@ const DataTableSM = ({ multipleActions = [], page, setPage = () => {}, disableEd
|
|
|
229
256
|
setRowsPerPage(newRowsPerPage);
|
|
230
257
|
setPage(0);
|
|
231
258
|
};
|
|
259
|
+
// Hàm gọi khi người dùng muốn xóa nhiều bản ghi
|
|
232
260
|
const handleDeleteMultiple = () => {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
261
|
+
setOpenDeleteMultipleDialog(true); // Mở Dialog để xác nhận xóa
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const handleDeleteMultipleDialogClose = (confirm) => {
|
|
265
|
+
if (confirm) {
|
|
266
|
+
// console.log("Đã xác nhận xóa các bản ghi", selectedItems);
|
|
267
|
+
// Gọi API hoặc mutation để xóa các bản ghi
|
|
268
|
+
deleteMultipleMutation.mutate({
|
|
269
|
+
tableName,
|
|
270
|
+
ids: selectedItems
|
|
271
|
+
});
|
|
272
|
+
// toast.success("Đã xóa các bản ghi.");
|
|
273
|
+
} else {
|
|
274
|
+
toast.info("Đã hủy thao tác xóa.");
|
|
275
|
+
}
|
|
276
|
+
setOpenDeleteMultipleDialog(false); // Đóng Dialog sau khi chọn
|
|
246
277
|
};
|
|
278
|
+
// const handleDeleteMultiple = () => {
|
|
279
|
+
// confirm({
|
|
280
|
+
// description: `Bạn có chắc chắn muốn xóa ${selectedItems?.length} bản ghi này không?`,
|
|
281
|
+
// title: "Xác nhận",
|
|
282
|
+
// cancellationText: "Hủy",
|
|
283
|
+
// confirmationText: "Đồng ý"
|
|
284
|
+
// })
|
|
285
|
+
// .then(() => {
|
|
286
|
+
// deleteMultipleMutation.mutate({
|
|
287
|
+
// tableName,
|
|
288
|
+
// ids: selectedItems
|
|
289
|
+
// });
|
|
290
|
+
// })
|
|
291
|
+
// .catch(() => {
|
|
292
|
+
// toast.info("Đã hủy thao tác xóa.");
|
|
293
|
+
// });
|
|
294
|
+
// };
|
|
247
295
|
const theme = useTheme();
|
|
248
296
|
const downXL = useMediaQuery(theme.breakpoints.down("xl"));
|
|
249
297
|
|
|
@@ -321,6 +369,13 @@ const DataTableSM = ({ multipleActions = [], page, setPage = () => {}, disableEd
|
|
|
321
369
|
/>
|
|
322
370
|
</Grid>
|
|
323
371
|
</Grid>
|
|
372
|
+
<DeleteConfirmationDialog open={openDialog} onClose={handleDialogClose} onConfirm={handleDialogClose} id={deleteId} />
|
|
373
|
+
<DeleteMultipleConfirmationDialog
|
|
374
|
+
open={openDeleteMultipleDialog}
|
|
375
|
+
onClose={handleDeleteMultipleDialogClose}
|
|
376
|
+
onConfirm={handleDeleteMultipleDialogClose}
|
|
377
|
+
selectedItems={selectedItems}
|
|
378
|
+
/>
|
|
324
379
|
</>
|
|
325
380
|
);
|
|
326
381
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Dialog, DialogActions, DialogContent, DialogTitle, Button } from "@mui/material";
|
|
3
|
+
import { toast } from "react-toastify"; // Nếu bạn đang sử dụng react-toastify
|
|
4
|
+
|
|
5
|
+
const DeleteConfirmationDialog = ({ open, onClose, onConfirm, id }) => {
|
|
6
|
+
return (
|
|
7
|
+
<Dialog open={open} onClose={() => onClose(false)}>
|
|
8
|
+
<DialogTitle>Xác nhận xóa</DialogTitle>
|
|
9
|
+
<DialogContent>
|
|
10
|
+
<p>Bạn có chắc chắn muốn xóa bản ghi này không?</p>
|
|
11
|
+
</DialogContent>
|
|
12
|
+
<DialogActions>
|
|
13
|
+
<Button onClick={() => onClose(false)} color="primary">
|
|
14
|
+
Hủy
|
|
15
|
+
</Button>
|
|
16
|
+
<Button onClick={() => onConfirm(true)} color="primary">
|
|
17
|
+
Xóa
|
|
18
|
+
</Button>
|
|
19
|
+
</DialogActions>
|
|
20
|
+
</Dialog>
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
export default DeleteConfirmationDialog;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { Dialog, DialogActions, DialogContent, DialogTitle, Button } from "@mui/material";
|
|
3
|
+
import { toast } from "react-toastify"; // Nếu bạn đang sử dụng react-toastify
|
|
4
|
+
|
|
5
|
+
const DeleteMultipleConfirmationDialog = ({ open, onClose, onConfirm, selectedItems }) => {
|
|
6
|
+
return (
|
|
7
|
+
<Dialog open={open} onClose={onClose}>
|
|
8
|
+
<DialogTitle>Xác nhận xóa</DialogTitle>
|
|
9
|
+
<DialogContent>
|
|
10
|
+
<p>Bạn có chắc chắn muốn xóa {selectedItems?.length} bản ghi này không?</p>
|
|
11
|
+
</DialogContent>
|
|
12
|
+
<DialogActions>
|
|
13
|
+
<Button onClick={() => onClose(false)} color="primary">
|
|
14
|
+
Hủy
|
|
15
|
+
</Button>
|
|
16
|
+
<Button onClick={() => onConfirm(selectedItems)} color="primary">
|
|
17
|
+
Xóa
|
|
18
|
+
</Button>
|
|
19
|
+
</DialogActions>
|
|
20
|
+
</Dialog>
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
export default DeleteMultipleConfirmationDialog;
|