react_admin_layout
This commit is contained in:
43
src/stores/slices/user.slice.ts
Normal file
43
src/stores/slices/user.slice.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
|
||||
import type { User } from "../../types/user.type";
|
||||
import { Apis } from "../../apis";
|
||||
|
||||
|
||||
interface UserState {
|
||||
data: User | null,
|
||||
loading: boolean;
|
||||
}
|
||||
|
||||
const InitUserState: UserState = {
|
||||
data: null,
|
||||
loading: false
|
||||
}
|
||||
|
||||
|
||||
const userSlice = createSlice({
|
||||
name: "user",
|
||||
initialState: InitUserState,
|
||||
reducers: {
|
||||
|
||||
},
|
||||
extraReducers: (bd) => {
|
||||
bd.addCase(fetchUserData.fulfilled, (state, action) => {
|
||||
state.data = action.payload
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
const fetchUserData = createAsyncThunk(
|
||||
"user/fetchUserData",
|
||||
async () => {
|
||||
let result = await Apis.user.findById(localStorage.getItem("userLogin"))
|
||||
return result.data
|
||||
}
|
||||
)
|
||||
|
||||
export const userReducer = userSlice.reducer;
|
||||
export const userAction = {
|
||||
...userSlice.actions,
|
||||
fetchUserData
|
||||
}
|
||||
Reference in New Issue
Block a user