Skip to content

Commit

Permalink
feat(ui-api): update importFile function
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Sep 15, 2024
1 parent ac2a2c4 commit 82db033
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
5 changes: 3 additions & 2 deletions webapp/src/components/common/MatrixInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
StudyMetadata,
} from "../../../common/types";
import useEnqueueErrorSnackbar from "../../../hooks/useEnqueueErrorSnackbar";
import { getStudyData, importFile } from "../../../services/api/study";
import { getStudyData } from "../../../services/api/study";
import usePromiseWithSnackbarError from "../../../hooks/usePromiseWithSnackbarError";
import { editMatrix, getStudyMatrixIndex } from "../../../services/api/matrix";
import { Root, Content, Header } from "./style";
Expand All @@ -23,6 +23,7 @@ import MatrixAssignDialog from "./MatrixAssignDialog";
import { fetchMatrixFn } from "../../App/Singlestudy/explore/Modelization/Areas/Hydro/utils";
import SplitButton from "../buttons/SplitButton";
import DownloadMatrixButton from "../buttons/DownloadMatrixButton.tsx";
import { importFile } from "../../../services/api/studies/raw/index.ts";

interface Props {
study: StudyMetadata | StudyMetadata["id"];
Expand Down Expand Up @@ -123,7 +124,7 @@ function MatrixInput({
};

const handleImport = async (file: File) => {
await importFile(file, studyId, url);
await importFile({ file, studyId, path: url });
reloadMatrix();
};

Expand Down
19 changes: 16 additions & 3 deletions webapp/src/services/api/studies/raw/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import client from "../../client";
import type { DownloadMatrixParams } from "./types";
import type { DownloadMatrixParams, ImportFileParams } from "./types";

export async function downloadMatrix(params: DownloadMatrixParams) {
const { studyId, ...rest } = params;
const { studyId, ...queryParams } = params;
const url = `v1/studies/${studyId}/raw/download`;
const res = await client.get<Blob>(url, {
params: rest,
params: queryParams,
responseType: "blob",
});

return res.data;
}

export async function importFile(params: ImportFileParams) {
const { studyId, file, onUploadProgress, ...queryParams } = params;
const url = `v1/studies/${studyId}/raw`;
const body = { file };
await client.putForm<void>(url, body, {
params: {
...queryParams,
create_missing: queryParams.createMissing,
},
onUploadProgress,
});
}
9 changes: 9 additions & 0 deletions webapp/src/services/api/studies/raw/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AxiosRequestConfig } from "axios";
import type { StudyMetadata } from "../../../../common/types";

export interface DownloadMatrixParams {
Expand All @@ -7,3 +8,11 @@ export interface DownloadMatrixParams {
header?: boolean;
index?: boolean;
}

export interface ImportFileParams {
studyId: StudyMetadata["id"];
path: string;
file: File;
createMissing?: boolean;
onUploadProgress?: AxiosRequestConfig["onUploadProgress"];
}
31 changes: 0 additions & 31 deletions webapp/src/services/api/study.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,37 +243,6 @@ export const importStudy = async (
return res.data;
};

export const importFile = async (
file: File,
study: string,
path: string,
onProgress?: (progress: number) => void,
): Promise<string> => {
const options: AxiosRequestConfig = {};
if (onProgress) {
options.onUploadProgress = (progressEvent): void => {
const percentCompleted = Math.round(
(progressEvent.loaded * 100) / (progressEvent.total || 1),
);
onProgress(percentCompleted);
};
}
const formData = new FormData();
formData.append("file", file);
const restconfig = {
...options,
headers: {
"content-type": "multipart/form-data",
},
};
const res = await client.put(
`/v1/studies/${study}/raw?path=${encodeURIComponent(path)}`,
formData,
restconfig,
);
return res.data;
};

export const launchStudy = async (
sid: string,
options: LaunchOptions = {},
Expand Down

0 comments on commit 82db033

Please sign in to comment.