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) => {
return await responseAbstract(
endpoint + '/' + objectId,
endpoint + objectId,
'GET',
token,
defaultBody
@ -34,7 +34,7 @@ const _post = async (endpoint, body, token) => {
const _patch = async (endpoint, objectId, body, token) => {
return await responseAbstract(
endpoint + '/' + objectId,
endpoint + objectId,
'PATCH',
token,
body
@ -43,7 +43,7 @@ const _patch = async (endpoint, objectId, body, token) => {
const _put = async (endpoint, objectId, body, token) => {
return await responseAbstract(
endpoint + '/' + objectId,
endpoint + objectId,
'PUT',
token,
body
@ -52,7 +52,7 @@ const _put = async (endpoint, objectId, body, token) => {
const _delete = async (endpoint, objectId, token) => {
return await responseAbstract(
endpoint + '/' + objectId,
endpoint + objectId,
'DELETE',
token,
defaultBody
@ -73,22 +73,30 @@ const responseAbstract = async (endpoint, method, token, body) => {
}
const headerBuilder = (url, method, token, body) => {
let headers = {
url: url,
method: method,
headers: {
'Authorization': token,
'accept': 'application/json',
'Content-Type': 'application/json',
headers = {
'Authorization': token,
'accept': 'application/json',
'Content-Type': 'application/json',
}
}
if (method === 'PUT' || method === 'POST' || method === 'PATCH') {
headers = Object.assign({}, headers, {
data: JSON.stringify(body),
withCredentials: true,
})
}
return headers
if ('file' in body) {
headers = {
'Authorization': token,
'accept': 'multipart/form-data',
'Content-Type': 'multipart/form-data',
}
}
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 abstractService from './abstractService'
let endpoint = '/model'
let endpoint = '/model/'
const fetchGetAllModels = createAsyncThunk(
@ -47,6 +47,7 @@ const fetchUploadModel = createAsyncThunk(
) => {
let formData = FormData()
formData.append("blend", body.file)
body.file = formData
return await abstractService._post(
trueEndpoint,
body,
@ -56,6 +57,7 @@ const fetchUploadModel = createAsyncThunk(
)
export default {
fetchGetAllModels,
fetchGetOneModelAndDownload,
fetchGetAllModels,
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
}