From e660ccce6fec43c93bf2a940dada73b0f59cd573 Mon Sep 17 00:00:00 2001 From: TBS093A Date: Wed, 17 Feb 2021 11:53:26 +0100 Subject: [PATCH] Redux -> simple fixes & upgrade slices --- src/redux/asyncThunks/abstractService.js | 8 +-- src/redux/asyncThunks/userCrudAsyncThunk.js | 69 ++++++++++++++++++++- src/redux/slices/renderCrudSlice.js | 4 +- src/redux/slices/userAuthSlice.js | 37 +++++++++++ src/redux/slices/userCrudSlice.js | 41 ++++++++++++ 5 files changed, 151 insertions(+), 8 deletions(-) diff --git a/src/redux/asyncThunks/abstractService.js b/src/redux/asyncThunks/abstractService.js index 61375ea..302950a 100644 --- a/src/redux/asyncThunks/abstractService.js +++ b/src/redux/asyncThunks/abstractService.js @@ -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 diff --git a/src/redux/asyncThunks/userCrudAsyncThunk.js b/src/redux/asyncThunks/userCrudAsyncThunk.js index 8f04df1..c83677e 100644 --- a/src/redux/asyncThunks/userCrudAsyncThunk.js +++ b/src/redux/asyncThunks/userCrudAsyncThunk.js @@ -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 } \ No newline at end of file diff --git a/src/redux/slices/renderCrudSlice.js b/src/redux/slices/renderCrudSlice.js index d9d92d2..1019395 100644 --- a/src/redux/slices/renderCrudSlice.js +++ b/src/redux/slices/renderCrudSlice.js @@ -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 diff --git a/src/redux/slices/userAuthSlice.js b/src/redux/slices/userAuthSlice.js index e69de29..b4b8fe0 100644 --- a/src/redux/slices/userAuthSlice.js +++ b/src/redux/slices/userAuthSlice.js @@ -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 \ No newline at end of file diff --git a/src/redux/slices/userCrudSlice.js b/src/redux/slices/userCrudSlice.js index e69de29..1e86608 100644 --- a/src/redux/slices/userCrudSlice.js +++ b/src/redux/slices/userCrudSlice.js @@ -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 \ No newline at end of file