implement redux toolkit for manage globle state

This commit is contained in:
Jaanu423
2025-09-16 18:30:30 +05:30
parent 6ed8163d25
commit a931f886b2
7 changed files with 317 additions and 160 deletions
+13
View File
@@ -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;
+46
View File
@@ -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;