implement redux toolkit for manage globle state
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
// store.ts
|
||||
import { configureStore } from "@reduxjs/toolkit";
|
||||
import userReducer from "./userSlice"; // adjust the path
|
||||
|
||||
export const store = configureStore({
|
||||
reducer: {
|
||||
user: userReducer, // register user slice here
|
||||
},
|
||||
});
|
||||
|
||||
// ✅ Types for TypeScript
|
||||
export type RootState = ReturnType<typeof store.getState>;
|
||||
export type AppDispatch = typeof store.dispatch;
|
||||
@@ -0,0 +1,46 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
import type { PayloadAction } from "@reduxjs/toolkit";
|
||||
|
||||
export interface UserState {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
profilePicture: string;
|
||||
role: string;
|
||||
organizerStatus: string;
|
||||
profileStatus: boolean;
|
||||
}
|
||||
|
||||
const initialState: UserState = {
|
||||
id: "",
|
||||
name: "",
|
||||
email: "",
|
||||
profilePicture: "",
|
||||
role: "",
|
||||
organizerStatus: "",
|
||||
profileStatus: false,
|
||||
};
|
||||
|
||||
export const userSlice = createSlice({
|
||||
name: "user",
|
||||
initialState,
|
||||
reducers: {
|
||||
// ✅ set all user details at once (e.g. after login)
|
||||
setUser: (state, action: PayloadAction<UserState>) => {
|
||||
return { ...action.payload };
|
||||
},
|
||||
|
||||
// ✅ update only selected fields (e.g. profile update)
|
||||
updateUser: (state, action: PayloadAction<Partial<UserState>>) => {
|
||||
return { ...state, ...action.payload };
|
||||
},
|
||||
|
||||
// ✅ clear user data (e.g. on logout)
|
||||
clearUser: () => initialState,
|
||||
},
|
||||
});
|
||||
|
||||
// Action creators
|
||||
export const { setUser, updateUser, clearUser } = userSlice.actions;
|
||||
|
||||
export default userSlice.reducer;
|
||||
Reference in New Issue
Block a user