trithuc-mvc-react 3.1.5 → 3.1.7

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
- confirm({
137
- description: "Bạn chắc chắn muốn xóa bản ghi này không?",
138
- title: "Xác nhận",
139
- cancellationText: "Hủy",
140
- confirmationText: "Xóa"
141
- })
142
- .then(() => {
143
- deleteMutation.mutate({
144
- id,
145
- tableName
146
- });
147
- })
148
- .catch(() => {});
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
- confirm({
223
- description: `Bạn có chắc chắn muốn xóa ${selectedItems?.length} bản ghi này không?`,
224
- title: "Xác nhận",
225
- cancellationText: "Hủy",
226
- confirmationText: "Đồng ý"
227
- })
228
- .then(() => {
229
- deleteMultipleMutation.mutate({
230
- tableName,
231
- ids: selectedItems
232
- });
233
- })
234
- .catch(() => {});
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,8 @@ 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";
17
+ import DeleteMultipleConfirmationDialog from "./DeleteMultipleConfirmationDialog";
16
18
  const defaultQueryOptions = {
17
19
  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
20
  cacheTime: 1000 * 60 * 30, // Thời gian dữ liệu được lưu trong cache trước khi bị xóa
@@ -37,9 +39,12 @@ const DataTableSM = ({ multipleActions = [], page, setPage = () => {}, disableEd
37
39
  const { set: setPermission } = usePermission(tableName);
38
40
  const queryClient = useQueryClient();
39
41
  const confirm = useConfirm();
42
+ const [openDeleteMultipleDialog, setOpenDeleteMultipleDialog] = useState(false);
40
43
  const [selectedItems, setSelectedItems] = useState([]);
41
44
  // const [page, setPage] = useState(0);
42
45
  const [rowsPerPage, setRowsPerPage] = useState(defaultRowsPerPage);
46
+ const [openDialog, setOpenDialog] = useState(false);
47
+ const [deleteId, setDeleteId] = useState(null);
43
48
 
44
49
  const { data, isLoading } = useQuery({
45
50
  queryKey: [tableName, page, rowsPerPage, dataSearch],
@@ -142,22 +147,45 @@ const DataTableSM = ({ multipleActions = [], page, setPage = () => {}, disableEd
142
147
  }
143
148
  }
144
149
  });
145
-
150
+ // Hàm gọi khi người dùng muốn xóa một bản ghi
146
151
  const handleDelete = (id) => {
147
- confirm({
148
- description: "Bạn chắc chắn muốn xóa bản ghi này không?",
149
- title: "Xác nhận",
150
- cancellationText: "Hủy",
151
- confirmationText: "Xóa"
152
- })
153
- .then(() => {
154
- deleteMutation.mutate({
155
- id,
156
- tableName
157
- });
158
- })
159
- .catch(() => {});
152
+ // console.log("id", id);
153
+ setDeleteId(id); // Lưu lại ID bản ghi muốn xóa
154
+ setOpenDialog(true); // Mở dialog xác nhận xóa
155
+ };
156
+
157
+ // Hàm xử lý khi đóng dialog (cả xác nhận và hủy)
158
+ const handleDialogClose = (confirm) => {
159
+ if (confirm) {
160
+ // console.log("Đã xác nhận xóa", deleteId);
161
+ // Gọi API hoặc mutation để xóa
162
+ deleteMutation.mutate({
163
+ id: deleteId,
164
+ tableName
165
+ });
166
+ } else {
167
+ toast.info("Đã hủy thao tác xóa.");
168
+ }
169
+ setOpenDialog(false); // Đóng dialog sau khi chọn
160
170
  };
171
+ // const handleDelete = (id) => {
172
+ // confirm({
173
+ // description: "Bạn có chắc chắn muốn xóa bản ghi này không?",
174
+ // title: "Xác nhận",
175
+ // cancellationText: "Hủy",
176
+ // confirmationText: "Xóa"
177
+ // })
178
+ // .then(() => {
179
+ // deleteMutation.mutate({
180
+ // id,
181
+ // tableName
182
+ // });
183
+ // })
184
+ // .finally(() => {
185
+ // // Đảm bảo phần hủy thao tác được xử lý trong finally
186
+ // toast.info("Đã hủy thao tác xóa.");
187
+ // });
188
+ // };
161
189
  const handleChangeStatus = (Id) => {
162
190
  changeStatusMutation.mutate({
163
191
  tableName,
@@ -229,21 +257,42 @@ const DataTableSM = ({ multipleActions = [], page, setPage = () => {}, disableEd
229
257
  setRowsPerPage(newRowsPerPage);
230
258
  setPage(0);
231
259
  };
260
+ // Hàm gọi khi người dùng muốn xóa nhiều bản ghi
232
261
  const handleDeleteMultiple = () => {
233
- confirm({
234
- description: `Bạn có chắc chắn muốn xóa ${selectedItems?.length} bản ghi này không?`,
235
- title: "Xác nhận",
236
- cancellationText: "Hủy",
237
- confirmationText: "Đồng ý"
238
- })
239
- .then(() => {
240
- deleteMultipleMutation.mutate({
241
- tableName,
242
- ids: selectedItems
243
- });
244
- })
245
- .catch(() => {});
262
+ setOpenDeleteMultipleDialog(true); // Mở Dialog để xác nhận xóa
263
+ };
264
+
265
+ const handleDeleteMultipleDialogClose = (confirm) => {
266
+ if (confirm) {
267
+ // console.log("Đã xác nhận xóa các bản ghi", selectedItems);
268
+ // Gọi API hoặc mutation để xóa các bản ghi
269
+ deleteMultipleMutation.mutate({
270
+ tableName,
271
+ ids: selectedItems
272
+ });
273
+ // toast.success("Đã xóa các bản ghi.");
274
+ } else {
275
+ toast.info("Đã hủy thao tác xóa.");
276
+ }
277
+ setOpenDeleteMultipleDialog(false); // Đóng Dialog sau khi chọn
246
278
  };
279
+ // const handleDeleteMultiple = () => {
280
+ // confirm({
281
+ // description: `Bạn có chắc chắn muốn xóa ${selectedItems?.length} bản ghi này không?`,
282
+ // title: "Xác nhận",
283
+ // cancellationText: "Hủy",
284
+ // confirmationText: "Đồng ý"
285
+ // })
286
+ // .then(() => {
287
+ // deleteMultipleMutation.mutate({
288
+ // tableName,
289
+ // ids: selectedItems
290
+ // });
291
+ // })
292
+ // .catch(() => {
293
+ // toast.info("Đã hủy thao tác xóa.");
294
+ // });
295
+ // };
247
296
  const theme = useTheme();
248
297
  const downXL = useMediaQuery(theme.breakpoints.down("xl"));
249
298
 
@@ -321,6 +370,13 @@ const DataTableSM = ({ multipleActions = [], page, setPage = () => {}, disableEd
321
370
  />
322
371
  </Grid>
323
372
  </Grid>
373
+ <DeleteConfirmationDialog open={openDialog} onClose={handleDialogClose} onConfirm={handleDialogClose} id={deleteId} />
374
+ <DeleteMultipleConfirmationDialog
375
+ open={openDeleteMultipleDialog}
376
+ onClose={handleDeleteMultipleDialogClose}
377
+ onConfirm={handleDeleteMultipleDialogClose}
378
+ selectedItems={selectedItems}
379
+ />
324
380
  </>
325
381
  );
326
382
  };
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trithuc-mvc-react",
3
- "version": "3.1.5",
3
+ "version": "3.1.7",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"