Redux -> simple fixes & upgrade slices

feature/2_forms
TBS093A 2021-02-17 11:53:26 +01:00
parent 39c796eb1a
commit e660ccce6f
5 changed files with 151 additions and 8 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

View File

@ -17,6 +17,25 @@ const fetchGetAllUsers = createAsyncThunk(
}
)
/**
* @param body:
* param token: token
* param user_id: user_id
*/
const fetchGetOneUser = createAsyncThunk(
'user/fetchGetAllUsers',
async (
body,
thunkAPI
) => {
return await abstractService._getOne(
endpoint,
body.user_id,
body.token
)
}
)
/**
* @param body:
* param username: username string
@ -30,14 +49,60 @@ const fetchRegister = createAsyncThunk(
thunkAPI
) => {
return await abstractService._post(
trueEndpoint,
endpoint,
body,
''
)
}
)
/**
* @param body:
* param token: token
* param user_id: user_id
* param user:
* param username: username
* param password: password
* param email: email
*/
const fetchUpdateUser = createAsyncThunk(
'user/fetchRegister',
async (
body,
thunkAPI
) => {
return await abstractService._patch(
endpoint,
body.user_id,
body.user,
body.token
)
}
)
/**
* @param body:
* param user_id: user_id
* param token: user token
*/
const fetchDeleteUser = createAsyncThunk(
'user/fetchRegister',
async (
body,
thunkAPI
) => {
return await abstractService._delete(
endpoint,
body.user_id,
body.token
)
}
)
export default {
fetchGetAllUsers,
fetchRegister
fetchGetOneUser,
fetchRegister,
fetchUpdateUser,
fetchDeleteUser
}

View File

@ -5,13 +5,13 @@ const renderCrudSlice = createSlice(
{
name: 'render',
initialState: {
models_list: [],
render_list: [],
download_zip_file: ''
},
reducers: {},
extraReducers: {
[renderCrudAsyncThunk.fetchGetAllRenders.fulfilled.type]: (state, action) => {
state.renders_list = action.payload.data
state.render_list = action.payload.data.render_list
},
[renderCrudAsyncThunk.fetchGetOneRenderAndDownload.fulfilled.type]: (state, action) => {
state.download_zip_file = action.payload.data

View File

@ -0,0 +1,37 @@
import { createSlice } from '@reduxjs/toolkit'
import userAuthAsyncThunk from '../asyncThunks/userAuthAsyncThunk'
const userAuthSlice = createSlice(
{
name: 'user/auth',
initialState: {
token: '',
user: {
id: 0,
username: '',
email: ''
}
},
reducers: {},
extraReducers: {
[userAuthAsyncThunk.fetchLogin.fulfilled.type]: (state, action) => {
state.token = action.payload.data.Authorization
state.user.id = action.payload.data.user.id
state.user.username = action.payload.data.user.username
state.user.email = action.payload.data.user.email
state.info = 'login success'
},
[userAuthAsyncThunk.fetchLogout.fulfilled.type]: (state, action) => {
state.token = ''
state.user.id = 0
state.user.username = ''
state.user.email = ''
state.info = action.payload.data.info
}
}
}
)
export const userAuthReducer = userAuthSlice.reducer
export const userAuthSelector = state => state.userAuthReducer

View File

@ -0,0 +1,41 @@
import { createSlice } from '@reduxjs/toolkit'
import userCrudAsyncThunk from '../asyncThunks/userCrudAsyncThunk'
const userCrudSlice = createSlice(
{
name: 'user',
initialState: {
users_list: [],
user_get: {},
user_register: {},
user_update: {},
user_delete: ''
},
reducers: {},
extraReducers: {
[userCrudAsyncThunk.fetchGetAllUsers.fulfilled.type]: (state, action) => {
state.users_list = action.payload.data
},
[userCrudAsyncThunk.fetchGetOneUser.fulfilled.type]: (state, action) => {
state.user_get = action.payload.data
},
[userCrudAsyncThunk.fetchRegister.fulfilled.type]: (state, action) => {
state.user_register = action.payload.data
},
[userCrudAsyncThunk.fetchUpdateUser.fulfilled.type]: (state, action) => {
state.user_update = action.payload.data
},
[userCrudAsyncThunk.fetchDeleteUser.fulfilled.type]: (state, action) => {
state.users_list = []
state.user_get = {}
state.user_register = {}
state.user_update = {}
state.user_delete = 'true'
}
}
}
)
export const userCrudReducer = userCrudSlice.reducer
export const userCrudSelector = state => state.userCrudReducer