trithuc-mvc-react 3.4.9 → 3.5.0

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.
@@ -1,108 +1,79 @@
1
1
  import { URL_APPLICATION, URL_APPLICATION_API } from "@/constants";
2
2
  import { Download } from "@mui/icons-material";
3
3
  import { Button, IconButton, Tooltip, Typography, useMediaQuery, useTheme } from "@mui/material";
4
+ import { useState } from "react";
4
5
  import { toast } from "react-toastify";
5
6
  import { exportExcel } from "../../api";
7
+ import useBlockNavigation from "./useBlockNavigation";
8
+ import LoadingOverlay from "./LoadingOverlay";
6
9
 
7
10
  const ExportExcelButton = ({ tableName, data, size = "small" }) => {
8
11
  const theme = useTheme();
9
- const isSmallScreen = useMediaQuery(theme.breakpoints.down("sm")); // Xác định màn hình nhỏ
10
- const handleExportExcel = async (tableName, data) => {
11
- const _data = await exportExcel({ tableName, data });
12
- if (_data.status) {
13
- if (URL_APPLICATION_API) {
14
- window.open(URL_APPLICATION + _data.url, "_blank").focus();
12
+ const isSmallScreen = useMediaQuery(theme.breakpoints.down("sm"));
13
+ const [exporting, setExporting] = useState(false);
14
+
15
+ // 🚫 Chặn reload / close tab khi đang export
16
+ useBlockNavigation(exporting);
17
+
18
+ const handleExportExcel = async () => {
19
+ try {
20
+ setExporting(true);
21
+
22
+ const res = await exportExcel({ tableName, data });
23
+
24
+ if (res?.status) {
25
+ const url = URL_APPLICATION_API ? URL_APPLICATION + res.url : res.url;
26
+
27
+ window.open(url, "_blank");
15
28
  } else {
16
- window.open(_data.url, "_blank").focus();
29
+ toast.error("Xuất file thất bại!");
17
30
  }
18
- } else {
19
- toast.error("Xuất file thất bại!");
31
+ } catch (error) {
32
+ toast.error(" lỗi khi xuất Excel!");
33
+ } finally {
34
+ setExporting(false);
20
35
  }
21
36
  };
22
- return !isSmallScreen ? (
23
- <Button
24
- size={size}
25
- variant="outlined"
26
- startIcon={<Download />}
27
- onClick={() => {
28
- handleExportExcel(tableName, data);
29
- }}
30
- sx={{
31
- ml: 0.5,
32
- mr: 0.5
33
- // fontSize: {
34
- // xs: "0.75rem",
35
- // sm: "0.85rem",
36
- // md: "0.95rem"
37
- // },
38
- // textAlign: "center",
39
- // textTransform: "none"
40
- }}
41
- >
42
- <Typography
43
- noWrap
44
- sx={{
45
- width: "100%",
46
- fontSize: {
47
- xs: "0.65rem",
48
- sm: "0.75rem",
49
- md: "0.95rem"
50
- },
51
- textAlign: "center",
52
- textTransform: "none"
53
- }}
54
- >
55
- Excel
56
- </Typography>
57
- </Button>
58
- ) : (
59
- <Tooltip title="Excel">
60
- <IconButton
61
- variant="outlined"
62
- color="primary"
63
- size={size}
64
- onClick={() => {
65
- handleExportExcel(tableName, data);
66
- }}
67
- >
68
- <Download fontSize="inherit" />
69
- </IconButton>
70
- </Tooltip>
71
- // <Button
72
- // size={size}
73
- // variant="outlined"
74
- // startIcon={<Download />}
75
- // onClick={() => {
76
- // handleExportExcel(tableName, data);
77
- // }}
78
- // sx={{
79
- // ml: 0.5,
80
- // mr: 0.5,
81
- // fontSize: {
82
- // xs: "0.75rem",
83
- // sm: "0.875rem",
84
- // md: "1rem"
85
- // },
86
- // textAlign: "center",
87
- // textTransform: "none"
88
- // }}
89
- // >
90
- // <Typography
91
- // noWrap
92
- // sx={{
93
- // width: "100%",
94
- // fontSize: {
95
- // xs: "0.75rem",
96
- // sm: "0.875rem",
97
- // md: "1rem"
98
- // },
99
- // textAlign: "center",
100
- // textTransform: "none" // ✅ Đảm bảo chữ gốc giữ nguyên
101
- // }}
102
- // >
103
- // Excel
104
- // </Typography>
105
- // </Button>
37
+
38
+ return (
39
+ <>
40
+ {/* 🔔 Màn hình chờ API */}
41
+ <LoadingOverlay open={exporting} text="Đang xuất file , vui lòng đợi..." />
42
+
43
+ {!isSmallScreen ? (
44
+ <Button
45
+ size={size}
46
+ variant="outlined"
47
+ startIcon={<Download />}
48
+ disabled={exporting}
49
+ onClick={handleExportExcel}
50
+ sx={{ ml: 0.5, mr: 0.5 }}
51
+ >
52
+ <Typography
53
+ noWrap
54
+ sx={{
55
+ width: "100%",
56
+ fontSize: {
57
+ xs: "0.65rem",
58
+ sm: "0.75rem",
59
+ md: "0.95rem"
60
+ },
61
+ textAlign: "center",
62
+ textTransform: "none"
63
+ }}
64
+ >
65
+ Excel
66
+ </Typography>
67
+ </Button>
68
+ ) : (
69
+ <Tooltip title="Excel">
70
+ <IconButton color="primary" size={size} disabled={exporting} onClick={handleExportExcel}>
71
+ <Download fontSize="inherit" />
72
+ </IconButton>
73
+ </Tooltip>
74
+ )}
75
+ </>
106
76
  );
107
77
  };
78
+
108
79
  export default ExportExcelButton;
@@ -0,0 +1,98 @@
1
+ import { Modal, Box, CircularProgress, Typography } from "@mui/material";
2
+ import { useEffect, useState } from "react";
3
+ import HubOutlinedIcon from "@mui/icons-material/HubOutlined";
4
+
5
+ const LoadingOverlay = ({ open, text }) => {
6
+ const [progress, setProgress] = useState(0);
7
+
8
+ useEffect(() => {
9
+ if (!open) {
10
+ setProgress(0);
11
+ return;
12
+ }
13
+
14
+ const timer = setInterval(() => {
15
+ setProgress((p) => (p < 96 ? p + Math.random() * 3 : p));
16
+ }, 420);
17
+
18
+ return () => clearInterval(timer);
19
+ }, [open]);
20
+
21
+ return (
22
+ <Modal open={open} sx={{ zIndex: 99999 }}>
23
+ <Box
24
+ display="flex"
25
+ alignItems="center"
26
+ justifyContent="center"
27
+ height="100vh"
28
+ sx={{
29
+ backdropFilter: "blur(8px)",
30
+ backgroundColor: "rgba(225,235,245,0.65)"
31
+ }}
32
+ >
33
+ <Box
34
+ sx={{
35
+ width: 320,
36
+ px: 4,
37
+ py: 4,
38
+ borderRadius: 3,
39
+ background: "linear-gradient(180deg, rgba(255,255,255,0.96), rgba(240,247,255,0.96))",
40
+ boxShadow: "0 25px 50px rgba(0,60,120,0.25)",
41
+ border: "1px solid rgba(0,120,200,0.2)",
42
+ color: "#0d2b45",
43
+ textAlign: "center"
44
+ }}
45
+ >
46
+ {/* ICON */}
47
+ <Box
48
+ sx={{
49
+ width: 60,
50
+ height: 60,
51
+ borderRadius: "50%",
52
+ mx: "auto",
53
+ mb: 2.5,
54
+ background: "linear-gradient(135deg, rgba(0,150,255,0.18), rgba(0,200,255,0.28))",
55
+ display: "flex",
56
+ alignItems: "center",
57
+ justifyContent: "center",
58
+ animation: "pulse 2.4s infinite",
59
+ "@keyframes pulse": {
60
+ "0%": { boxShadow: "0 0 0 0 rgba(0,150,255,0.45)" },
61
+ "70%": { boxShadow: "0 0 0 20px rgba(0,150,255,0)" },
62
+ "100%": { boxShadow: "0 0 0 0 rgba(0,150,255,0)" }
63
+ }
64
+ }}
65
+ >
66
+ <HubOutlinedIcon sx={{ fontSize: 30, color: "#0277bd" }} />
67
+ </Box>
68
+
69
+ <Typography fontSize="1rem" fontWeight={600}>
70
+ {text || "Đang khởi tạo dữ liệu hệ thống"}
71
+ </Typography>
72
+
73
+ <Typography fontSize="0.75rem" color="rgba(13,43,69,0.7)" mt={0.6}>
74
+ Nền tảng số đang xử lý và kết nối dữ liệu
75
+ </Typography>
76
+
77
+ <Box mt={3}>
78
+ <CircularProgress
79
+ variant="determinate"
80
+ value={Math.min(progress, 100)}
81
+ size={50}
82
+ thickness={4}
83
+ sx={{
84
+ color: "#0288d1",
85
+ mb: 1
86
+ }}
87
+ />
88
+ <Typography fontSize="0.8rem" fontWeight={500}>
89
+ {Math.floor(progress)}%
90
+ </Typography>
91
+ </Box>
92
+ </Box>
93
+ </Box>
94
+ </Modal>
95
+ );
96
+ };
97
+
98
+ export default LoadingOverlay;
@@ -0,0 +1,23 @@
1
+ import { useEffect } from "react";
2
+
3
+ /**
4
+ * Chặn navigate (reload / close tab) khi shouldBlock = true
5
+ */
6
+ const useBlockNavigation = (shouldBlock) => {
7
+ useEffect(() => {
8
+ if (!shouldBlock) return;
9
+
10
+ const handleBeforeUnload = (event) => {
11
+ event.preventDefault();
12
+ event.returnValue = ""; // Bắt buộc cho Chrome
13
+ };
14
+
15
+ window.addEventListener("beforeunload", handleBeforeUnload);
16
+
17
+ return () => {
18
+ window.removeEventListener("beforeunload", handleBeforeUnload);
19
+ };
20
+ }, [shouldBlock]);
21
+ };
22
+
23
+ export default useBlockNavigation;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trithuc-mvc-react",
3
- "version": "3.4.9",
3
+ "version": "3.5.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"