From 1045fff5424a4e8b537de86fdc7da8176bcb6790 Mon Sep 17 00:00:00 2001 From: Samir Kamal <1954121+skamril@users.noreply.github.com> Date: Mon, 4 Mar 2024 10:48:02 +0100 Subject: [PATCH] fix(ui-common): dowload latest value in MatrixInput component --- .../components/common/MatrixInput/index.tsx | 57 ++++++++++++------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/webapp/src/components/common/MatrixInput/index.tsx b/webapp/src/components/common/MatrixInput/index.tsx index 0d43522296..400465ee19 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 [isDowloading, 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 && ( - + )}