From 49b0aa505e9db56e70a7a6890c85a6f22cc27ac4 Mon Sep 17 00:00:00 2001 From: TBS093A Date: Thu, 18 Feb 2021 20:38:17 +0100 Subject: [PATCH] React / Forms -> compleate uploader form --- .../asyncThunks/abstracts/abstractService.js | 42 ++++++++++--------- src/redux/asyncThunks/modelCrudAsyncThunk.js | 9 +++- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/redux/asyncThunks/abstracts/abstractService.js b/src/redux/asyncThunks/abstracts/abstractService.js index b3ae2b0..51fe1c1 100644 --- a/src/redux/asyncThunks/abstracts/abstractService.js +++ b/src/redux/asyncThunks/abstracts/abstractService.js @@ -72,7 +72,6 @@ const responseAbstract = async (endpoint, method, token, body) => { body, ) ) - console.log(response) return response } @@ -83,14 +82,6 @@ const headerBuilder = (url, method, token, body) => { 'accept': 'application/json', 'content-type': 'application/json', } - if ('file' in body) { - headers_r = { - 'authorization': token, - 'x-csrftoken': getCookie('csrftoken'), - // 'accept': 'multipart/form-data', - // 'content-type': 'multipart/form-data' - } - } let headers = { url: url, method: method, @@ -98,15 +89,9 @@ const headerBuilder = (url, method, token, body) => { credentials: 'same-origin' } if (method === 'PUT' || method === 'POST' || method === 'PATCH') { - if ('file' in body) { - headers = Object.assign({}, headers, { - data: body, - }) - } else { - headers = Object.assign({}, headers, { - data: JSON.stringify(body), - }) - } + headers = Object.assign({}, headers, { + data: JSON.stringify(body), + }) } return headers } @@ -129,7 +114,23 @@ const getCookie = (name) => { return decodeURIComponent(token[0].split('=')[1]); } - +const axiosFilePost = async ( endpoint, body, token ) => { + let formData = new FormData() + formData.append('file', body.file) + formData.append('user_id', body.user_id) + let response = await axios.post( + APIAddress + endpoint, + formData, + { + headers: { + 'authorization': token, + 'x-csrftoken': getCookie('csrftoken') + } + } + ) + console.log(response) + return response +} export default { APIAddress, @@ -138,5 +139,6 @@ export default { _post, _put, _patch, - _delete + _delete, + axiosFilePost } \ No newline at end of file diff --git a/src/redux/asyncThunks/modelCrudAsyncThunk.js b/src/redux/asyncThunks/modelCrudAsyncThunk.js index 261312d..1dab313 100644 --- a/src/redux/asyncThunks/modelCrudAsyncThunk.js +++ b/src/redux/asyncThunks/modelCrudAsyncThunk.js @@ -45,11 +45,16 @@ const fetchUploadModel = createAsyncThunk( body, thunkAPI ) => { - return await abstractService._post( - endpoint, + return await abstractService.axiosFilePost( + endpoint, body, body.token ) + // return await abstractService._post( + // endpoint, + // body, + // body.token + // ) } )