Skip to content

Commit

Permalink
feat(redux): create user slice and add it to the store
Browse files Browse the repository at this point in the history
  • Loading branch information
danilych committed Feb 10, 2024
1 parent 97baac2 commit a05dbc1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
54 changes: 54 additions & 0 deletions app/redux/slices/user.ts
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
3 changes: 2 additions & 1 deletion app/redux/store.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { configureStore } from "@reduxjs/toolkit";
import { authReducer } from "./slices/auth";
import { courseReducer } from "./slices/courses";

import { userReducer } from "./slices/user";

const store = configureStore({
reducer: {
courses: courseReducer,
auth: authReducer,
user: userReducer,
},
});

Expand Down

0 comments on commit a05dbc1

Please sign in to comment.