feat: add persist config
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
import dayjs from "dayjs"
|
||||||
|
import duration from "dayjs/plugin/duration"
|
||||||
|
|
||||||
|
dayjs.extend(duration)
|
||||||
|
|
||||||
|
console.log("Global Dayjs plugins initialised.")
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
import "./dayjs"
|
||||||
@@ -1,16 +1,16 @@
|
|||||||
import { Outlet, Link } from "react-router-dom"
|
import { Outlet, Link } from "react-router-dom"
|
||||||
import { useMemo, useState } from "react"
|
import { useMemo } from "react"
|
||||||
import moment from "moment"
|
import dayjs from "dayjs"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main application component that serves as the root layout.
|
* Main application component that serves as the root layout.
|
||||||
* Uses React Router's Outlet to render child routes.
|
* Uses React Router's Outlet to render child routes.
|
||||||
*/
|
*/
|
||||||
export default function App() {
|
export default function HeroLayout() {
|
||||||
const today = useMemo(() => moment(), [])
|
const today = useMemo(() => dayjs(), [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-gray-50">
|
<div className="min-h-screen bg-gray-50 flex flex-col">
|
||||||
{/* Navigation Header */}
|
{/* Navigation Header */}
|
||||||
<header className="bg-white shadow-sm border-b">
|
<header className="bg-white shadow-sm border-b">
|
||||||
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
||||||
@@ -45,7 +45,7 @@ export default function App() {
|
|||||||
</header>
|
</header>
|
||||||
|
|
||||||
{/* Main Content Area */}
|
{/* Main Content Area */}
|
||||||
<main className="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
|
<main className="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8 flex-grow">
|
||||||
<div className="px-4 py-6 sm:px-0">
|
<div className="px-4 py-6 sm:px-0">
|
||||||
<Outlet />
|
<Outlet />
|
||||||
</div>
|
</div>
|
||||||
+9
-5
@@ -1,8 +1,10 @@
|
|||||||
import { StrictMode } from "react"
|
import { StrictMode } from "react"
|
||||||
import { createRoot } from "react-dom/client"
|
import { createRoot } from "react-dom/client"
|
||||||
import { Provider } from "react-redux"
|
import { Provider as ReduxProvider } from "react-redux"
|
||||||
|
import { PersistGate } from "redux-persist/integration/react"
|
||||||
import { RouterProvider } from "react-router-dom"
|
import { RouterProvider } from "react-router-dom"
|
||||||
import store from "@/store"
|
import "@/init"
|
||||||
|
import store, { persistor } from "@/store"
|
||||||
import router from "@/router"
|
import router from "@/router"
|
||||||
import "./index.css"
|
import "./index.css"
|
||||||
|
|
||||||
@@ -12,8 +14,10 @@ import "./index.css"
|
|||||||
*/
|
*/
|
||||||
createRoot(document.getElementById("root")!).render(
|
createRoot(document.getElementById("root")!).render(
|
||||||
<StrictMode>
|
<StrictMode>
|
||||||
<Provider store={store}>
|
<ReduxProvider store={store}>
|
||||||
<RouterProvider router={router} />
|
<PersistGate persistor={persistor} loading={null}>
|
||||||
</Provider>
|
<RouterProvider router={router} />
|
||||||
|
</PersistGate>
|
||||||
|
</ReduxProvider>
|
||||||
</StrictMode>,
|
</StrictMode>,
|
||||||
)
|
)
|
||||||
|
|||||||
+15
-8
@@ -1,9 +1,16 @@
|
|||||||
|
import { ComponentType } from "react"
|
||||||
import { createBrowserRouter } from "react-router-dom"
|
import { createBrowserRouter } from "react-router-dom"
|
||||||
import App from "@/App"
|
|
||||||
import ErrorPage from "@/components/error-page"
|
import ErrorPage from "@/components/error-page"
|
||||||
import HomePage from "@/page/home"
|
import HeroLayout from "@/layout/hero-layout"
|
||||||
import AboutPage from "@/page/about"
|
|
||||||
import ContactPage from "@/page/contact"
|
function lazy<T extends { default: ComponentType<unknown> }>(importer: () => Promise<T>) {
|
||||||
|
return async () => {
|
||||||
|
const module = await importer()
|
||||||
|
return {
|
||||||
|
Component: module.default,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main application router configuration using React Router v6.
|
* Main application router configuration using React Router v6.
|
||||||
@@ -13,20 +20,20 @@ const router = createBrowserRouter(
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
path: "/",
|
path: "/",
|
||||||
element: <App />,
|
element: <HeroLayout />,
|
||||||
errorElement: <ErrorPage />,
|
errorElement: <ErrorPage />,
|
||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
index: true,
|
index: true,
|
||||||
element: <HomePage />,
|
lazy: lazy(() => import("@/page/home")),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "about",
|
path: "about",
|
||||||
element: <AboutPage />,
|
lazy: lazy(() => import("@/page/about")),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "contact",
|
path: "contact",
|
||||||
element: <ContactPage />,
|
lazy: lazy(() => import("@/page/contact")),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
+38
-14
@@ -1,22 +1,46 @@
|
|||||||
import { configureStore } from "@reduxjs/toolkit"
|
import { configureStore, combineReducers } from "@reduxjs/toolkit"
|
||||||
import { useDispatch, useSelector } from "react-redux"
|
import { useDispatch, useSelector } from "react-redux"
|
||||||
import authReducer from "./auth-slice.ts"
|
import {
|
||||||
|
persistStore,
|
||||||
|
persistReducer,
|
||||||
|
FLUSH,
|
||||||
|
REHYDRATE,
|
||||||
|
PAUSE,
|
||||||
|
PERSIST,
|
||||||
|
PURGE,
|
||||||
|
REGISTER,
|
||||||
|
} from "redux-persist"
|
||||||
|
import storage from "redux-persist/lib/storage/session" // use session storage
|
||||||
|
// import storage from "redux-persist/lib/storage" // use local storage
|
||||||
|
import authReducer from "./auth-slice"
|
||||||
|
|
||||||
/**
|
const persistConfig = {
|
||||||
* The Redux store instance for the application.
|
key: "root",
|
||||||
*
|
storage,
|
||||||
* This store is configured using [`configureStore`](https://redux-toolkit.js.org/api/configureStore)
|
whitelist: ["auth"],
|
||||||
* from <code>@reduxjs/toolkit</code>. It combines various slice reducers into
|
// blacklist: ['department'],
|
||||||
* a single root redux.
|
}
|
||||||
*/
|
|
||||||
const store = configureStore({
|
const rootReducer = combineReducers({
|
||||||
reducer: {
|
auth: authReducer,
|
||||||
auth: authReducer,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const persistedReducer = persistReducer(persistConfig, rootReducer)
|
||||||
|
|
||||||
|
const store = configureStore({
|
||||||
|
reducer: persistedReducer,
|
||||||
|
middleware: (getDefaultMiddleware) =>
|
||||||
|
getDefaultMiddleware({
|
||||||
|
serializableCheck: {
|
||||||
|
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
export const persistor = persistStore(store)
|
||||||
|
|
||||||
export default store
|
export default store
|
||||||
export type RootState = ReturnType<typeof store.getState>
|
export type RootState = ReturnType<typeof rootReducer>
|
||||||
export type AppDispatch = typeof store.dispatch
|
export type AppDispatch = typeof store.dispatch
|
||||||
export type AppStore = typeof store
|
export type AppStore = typeof store
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user