chore: fix user state loader

This commit is contained in:
Steven 2023-09-15 08:43:52 +08:00
parent 936fe5ac9d
commit 8824ee9b9d

View File

@ -1,12 +1,10 @@
import { lazy } from "react"; import { lazy } from "react";
import { createBrowserRouter, redirect } from "react-router-dom"; import { createBrowserRouter, redirect } from "react-router-dom";
import App from "@/App"; import App from "@/App";
import { isNullorUndefined } from "@/helpers/utils";
import Archived from "@/pages/Archived"; import Archived from "@/pages/Archived";
import DailyReview from "@/pages/DailyReview"; import DailyReview from "@/pages/DailyReview";
import ResourcesDashboard from "@/pages/ResourcesDashboard"; import ResourcesDashboard from "@/pages/ResourcesDashboard";
import Setting from "@/pages/Setting"; import Setting from "@/pages/Setting";
import store from "@/store";
import { initialGlobalState, initialUserState } from "@/store/module"; import { initialGlobalState, initialUserState } from "@/store/module";
const Root = lazy(() => import("@/layouts/Root")); const Root = lazy(() => import("@/layouts/Root"));
@ -35,6 +33,18 @@ const initialGlobalStateLoader = (() => {
}; };
})(); })();
const userStateLoader = async () => {
try {
const user = await initialUserState();
if (!user) {
return redirect("/explore");
}
} catch (error) {
// do nothing.
}
return null;
};
const router = createBrowserRouter([ const router = createBrowserRouter([
{ {
path: "/", path: "/",
@ -55,73 +65,43 @@ const router = createBrowserRouter([
{ {
path: "/", path: "/",
element: <Root />, element: <Root />,
loader: async () => {
try {
await initialUserState();
} catch (error) {
// do nth
}
return null;
},
children: [ children: [
{ {
path: "", path: "",
element: <Home />, element: <Home />,
loader: async () => { loader: userStateLoader,
const { user } = store.getState().user;
if (isNullorUndefined(user)) {
return redirect("/explore");
}
},
}, },
{ {
path: "explore", path: "explore",
element: <Explore />, element: <Explore />,
loader: async () => {
try {
await initialUserState();
} catch (error) {
// do nothing.
}
return null;
},
}, },
{ {
path: "review", path: "review",
element: <DailyReview />, element: <DailyReview />,
loader: async () => { loader: userStateLoader,
const { user } = store.getState().user;
if (isNullorUndefined(user)) {
return redirect("/explore");
}
},
}, },
{ {
path: "resources", path: "resources",
element: <ResourcesDashboard />, element: <ResourcesDashboard />,
loader: async () => { loader: userStateLoader,
const { user } = store.getState().user;
if (isNullorUndefined(user)) {
return redirect("/explore");
}
},
}, },
{ {
path: "archived", path: "archived",
element: <Archived />, element: <Archived />,
loader: async () => { loader: userStateLoader,
const { user } = store.getState().user;
if (isNullorUndefined(user)) {
return redirect("/explore");
}
},
}, },
{ {
path: "setting", path: "setting",
element: <Setting />, element: <Setting />,
loader: async () => { loader: userStateLoader,
const { user } = store.getState().user;
if (isNullorUndefined(user)) {
return redirect("/explore");
}
},
}, },
], ],
}, },