diff --git a/webapp/src/components/common/MatrixInput/index.tsx b/webapp/src/components/common/MatrixInput/index.tsx index 0d43522296..1bc3d00f33 100644 --- a/webapp/src/components/common/MatrixInput/index.tsx +++ b/webapp/src/components/common/MatrixInput/index.tsx @@ -25,6 +25,7 @@ import ImportDialog from "../dialogs/ImportDialog"; import MatrixAssignDialog from "./MatrixAssignDialog"; import { downloadMatrix } from "../../../utils/matrixUtils"; import { fetchMatrixFn } from "../../App/Singlestudy/explore/Modelization/Areas/Hydro/utils"; +import { LoadingButton } from "@mui/lab"; const logErr = debug("antares:createimportform:error"); @@ -56,29 +57,16 @@ function MatrixInput({ const [t] = useTranslation(); const [openImportDialog, setOpenImportDialog] = useState(false); const [openMatrixAsignDialog, setOpenMatrixAsignDialog] = useState(false); + const [isDownloading, setIsDownloading] = useState(false); const { data: matrixData, isLoading, reload: reloadMatrix, - } = usePromiseWithSnackbarError( - async () => { - const res = fetchFn - ? await fetchFn(study.id) - : await getStudyData(study.id, url); - if (typeof res === "string") { - const fixed = res - .replace(/NaN/g, '"NaN"') - .replace(/Infinity/g, '"Infinity"'); - return JSON.parse(fixed); - } - return res; - }, - { - errorMessage: t("data.error.matrix"), - deps: [study, url], - }, - ); + } = usePromiseWithSnackbarError(fetchMatrixData, { + errorMessage: t("data.error.matrix"), + deps: [study.id, url, fetchFn], + }); const { data: matrixIndex } = usePromiseWithSnackbarError( async () => { @@ -99,6 +87,23 @@ function MatrixInput({ */ const rowNames = fetchFn ? matrixIndex : initialRowNames; + //////////////////////////////////////////////////////////////// + // Utils + //////////////////////////////////////////////////////////////// + + async function fetchMatrixData() { + const res = fetchFn + ? await fetchFn(study.id) + : await getStudyData(study.id, url); + if (typeof res === "string") { + const fixed = res + .replace(/NaN/g, '"NaN"') + .replace(/Infinity/g, '"Infinity"'); + return JSON.parse(fixed); + } + return res; + } + //////////////////////////////////////////////////////////////// // Event Handlers //////////////////////////////////////////////////////////////// @@ -133,8 +138,14 @@ function MatrixInput({ } }; - const handleDownload = (matrixData: MatrixType, fileName: string): void => { - downloadMatrix(matrixData, fileName); + const handleDownload = async (matrixData: MatrixType, fileName: string) => { + setIsDownloading(true); + + // Re-fetch to get latest data + const data = await fetchMatrixData(); + downloadMatrix(data, fileName); + + setIsDownloading(false); }; //////////////////////////////////////////////////////////////// @@ -179,10 +190,12 @@ function MatrixInput({ {matrixData?.columns?.length >= 1 && ( - + )}