-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(redux): create user slice and add it to the store
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit' | ||
import instance from '~/axios' | ||
|
||
export const fetchUser = createAsyncThunk( | ||
'user/fetchUser', | ||
async (params: any) => { | ||
const { data } = await instance.post('/api/user/getUserProfile', params) | ||
|
||
return data | ||
} | ||
) | ||
|
||
const initialState = { | ||
user: { | ||
userData: { | ||
id: "", | ||
name: "", | ||
email: "", | ||
firstName: "", | ||
lastName: "", | ||
phoneNumber: "", | ||
aboutMe: "", | ||
enctyptedID: "", | ||
dateOfBirth: "", | ||
}, | ||
status: 'loading', | ||
}, | ||
} | ||
|
||
const userSlice = createSlice({ | ||
name: 'user', | ||
initialState, | ||
reducers: {}, | ||
extraReducers: builder => { | ||
builder.addCase(fetchUser.pending, (state, action) => { | ||
state.user.status = 'loading' | ||
|
||
// @ts-ignore | ||
state.user.userData = null | ||
}) | ||
builder.addCase(fetchUser.fulfilled, (state, action) => { | ||
state.user.status = 'loaded' | ||
state.user.userData = action.payload | ||
}) | ||
builder.addCase(fetchUser.rejected, (state, action) => { | ||
state.user.status = 'error' | ||
|
||
// @ts-ignore | ||
state.user.userData = null | ||
}) | ||
}, | ||
}) | ||
|
||
export const userReducer = userSlice.reducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters