From c71575faed0966eb185bb708869a7acb01cb233b Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 21 Nov 2023 21:00:36 +0800 Subject: [PATCH] chore: user user store --- frontend/web/src/App.tsx | 2 +- frontend/web/src/pages/CollectionSpace.tsx | 2 +- frontend/web/src/pages/SignIn.tsx | 11 +++++++---- frontend/web/src/pages/SignUp.tsx | 9 +++++---- frontend/web/src/stores/v1/user.ts | 7 +++++++ 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/frontend/web/src/App.tsx b/frontend/web/src/App.tsx index b7dacbb..17b9bde 100644 --- a/frontend/web/src/App.tsx +++ b/frontend/web/src/App.tsx @@ -16,7 +16,7 @@ function App() { try { await Promise.all([workspaceStore.fetchWorkspaceProfile(), workspaceStore.fetchWorkspaceSetting(), userStore.fetchCurrentUser()]); } catch (error) { - // do nth + // Do nothing. } setLoading(false); })(); diff --git a/frontend/web/src/pages/CollectionSpace.tsx b/frontend/web/src/pages/CollectionSpace.tsx index 444a911..2827ece 100644 --- a/frontend/web/src/pages/CollectionSpace.tsx +++ b/frontend/web/src/pages/CollectionSpace.tsx @@ -42,7 +42,7 @@ const CollectionSpace = () => { return [...shortcuts, shortcut]; }); } catch (error) { - // do nth + // Do nothing. } } document.title = `${collection.title} - Slash`; diff --git a/frontend/web/src/pages/SignIn.tsx b/frontend/web/src/pages/SignIn.tsx index 9e07708..1d02819 100644 --- a/frontend/web/src/pages/SignIn.tsx +++ b/frontend/web/src/pages/SignIn.tsx @@ -3,21 +3,23 @@ import React, { FormEvent, useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; +import useNavigateTo from "@/hooks/useNavigateTo"; +import useUserStore from "@/stores/v1/user"; import useWorkspaceStore from "@/stores/v1/workspace"; import * as api from "../helpers/api"; import useLoading from "../hooks/useLoading"; const SignIn: React.FC = () => { const { t } = useTranslation(); + const navigateTo = useNavigateTo(); const workspaceStore = useWorkspaceStore(); + const userStore = useUserStore(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const actionBtnLoadingState = useLoading(false); const allowConfirm = email.length > 0 && password.length > 0; useEffect(() => { - localStorage.removeItem("userId"); - if (workspaceStore.profile.mode === "demo") { setEmail("steven@yourselfhosted.com"); setPassword("secret"); @@ -44,8 +46,9 @@ const SignIn: React.FC = () => { actionBtnLoadingState.setLoading(); const { data: user } = await api.signin(email, password); if (user) { - localStorage.setItem("userId", `${user.id}`); - window.location.href = "/"; + userStore.setCurrentUserId(user.id); + await userStore.fetchCurrentUser(); + navigateTo("/"); } else { toast.error("Signin failed"); } diff --git a/frontend/web/src/pages/SignUp.tsx b/frontend/web/src/pages/SignUp.tsx index 6f33bee..2982a73 100644 --- a/frontend/web/src/pages/SignUp.tsx +++ b/frontend/web/src/pages/SignUp.tsx @@ -4,6 +4,7 @@ import { toast } from "react-hot-toast"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import useNavigateTo from "@/hooks/useNavigateTo"; +import useUserStore from "@/stores/v1/user"; import useWorkspaceStore from "@/stores/v1/workspace"; import * as api from "../helpers/api"; import useLoading from "../hooks/useLoading"; @@ -12,6 +13,7 @@ const SignUp: React.FC = () => { const { t } = useTranslation(); const navigateTo = useNavigateTo(); const workspaceStore = useWorkspaceStore(); + const userStore = useUserStore(); const [email, setEmail] = useState(""); const [nickname, setNickname] = useState(""); const [password, setPassword] = useState(""); @@ -19,8 +21,6 @@ const SignUp: React.FC = () => { const allowConfirm = email.length > 0 && nickname.length > 0 && password.length > 0; useEffect(() => { - localStorage.removeItem("userId"); - if (!workspaceStore.profile.enableSignup) { return navigateTo("/auth", { replace: true, @@ -53,8 +53,9 @@ const SignUp: React.FC = () => { actionBtnLoadingState.setLoading(); const { data: user } = await api.signup(email, nickname, password); if (user) { - localStorage.setItem("userId", `${user.id}`); - window.location.href = "/"; + userStore.setCurrentUserId(user.id); + await userStore.fetchCurrentUser(); + navigateTo("/"); } else { toast.error("Signup failed"); } diff --git a/frontend/web/src/stores/v1/user.ts b/frontend/web/src/stores/v1/user.ts index b48c7bd..e270348 100644 --- a/frontend/web/src/stores/v1/user.ts +++ b/frontend/web/src/stores/v1/user.ts @@ -14,6 +14,7 @@ interface UserState { getOrFetchUserById: (id: number) => Promise; getUserById: (id: number) => User; getCurrentUser: () => User; + setCurrentUserId: (id: number) => void; createUser: (create: Partial) => Promise; patchUser: (userPatch: Partial) => Promise; deleteUser: (id: number) => Promise; @@ -109,6 +110,12 @@ const useUserStore = create()((set, get) => ({ const currentUserId = get().currentUserId; return userMap[currentUserId as number]; }, + setCurrentUserId: (id: number) => { + localStorage.setItem("userId", `${id}`); + set({ + currentUserId: id, + }); + }, fetchUserSetting: async (userId: number) => { const userSetting = ( await userSettingServiceClient.getUserSetting({