Skip to content

Commit

Permalink
refactor(ui-raw): update getRawFile to return File instance instead…
Browse files Browse the repository at this point in the history
… of custom RawFile type
  • Loading branch information
hdinia committed Dec 20, 2024
1 parent 60cf750 commit 5f6d707
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ function Json({ filePath, filename, studyId, canEdit }: DataCompProps) {
////////////////////////////////////////////////////////////////

const handleDownload = async () => {
const { data, filename } = await getRawFile({ studyId, path: filePath });
downloadFile(data, filename);
const file = await getRawFile({ studyId, path: filePath });
downloadFile(file, file.name);
};

const handleSave: JSONEditorProps["onSave"] = (json) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ function Text({
////////////////////////////////////////////////////////////////

const handleDownload = async () => {
const { data, filename } = await getRawFile({ studyId, path: filePath });
downloadFile(data, filename);
const file = await getRawFile({ studyId, path: filePath });
downloadFile(file, file.name);
};

const handleUploadSuccessful = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function Unsupported({ studyId, filePath, filename, canEdit }: DataCompProps) {
////////////////////////////////////////////////////////////////

const handleDownload = async () => {
const { data, filename } = await getRawFile({ studyId, path: filePath });
downloadFile(data, filename);
const file = await getRawFile({ studyId, path: filePath });
downloadFile(file, file.name);
};

////////////////////////////////////////////////////////////////
Expand Down
10 changes: 6 additions & 4 deletions webapp/src/components/common/buttons/DownloadMatrixButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { useTranslation } from "react-i18next";
import DownloadButton from "./DownloadButton";
import type { TTableExportFormat } from "@/services/api/studies/raw/types";

type ExportFormat = TTableExportFormat | "raw";

export interface DownloadMatrixButtonProps {
studyId: StudyMetadata["id"];
path: string;
Expand All @@ -30,7 +32,7 @@ function DownloadMatrixButton(props: DownloadMatrixButtonProps) {
const { t } = useTranslation();
const { studyId, path, disabled, label = t("global.export") } = props;

const options: Array<{ label: string; value: TTableExportFormat }> = [
const options: Array<{ label: string; value: ExportFormat }> = [
{ label: "CSV", value: "csv" },
{
label: `CSV (${t("global.semicolon").toLowerCase()})`,
Expand All @@ -45,14 +47,14 @@ function DownloadMatrixButton(props: DownloadMatrixButtonProps) {
// Event Handlers
////////////////////////////////////////////////////////////////

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

if (format === "raw") {
const { data, filename } = await getRawFile({ studyId, path });
return downloadFile(data, filename);
const file = await getRawFile({ studyId, path });
return downloadFile(file, file.name);
}

const isXlsx = format === "xlsx";
Expand Down
1 change: 0 additions & 1 deletion webapp/src/services/api/studies/raw/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ export const TableExportFormat = {
TSV: "tsv",
CSV: "csv",
CSV_SEMICOLON: "csv (semicolon)",
RAW: "raw",
} as const;
13 changes: 6 additions & 7 deletions webapp/src/services/api/studies/raw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
DeleteFileParams,
GetMatrixFileParams,
GetRawFileParams,
RawFile,
UploadFileParams,
} from "./types";

Expand Down Expand Up @@ -94,10 +93,10 @@ export async function deleteFile(params: DeleteFileParams) {
* @param params.path - Path to the file within the study
* @returns Promise containing the file data and metadata
*/
export async function getRawFile(params: GetRawFileParams): Promise<RawFile> {
export async function getRawFile(params: GetRawFileParams) {
const { studyId, path } = params;

const { data, headers } = await client.get<RawFile["data"]>(
const { data, headers } = await client.get<File>(
`/v1/studies/${studyId}/raw/original-file`,
{
params: {
Expand All @@ -119,8 +118,8 @@ export async function getRawFile(params: GetRawFileParams): Promise<RawFile> {
}
}

return {
data,
filename,
};
return new File([data], filename, {
type: data.type, // Preserve the MIME type from the Blob
lastModified: new Date().getTime(),
});
}
4 changes: 0 additions & 4 deletions webapp/src/services/api/studies/raw/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,3 @@ export interface GetRawFileParams {
studyId: string;
path: string;
}
export interface RawFile {
data: Blob;
filename: string;
}

0 comments on commit 5f6d707

Please sign in to comment.