-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui-commons): use DownloadButton in DownloadMatrixButton
- Loading branch information
Showing
4 changed files
with
67 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
webapp/src/components/common/buttons/DownloadMatrixButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |