Skip to content

Commit

Permalink
feat(ui-commons): use DownloadButton in DownloadMatrixButton
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Sep 13, 2024
1 parent 9d6ea71 commit eb16e04
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import BooleanFE from "../../../../../common/fieldEditors/BooleanFE";
import SelectFE from "../../../../../common/fieldEditors/SelectFE";
import NumberFE from "../../../../../common/fieldEditors/NumberFE";
import moment from "moment";
import DownloadMatrixButton from "../../../../../common/DownloadMatrixButton.tsx";
import DownloadMatrixButton from "../../../../../common/buttons/DownloadMatrixButton.tsx";

function ResultDetails() {
const { study } = useOutletContext<{ study: StudyMetadata }>();
Expand Down
85 changes: 0 additions & 85 deletions webapp/src/components/common/DownloadMatrixButton.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion webapp/src/components/common/MatrixInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ImportDialog from "../dialogs/ImportDialog";
import MatrixAssignDialog from "./MatrixAssignDialog";
import { fetchMatrixFn } from "../../App/Singlestudy/explore/Modelization/Areas/Hydro/utils";
import SplitButton from "../buttons/SplitButton";
import DownloadMatrixButton from "../DownloadMatrixButton.tsx";
import DownloadMatrixButton from "../buttons/DownloadMatrixButton.tsx";

interface Props {
study: StudyMetadata | StudyMetadata["id"];
Expand Down
65 changes: 65 additions & 0 deletions webapp/src/components/common/buttons/DownloadMatrixButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { downloadMatrix } from "../../../services/api/studies/raw";
import { downloadFile } from "../../../utils/fileUtils";
import { StudyMetadata } from "../../../common/types";
import { useTranslation } from "react-i18next";
import DownloadButton from "./DownloadButton";

export interface DownloadMatrixButtonProps {
studyId: StudyMetadata["id"];
path: string;
disabled?: boolean;
label?: string;
}

const EXPORT_OPTIONS = [
{ label: "TSV", value: "tsv" },
{ label: "Excel", value: "xlsx" },
] as const;

type ExportFormat = (typeof EXPORT_OPTIONS)[number]["value"];

function DownloadMatrixButton(props: DownloadMatrixButtonProps) {
const { t } = useTranslation();
const { studyId, path, disabled, label = t("global.export") } = props;

////////////////////////////////////////////////////////////////
// Event Handlers
////////////////////////////////////////////////////////////////

const handleDownload = async (format: ExportFormat) => {
if (!path) {
return;
}

const isExcel = format === "xlsx";

const res = await downloadMatrix({
studyId,
path,
format,
header: isExcel,
index: isExcel,
});

return downloadFile(
res,
`matrix_${studyId}_${path.replace("/", "_")}.${format}`,
);
};

////////////////////////////////////////////////////////////////
// JSX
////////////////////////////////////////////////////////////////

return (
<DownloadButton
formatOptions={[...EXPORT_OPTIONS]}
onClick={handleDownload}
disabled={!path || disabled}
>
{label}
</DownloadButton>
);
}

export default DownloadMatrixButton;

0 comments on commit eb16e04

Please sign in to comment.