diff --git a/webapp/src/components/common/MatrixInput/index.tsx b/webapp/src/components/common/MatrixInput/index.tsx index 2cea0abb24..3c507e6414 100644 --- a/webapp/src/components/common/MatrixInput/index.tsx +++ b/webapp/src/components/common/MatrixInput/index.tsx @@ -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"; @@ -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"]; @@ -123,7 +124,7 @@ function MatrixInput({ }; const handleImport = async (file: File) => { - await importFile(file, studyId, url); + await importFile({ file, studyId, path: url }); reloadMatrix(); }; diff --git a/webapp/src/services/api/studies/raw/index.ts b/webapp/src/services/api/studies/raw/index.ts index 63e46dc31a..f7350f1015 100644 --- a/webapp/src/services/api/studies/raw/index.ts +++ b/webapp/src/services/api/studies/raw/index.ts @@ -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(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(url, body, { + params: { + ...queryParams, + create_missing: queryParams.createMissing, + }, + onUploadProgress, + }); +} diff --git a/webapp/src/services/api/studies/raw/types.ts b/webapp/src/services/api/studies/raw/types.ts index e524fbdc72..2c62844115 100644 --- a/webapp/src/services/api/studies/raw/types.ts +++ b/webapp/src/services/api/studies/raw/types.ts @@ -1,3 +1,4 @@ +import type { AxiosRequestConfig } from "axios"; import type { StudyMetadata } from "../../../../common/types"; export interface DownloadMatrixParams { @@ -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"]; +} diff --git a/webapp/src/services/api/study.ts b/webapp/src/services/api/study.ts index a060bd8a16..f63404f207 100644 --- a/webapp/src/services/api/study.ts +++ b/webapp/src/services/api/study.ts @@ -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 => { - 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 = {},