|
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"; |
3 | 3 | import apiSingleton from "../../api/ApiSingleton"; |
4 | | -import { LoadingButton } from "@mui/lab"; |
5 | 4 | import Utils from "@/services/Utils"; |
6 | 5 |
|
7 | 6 | interface DownloadStatsProps { |
8 | 7 | courseId: number | undefined |
9 | 8 | userId: string |
10 | | - onCancellation: () => void |
| 9 | + onClose: () => void |
11 | 10 | } |
12 | 11 |
|
13 | 12 | const DownloadStats: FC<DownloadStatsProps> = (props: DownloadStatsProps) => { |
14 | | - const [loading, setLoading] = useState<boolean>(false) |
| 13 | + const {courseId, userId, onClose} = props |
| 14 | + const {enqueueSnackbar} = useSnackbar() |
15 | 15 |
|
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 | + } |
33 | 35 |
|
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 |
57 | 41 | } |
58 | 42 |
|
59 | 43 | export default DownloadStats; |
0 commit comments