Redux -> add all Slices / AsyncThunks / storage elements + create code in abstractServica & modelCrudAT

feature/2_forms
TBS093A 2021-02-17 09:15:23 +01:00
parent 10ba48fd08
commit bea0db148c
13 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,103 @@
import axios from 'axios'
const APIAddress = 'http://localhost:9090'
let defaultBody = ''
const _getList = async (endpoint, token) => {
return await responseAbstract(
endpoint,
'GET',
token,
defaultBody
)
}
const _getOne = async (endpoint, objectId, token) => {
return await responseAbstract(
endpoint + '/' + objectId,
'GET',
token,
defaultBody
)
}
const _post = async (endpoint, body, token) => {
return await responseAbstract(
endpoint,
'POST',
token,
body
)
}
const _patch = async (endpoint, objectId, body, token) => {
return await responseAbstract(
endpoint + '/' + objectId,
'PATCH',
token,
body
)
}
const _put = async (endpoint, objectId, body, token) => {
return await responseAbstract(
endpoint + '/' + objectId,
'PUT',
token,
body
)
}
const _delete = async (endpoint, objectId, token) => {
return await responseAbstract(
endpoint + '/' + objectId,
'DELETE',
token,
defaultBody
)
}
const responseAbstract = async (endpoint, method, token, body) => {
let response = await axios(
headerBuilder(
APIAddress + endpoint,
method,
token,
body,
)
)
return response
}
const headerBuilder = (url, method, token, body) => {
let headers = {
url: url,
method: method,
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
}
export default {
APIAddress,
_getList,
_getOne,
_post,
_put,
_patch,
_delete
}

View File

@ -0,0 +1,61 @@
import { createAsyncThunk } from '@reduxjs/toolkit'
import abstractService from './abstractService'
let endpoint = '/model'
const fetchGetAllModels = createAsyncThunk(
'model/fetchGetAllModels',
async (
token,
thunkAPI
) => {
return await abstractService._getList(endpoint, token)
}
)
/**
* @param body:
* param token: base64 token,
* param id: model id
*/
const fetchGetOneModelAndDownload = createAsyncThunk(
'model/fetchGetAllModels',
async (
body,
thunkAPI
) => {
return await abstractService._getOne(
trueEndpoint,
body.id,
body.token
)
}
)
/**
* @param body:
* param token: base64 token,
* param file: document.querySelector('#file').files[0]
* param user_id: user id
*/
const fetchUploadModel = createAsyncThunk(
'model/fetchGetAllModels',
async (
body,
thunkAPI
) => {
let formData = FormData()
formData.append("blend", body.file)
return await abstractService._post(
trueEndpoint,
body,
body.token
)
}
)
export default {
fetchGetAllModels,
fetchGetOneModelAndDownload,
}

View File

View File

View File

View File