Redux -> add other endpoint operations in AsyncThunks

feature/2_forms
TBS093A 2021-02-17 09:55:24 +01:00
parent bea0db148c
commit cd746fbb6b
5 changed files with 141 additions and 22 deletions

View File

@ -16,7 +16,7 @@ const _getList = async (endpoint, token) => {
const _getOne = async (endpoint, objectId, token) => { const _getOne = async (endpoint, objectId, token) => {
return await responseAbstract( return await responseAbstract(
endpoint + '/' + objectId, endpoint + objectId,
'GET', 'GET',
token, token,
defaultBody defaultBody
@ -34,7 +34,7 @@ const _post = async (endpoint, body, token) => {
const _patch = async (endpoint, objectId, body, token) => { const _patch = async (endpoint, objectId, body, token) => {
return await responseAbstract( return await responseAbstract(
endpoint + '/' + objectId, endpoint + objectId,
'PATCH', 'PATCH',
token, token,
body body
@ -43,7 +43,7 @@ const _patch = async (endpoint, objectId, body, token) => {
const _put = async (endpoint, objectId, body, token) => { const _put = async (endpoint, objectId, body, token) => {
return await responseAbstract( return await responseAbstract(
endpoint + '/' + objectId, endpoint + objectId,
'PUT', 'PUT',
token, token,
body body
@ -52,7 +52,7 @@ const _put = async (endpoint, objectId, body, token) => {
const _delete = async (endpoint, objectId, token) => { const _delete = async (endpoint, objectId, token) => {
return await responseAbstract( return await responseAbstract(
endpoint + '/' + objectId, endpoint + objectId,
'DELETE', 'DELETE',
token, token,
defaultBody defaultBody
@ -73,22 +73,30 @@ const responseAbstract = async (endpoint, method, token, body) => {
} }
const headerBuilder = (url, method, token, body) => { const headerBuilder = (url, method, token, body) => {
let headers = { headers = {
url: url, 'Authorization': token,
method: method, 'accept': 'application/json',
headers: { 'Content-Type': 'application/json',
'Authorization': token,
'accept': 'application/json',
'Content-Type': 'application/json',
} }
} if ('file' in body) {
if (method === 'PUT' || method === 'POST' || method === 'PATCH') { headers = {
headers = Object.assign({}, headers, { 'Authorization': token,
data: JSON.stringify(body), 'accept': 'multipart/form-data',
withCredentials: true, 'Content-Type': 'multipart/form-data',
}) }
} }
return headers let headers = {
url: url,
method: method,
headers: headers
}
if (method === 'PUT' || method === 'POST' || method === 'PATCH') {
headers = Object.assign({}, headers, {
data: JSON.stringify(body),
withCredentials: true,
})
}
return headers
} }

View File

@ -1,7 +1,7 @@
import { createAsyncThunk } from '@reduxjs/toolkit' import { createAsyncThunk } from '@reduxjs/toolkit'
import abstractService from './abstractService' import abstractService from './abstractService'
let endpoint = '/model' let endpoint = '/model/'
const fetchGetAllModels = createAsyncThunk( const fetchGetAllModels = createAsyncThunk(
@ -47,6 +47,7 @@ const fetchUploadModel = createAsyncThunk(
) => { ) => {
let formData = FormData() let formData = FormData()
formData.append("blend", body.file) formData.append("blend", body.file)
body.file = formData
return await abstractService._post( return await abstractService._post(
trueEndpoint, trueEndpoint,
body, body,
@ -56,6 +57,7 @@ const fetchUploadModel = createAsyncThunk(
) )
export default { export default {
fetchGetAllModels, fetchGetAllModels,
fetchGetOneModelAndDownload, fetchGetOneModelAndDownload,
fetchUploadModel
} }

View File

@ -0,0 +1,39 @@
import { createAsyncThunk } from '@reduxjs/toolkit'
import abstractService from './abstractService'
let endpoint = '/render/'
const fetchGetAllRenders = createAsyncThunk(
'model/fetchGetAllModels',
async (
token,
thunkAPI
) => {
return await abstractService._getList(endpoint, token)
}
)
/**
* @param body:
* param token: base64 token,
* param id: render id
*/
const fetchGetOneRenderAndDownload = createAsyncThunk(
'model/fetchGetAllModels',
async (
body,
thunkAPI
) => {
return await abstractService._getOne(
trueEndpoint,
body.id,
body.token
)
}
)
export default {
fetchGetAllRenders,
fetchGetOneRenderAndDownload
}

View File

@ -0,0 +1,27 @@
import { createAsyncThunk } from '@reduxjs/toolkit'
import abstractService from './abstractService'
let endpoint = '/user/auth'
/**
* @param body:
* param username: username string
* param password: password string
*/
const fetchLogin = createAsyncThunk(
'model/fetchGetAllModels',
async (
body,
thunkAPI
) => {
return await abstractService._post(
endpoint,
body,
''
)
}
)
export default {
fetchLogin
}

View File

@ -0,0 +1,43 @@
import { createAsyncThunk } from '@reduxjs/toolkit'
import abstractService from './abstractService'
let endpoint = '/model/'
const fetchGetAllUsers = createAsyncThunk(
'model/fetchGetAllModels',
async (
token,
thunkAPI
) => {
return await abstractService._getList(
endpoint,
token
)
}
)
/**
* @param body:
* param username: username string
* param password: password string
* param email: email string
*/
const fetchRegister = createAsyncThunk(
'model/fetchGetAllModels',
async (
body,
thunkAPI
) => {
return await abstractService._post(
trueEndpoint,
body,
''
)
}
)
export default {
fetchGetAllUsers,
fetchRegister
}