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,
)
)
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,16 +89,10 @@ 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),
})
}
}
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
}

View File

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