pig update: decentralization, format date time
All checks were successful
Build and Release / release (push) Successful in 27s

This commit is contained in:
2026-04-20 00:47:41 +07:00
parent 5c622902ad
commit 49b99289bb
16 changed files with 779 additions and 240 deletions

View File

@@ -3,6 +3,13 @@
import { useRef } from 'react';
import { Provider } from 'react-redux';
import { store } from './store';
import { useRestoreUserData } from '@/hooks/useRestoreUserData';
function RestoreUserDataComponent() {
useRestoreUserData();
return null;
}
export default function StoreProvider({
children,
@@ -10,5 +17,10 @@ export default function StoreProvider({
children: React.ReactNode;
}) {
const storeRef = useRef(store);
return <Provider store={storeRef.current}>{children}</Provider>;
return (
<Provider store={storeRef.current}>
<RestoreUserDataComponent />
{children}
</Provider>
);
}

View File

@@ -1,5 +1,6 @@
import { UserData } from '@/interface/user';
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { getUserFromCookie } from '@/lib/cookieStorage';
const getStoredApplication = () => {
if (typeof window !== "undefined") {
@@ -9,15 +10,24 @@ const getStoredApplication = () => {
return null;
};
const getStoredUserData = (): UserData | null => {
if (typeof window === "undefined") {
return null;
}
return getUserFromCookie();
};
interface UserState {
data: UserData | null;
isAuthenticated: boolean;
selectedApplication: any | null;
}
const storedUserData = getStoredUserData();
const initialState: UserState = {
data: null,
isAuthenticated: false,
data: storedUserData,
isAuthenticated: Boolean(storedUserData),
selectedApplication: getStoredApplication(),
};
@@ -29,6 +39,10 @@ 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;
if (typeof window !== "undefined") {
@@ -44,5 +58,5 @@ const userSlice = createSlice({
},
});
export const { setUserData, setSelectedApplication, clearSelectedApplication } = userSlice.actions;
export const { setUserData, clearUserData, setSelectedApplication, clearSelectedApplication } = userSlice.actions;
export default userSlice.reducer;

View File

@@ -1,4 +1,5 @@
import { configureStore } from '@reduxjs/toolkit';
import { useDispatch, useSelector, TypedUseSelectorHook } from 'react-redux'; // Thêm dòng này
import userReducer from './features/userSlice';
export const store = configureStore({
@@ -8,4 +9,7 @@ export const store = configureStore({
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;