From 531e04a4dfc4371083cda8cc8db967fd0bdfde43 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sun, 10 Jul 2022 15:52:21 +0800 Subject: [PATCH] feat: initial settings and current user --- src/app/Boundary.tsx | 19 +++--------- src/app/MustUser.tsx | 22 +++++++++++++ src/app/index.tsx | 46 +++++++++++++++++++++++----- src/components/FullScreenLoading.tsx | 18 +++++++++++ src/pages/manage/index.tsx | 9 +++++- src/store/settings.ts | 4 +++ src/store/state.ts | 9 +++++- src/store/user.ts | 7 ++++- 8 files changed, 108 insertions(+), 26 deletions(-) create mode 100644 src/app/MustUser.tsx create mode 100644 src/components/FullScreenLoading.tsx diff --git a/src/app/Boundary.tsx b/src/app/Boundary.tsx index 8a58cbce5..e88e6a077 100644 --- a/src/app/Boundary.tsx +++ b/src/app/Boundary.tsx @@ -5,11 +5,12 @@ import { Spinner, } from "@hope-ui/solid"; import { I18nContext } from "@solid-primitives/i18n"; -import { ErrorBoundary, JSX, Suspense } from "solid-js"; +import { ErrorBoundary, JSXElement, Suspense } from "solid-js"; +import { FullScreenLoading } from "~/components/FullScreenLoading"; import { i18n } from "./i18n"; import { globalStyles, theme } from "./theme"; -const Boundary = (props: { children: JSX.Element }) => { +const Boundary = (props: { children: JSXElement }) => { globalStyles(); return ( { - - - - } - > + }> {props.children} diff --git a/src/app/MustUser.tsx b/src/app/MustUser.tsx new file mode 100644 index 000000000..c3655b553 --- /dev/null +++ b/src/app/MustUser.tsx @@ -0,0 +1,22 @@ +import { Center } from "@hope-ui/solid"; +import { Route, Routes } from "solid-app-router"; +import { createEffect, JSXElement, lazy, Match, Switch } from "solid-js"; +import { FullScreenLoading } from "~/components/FullScreenLoading"; +import { err, state, State } from "~/store/state"; +import { resetUser } from "~/store/user"; + +const MustUser = (props: { children: JSXElement }) => { + resetUser(); + return ( + + + + + +
{err()}
+
+
+ ); +}; + +export { MustUser }; diff --git a/src/app/index.tsx b/src/app/index.tsx index c7e1da91b..9ca03dac3 100644 --- a/src/app/index.tsx +++ b/src/app/index.tsx @@ -1,11 +1,15 @@ -import { Progress, ProgressIndicator } from "@hope-ui/solid"; +import { Center, Progress, ProgressIndicator } from "@hope-ui/solid"; import { Route, Routes, useIsRouting } from "solid-app-router"; -import { Component, lazy, onCleanup } from "solid-js"; +import { Component, lazy, Match, onCleanup, Switch } from "solid-js"; import { Portal } from "solid-js/web"; import { Boundary } from "./Boundary"; import { useRouter } from "~/hooks/useRouter"; import { globalStyles } from "./theme"; import { bus } from "~/utils/bus"; +import { err, State, state } from "~/store/state"; +import { FullScreenLoading } from "~/components/FullScreenLoading"; +import { initSettings } from "~/store/settings"; +import { MustUser } from "./MustUser"; const Index = lazy(() => import("~/pages/index")); const Manage = lazy(() => import("~/pages/manage")); @@ -23,6 +27,7 @@ const App: Component = () => { onCleanup(() => { bus.off("to", onTo); }); + initSettings(); return ( @@ -39,12 +44,37 @@ const App: Component = () => { - - - - - - + + + + + + + } + /> + + + + } + /> + + } + > + + + + +
{err}
+
+
); }; diff --git a/src/components/FullScreenLoading.tsx b/src/components/FullScreenLoading.tsx new file mode 100644 index 000000000..aefdbb3df --- /dev/null +++ b/src/components/FullScreenLoading.tsx @@ -0,0 +1,18 @@ +import {Center,Spinner} from '@hope-ui/solid' +const FullScreenLoading = () => { + return ( +
+ +
+ ); +}; + +export { + FullScreenLoading, +} \ No newline at end of file diff --git a/src/pages/manage/index.tsx b/src/pages/manage/index.tsx index d0519ab73..11d6fb1f0 100644 --- a/src/pages/manage/index.tsx +++ b/src/pages/manage/index.tsx @@ -3,13 +3,20 @@ import { SwitchColorMode } from "~/components/SwitchColorMode"; import { SwitchLnaguage } from "~/components/SwitchLanguage"; import { useT } from "~/hooks/useT"; import { useTitle } from "~/hooks/useTitle"; +import { user } from "~/store/user"; import { Header } from "./Header"; import { SideMenu } from "./SideMenu"; import { side_menu_items } from "./sidemenu_items"; +import { UserMethods } from "~/types/user"; +import { useRouter } from "~/hooks/useRouter"; const Manage = () => { const t = useT(); - useTitle(()=>t("manage.title")); + useTitle(() => t("manage.title")); + const { to } = useRouter(); + if (UserMethods.is_guest(user()!)) { + to("/@login"); + } return (
diff --git a/src/store/settings.ts b/src/store/settings.ts index ed60f837c..dd866739a 100644 --- a/src/store/settings.ts +++ b/src/store/settings.ts @@ -1,5 +1,6 @@ import { Resp } from "~/types/resp"; import { r } from "~/utils/request"; +import { setErr, setState, State } from "./state"; const settings: Record = {}; export const initSettings = async () => { @@ -8,6 +9,9 @@ export const initSettings = async () => { Object.keys(resp.data).forEach((key) => { settings[key] = resp.data[key]; }); + setState(State.FetchingSettingsSuccess); + } else { + setErr(resp.message); } }; diff --git a/src/store/state.ts b/src/store/state.ts index 62844cff0..f8de14e34 100644 --- a/src/store/state.ts +++ b/src/store/state.ts @@ -3,6 +3,11 @@ import { createSignal } from "solid-js"; export enum State { Initial, // Initial state FetchingSettings, + FetchingSettingsError, + FetchingSettingsSuccess, + FetchingCurrentUser, + FetchingCurrentUserError, + FetchingCurrentUserSuccess, FetchingObj, FetchingObjs, TokenExpired, // Token expired @@ -10,6 +15,8 @@ export enum State { File, // File state } -const [state, setState] = createSignal(State.Initial); +const [state, setState] = createSignal(State.FetchingSettings); +const [err, setErr] = createSignal(""); export { state, setState }; +export { err, setErr }; diff --git a/src/store/user.ts b/src/store/user.ts index 1bd0c67e9..a377b56fe 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -2,12 +2,17 @@ import { createSignal } from "solid-js"; import { Resp } from "~/types/resp"; import { User } from "~/types/user"; import { r } from "~/utils/request"; +import { setState, State } from "./state"; const [user, setUser] = createSignal(); const resetUser = async () => { - const resp: Resp = (await r.get("/auth/current")).data; + setState(State.FetchingCurrentUser); + const resp: Resp = await r.get("/auth/current"); if (resp.code === 200) { setUser(resp.data); + setState(State.FetchingCurrentUserSuccess); + } else { + setState(State.FetchingCurrentUserError); } };