application page
This commit is contained in:
@@ -1,13 +1,25 @@
|
||||
import { UserData } from '@/interface/user';
|
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||
|
||||
// Hàm helper để đọc dữ liệu an toàn từ storage
|
||||
const getStoredApplication = () => {
|
||||
if (typeof window !== "undefined") {
|
||||
const saved = sessionStorage.getItem('selected_application');
|
||||
return saved ? JSON.parse(saved) : null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
interface UserState {
|
||||
data: UserData | null;
|
||||
isAuthenticated: boolean;
|
||||
selectedApplication: any | null;
|
||||
}
|
||||
|
||||
const initialState: UserState = {
|
||||
data: null,
|
||||
isAuthenticated: false,
|
||||
selectedApplication: getStoredApplication(), // Khởi tạo từ storage
|
||||
};
|
||||
|
||||
const userSlice = createSlice({
|
||||
@@ -18,12 +30,21 @@ const userSlice = createSlice({
|
||||
state.data = action.payload;
|
||||
state.isAuthenticated = true;
|
||||
},
|
||||
clearUserData: (state) => {
|
||||
state.data = null;
|
||||
state.isAuthenticated = false;
|
||||
setSelectedApplication: (state, action: PayloadAction<any>) => {
|
||||
state.selectedApplication = action.payload;
|
||||
// Lưu vào sessionStorage để khi reload trang không bị mất
|
||||
if (typeof window !== "undefined") {
|
||||
sessionStorage.setItem('selected_application', JSON.stringify(action.payload));
|
||||
}
|
||||
},
|
||||
clearSelectedApplication: (state) => {
|
||||
state.selectedApplication = null;
|
||||
if (typeof window !== "undefined") {
|
||||
sessionStorage.removeItem('selected_application');
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const { setUserData, clearUserData } = userSlice.actions;
|
||||
export const { setUserData, setSelectedApplication, clearSelectedApplication } = userSlice.actions;
|
||||
export default userSlice.reducer;
|
||||
Reference in New Issue
Block a user