Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui-common): download latest value in MatrixInput component #1962

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions webapp/src/components/common/MatrixInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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);
};

////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -179,10 +190,12 @@ function MatrixInput({
</Button>

{matrixData?.columns?.length >= 1 && (
<Button
<LoadingButton
sx={{
ml: 2,
}}
loadingPosition="start"
loading={isDownloading}
variant="outlined"
color="primary"
startIcon={<DownloadOutlinedIcon />}
Expand All @@ -194,7 +207,7 @@ function MatrixInput({
}
>
{t("global.download")}
</Button>
</LoadingButton>
)}
</Box>
</Header>
Expand Down
Loading