Skip to content

Commit aa30dc9

Browse files
committed
remove redundant download table dialog, refactor, use snackbar to show download errors
1 parent 220cb83 commit aa30dc9

File tree

4 files changed

+235
-251
lines changed

4 files changed

+235
-251
lines changed

hwproj.front/src/components/Courses/StatsMenu.tsx

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
MenuItem,
66
ListItemIcon,
77
ListItemText,
8-
Dialog,
9-
DialogTitle,
108
} from "@mui/material";
119
import { NestedMenuItem } from "mui-nested-menu";
1210
import { Download, ShowChart } from "@mui/icons-material";
@@ -40,26 +38,27 @@ interface StatsMenuState {
4038
}
4139

4240
const StatsMenu: FC<StatsMenuProps> = props => {
41+
const {courseId, userId, yandexCode, onActionOpening, onActionClosing} = props
42+
4343
const [menuState, setMenuState] = useState<StatsMenuState>({
4444
anchorEl: null,
45-
saveStatsAction: props.yandexCode !== null ? SaveStatsAction.ShareWithYandex : null,
45+
saveStatsAction: yandexCode !== null ? SaveStatsAction.ShareWithYandex : null,
4646
})
4747

4848
const {anchorEl, saveStatsAction} = menuState
4949
const showMenu = anchorEl !== null
50+
const openAction = saveStatsAction !== null
5051

5152
useEffect(() => {
5253
if (saveStatsAction !== null)
53-
props.onActionOpening()
54+
onActionOpening()
5455
else
55-
props.onActionClosing()
56+
onActionClosing()
5657
}, [saveStatsAction]);
5758

5859
const navigate = useNavigate();
5960

60-
const goToCharts = () => {
61-
navigate(`/statistics/${props.courseId}/charts`)
62-
}
61+
const goToCharts = () => navigate(`/statistics/${courseId}/charts`)
6362

6463
const handleOpen = (event: React.MouseEvent<HTMLElement>) =>
6564
setMenuState ({
@@ -105,39 +104,27 @@ const StatsMenu: FC<StatsMenuProps> = props => {
105104
}
106105
}
107106

108-
const getActionTitle = (action: SaveStatsAction | null) => {
109-
switch (action) {
110-
case SaveStatsAction.Download:
111-
return "Скачать таблицу со статистикой"
112-
case SaveStatsAction.ShareWithGoogle:
113-
return "Выгрузить таблицу в Google Docs"
114-
case SaveStatsAction.ShareWithYandex:
115-
return "Выгрузить таблицу на Яндекс Диск"
116-
default:
117-
return ""
118-
}
119-
}
120-
121107
const getActionContent = (action: SaveStatsAction | null) => {
122108
switch (action) {
123109
case SaveStatsAction.Download:
124110
return <DownloadStats
125-
courseId={props.courseId}
126-
userId={props.userId}
127-
onCancellation={handleClose}
111+
courseId={courseId}
112+
userId={userId}
113+
onClose={handleClose}
128114
/>
129115
case SaveStatsAction.ShareWithGoogle:
130116
return <ExportToGoogle
131-
courseId={props.courseId}
132-
userId={props.userId}
133-
onCancellation={handleClose}
117+
courseId={courseId}
118+
open={openAction}
119+
onClose={handleClose}
134120
/>
135121
case SaveStatsAction.ShareWithYandex:
136122
return <ExportToYandex
137-
courseId={props.courseId}
138-
userId={props.userId}
139-
onCancellation={handleClose}
140-
userCode={props.yandexCode}
123+
courseId={courseId}
124+
userId={userId}
125+
userCode={yandexCode}
126+
open={openAction}
127+
onClose={handleClose}
141128
/>
142129
default:
143130
return null
@@ -194,15 +181,7 @@ const StatsMenu: FC<StatsMenuProps> = props => {
194181
)}
195182
</NestedMenuItem>
196183
</Menu>
197-
<Dialog
198-
open={saveStatsAction !== null}
199-
onClose={handleClose}
200-
>
201-
<DialogTitle>
202-
{getActionTitle(saveStatsAction)}
203-
</DialogTitle>
204-
{getActionContent(saveStatsAction)}
205-
</Dialog>
184+
{getActionContent(saveStatsAction)}
206185
</div>
207186
)
208187
}
Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,43 @@
1-
import { FC, useState } from "react";
2-
import { Button, DialogActions, DialogContent, Grid } from "@mui/material";
1+
import { FC, useEffect } from "react";
2+
import { useSnackbar } from "notistack";
33
import apiSingleton from "../../api/ApiSingleton";
4-
import { LoadingButton } from "@mui/lab";
54
import Utils from "@/services/Utils";
65

76
interface DownloadStatsProps {
87
courseId: number | undefined
98
userId: string
10-
onCancellation: () => void
9+
onClose: () => void
1110
}
1211

1312
const DownloadStats: FC<DownloadStatsProps> = (props: DownloadStatsProps) => {
14-
const [loading, setLoading] = useState<boolean>(false)
13+
const {courseId, userId, onClose} = props
14+
const {enqueueSnackbar} = useSnackbar()
1515

16-
const handleFileDownloading = () => {
17-
const statsDatetime = Utils.toStringForFileName(new Date())
18-
setLoading(true)
19-
apiSingleton.statisticsApi.statisticsGetFile(props.courseId, props.userId, "Лист 1")
20-
.then((response) => response.blob())
21-
.then((blob) => {
22-
const fileName = `StatsReport_${statsDatetime}`
23-
const url = window.URL.createObjectURL(new Blob([blob]));
24-
const link = document.createElement("a");
25-
link.href = url;
26-
link.setAttribute("download", `${fileName}.xlsx`);
27-
document.body.appendChild(link);
28-
link.click();
29-
link.parentNode!.removeChild(link);
30-
})
31-
.finally(() => setLoading(false))
32-
}
16+
useEffect(() => {
17+
const downloadStats = async () => {
18+
try {
19+
const statsDatetime = Utils.toStringForFileName(new Date())
20+
const response = await apiSingleton.statisticsApi.statisticsGetFile(courseId, userId, "Лист 1")
21+
const blob = await response.blob()
22+
const fileName = `StatsReport_${statsDatetime}`
23+
const url = window.URL.createObjectURL(new Blob([blob]));
24+
const link = document.createElement("a");
25+
link.href = url;
26+
link.setAttribute("download", `${fileName}.xlsx`);
27+
document.body.appendChild(link);
28+
link.click();
29+
link.parentNode!.removeChild(link);
30+
} catch (e) {
31+
console.error("Ошибка при загрузке статистики:", e)
32+
enqueueSnackbar("Не удалось загрузить файл со статистикой, попробуйте позже", {variant: "error"})
33+
}
34+
}
3335

34-
return (
35-
<DialogContent>
36-
<DialogActions style={{ padding: 0 }}>
37-
<Grid item>
38-
<LoadingButton
39-
variant="text"
40-
color="primary"
41-
type="button"
42-
loading={loading}
43-
onClick={handleFileDownloading}
44-
>
45-
Скачать
46-
</LoadingButton>
47-
</Grid>
48-
<Grid item>
49-
<Button variant="text" color="inherit" type="button"
50-
onClick={props.onCancellation}>
51-
Отмена
52-
</Button>
53-
</Grid>
54-
</DialogActions>
55-
</DialogContent>
56-
)
36+
downloadStats()
37+
onClose()
38+
})
39+
40+
return null
5741
}
5842

5943
export default DownloadStats;

0 commit comments

Comments
 (0)