React / Forms -> compleate uploader form

feature/2_forms
TBS093A 2021-02-18 20:38:17 +01:00
parent 96f1d508fb
commit 9b066d6d2f
2 changed files with 29 additions and 22 deletions

View File

@ -72,7 +72,6 @@ const responseAbstract = async (endpoint, method, token, body) => {
body, body,
) )
) )
console.log(response)
return response return response
} }
@ -83,14 +82,6 @@ const headerBuilder = (url, method, token, body) => {
'accept': 'application/json', 'accept': 'application/json',
'content-type': '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 = { let headers = {
url: url, url: url,
method: method, method: method,
@ -98,15 +89,9 @@ const headerBuilder = (url, method, token, body) => {
credentials: 'same-origin' credentials: 'same-origin'
} }
if (method === 'PUT' || method === 'POST' || method === 'PATCH') { if (method === 'PUT' || method === 'POST' || method === 'PATCH') {
if ('file' in body) { headers = Object.assign({}, headers, {
headers = Object.assign({}, headers, { data: JSON.stringify(body),
data: body, })
})
} else {
headers = Object.assign({}, headers, {
data: JSON.stringify(body),
})
}
} }
return headers return headers
} }
@ -129,7 +114,23 @@ const getCookie = (name) => {
return decodeURIComponent(token[0].split('=')[1]); 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 { export default {
APIAddress, APIAddress,
@ -138,5 +139,6 @@ export default {
_post, _post,
_put, _put,
_patch, _patch,
_delete _delete,
axiosFilePost
} }

View File

@ -45,11 +45,16 @@ const fetchUploadModel = createAsyncThunk(
body, body,
thunkAPI thunkAPI
) => { ) => {
return await abstractService._post( return await abstractService.axiosFilePost(
endpoint, endpoint,
body, body,
body.token body.token
) )
// return await abstractService._post(
// endpoint,
// body,
// body.token
// )
} }
) )