chore: user user store

This commit is contained in:
Steven 2023-11-21 21:00:36 +08:00
parent 35785a1a28
commit c71575faed
5 changed files with 21 additions and 10 deletions

View File

@ -16,7 +16,7 @@ function App() {
try {
await Promise.all([workspaceStore.fetchWorkspaceProfile(), workspaceStore.fetchWorkspaceSetting(), userStore.fetchCurrentUser()]);
} catch (error) {
// do nth
// Do nothing.
}
setLoading(false);
})();

View File

@ -42,7 +42,7 @@ const CollectionSpace = () => {
return [...shortcuts, shortcut];
});
} catch (error) {
// do nth
// Do nothing.
}
}
document.title = `${collection.title} - Slash`;

View File

@ -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");
}

View File

@ -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");
}

View File

@ -14,6 +14,7 @@ interface UserState {
getOrFetchUserById: (id: number) => Promise<User>;
getUserById: (id: number) => User;
getCurrentUser: () => User;
setCurrentUserId: (id: number) => void;
createUser: (create: Partial<User>) => Promise<User>;
patchUser: (userPatch: Partial<User>) => Promise<void>;
deleteUser: (id: number) => Promise<void>;
@ -109,6 +110,12 @@ const useUserStore = create<UserState>()((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({