mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-18 05:05:29 +00:00
chore: user user store
This commit is contained in:
parent
35785a1a28
commit
c71575faed
@ -16,7 +16,7 @@ function App() {
|
|||||||
try {
|
try {
|
||||||
await Promise.all([workspaceStore.fetchWorkspaceProfile(), workspaceStore.fetchWorkspaceSetting(), userStore.fetchCurrentUser()]);
|
await Promise.all([workspaceStore.fetchWorkspaceProfile(), workspaceStore.fetchWorkspaceSetting(), userStore.fetchCurrentUser()]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// do nth
|
// Do nothing.
|
||||||
}
|
}
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
})();
|
})();
|
||||||
|
@ -42,7 +42,7 @@ const CollectionSpace = () => {
|
|||||||
return [...shortcuts, shortcut];
|
return [...shortcuts, shortcut];
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// do nth
|
// Do nothing.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
document.title = `${collection.title} - Slash`;
|
document.title = `${collection.title} - Slash`;
|
||||||
|
@ -3,21 +3,23 @@ import React, { FormEvent, useEffect, useState } from "react";
|
|||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
|
import useNavigateTo from "@/hooks/useNavigateTo";
|
||||||
|
import useUserStore from "@/stores/v1/user";
|
||||||
import useWorkspaceStore from "@/stores/v1/workspace";
|
import useWorkspaceStore from "@/stores/v1/workspace";
|
||||||
import * as api from "../helpers/api";
|
import * as api from "../helpers/api";
|
||||||
import useLoading from "../hooks/useLoading";
|
import useLoading from "../hooks/useLoading";
|
||||||
|
|
||||||
const SignIn: React.FC = () => {
|
const SignIn: React.FC = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const navigateTo = useNavigateTo();
|
||||||
const workspaceStore = useWorkspaceStore();
|
const workspaceStore = useWorkspaceStore();
|
||||||
|
const userStore = useUserStore();
|
||||||
const [email, setEmail] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
const actionBtnLoadingState = useLoading(false);
|
const actionBtnLoadingState = useLoading(false);
|
||||||
const allowConfirm = email.length > 0 && password.length > 0;
|
const allowConfirm = email.length > 0 && password.length > 0;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
localStorage.removeItem("userId");
|
|
||||||
|
|
||||||
if (workspaceStore.profile.mode === "demo") {
|
if (workspaceStore.profile.mode === "demo") {
|
||||||
setEmail("steven@yourselfhosted.com");
|
setEmail("steven@yourselfhosted.com");
|
||||||
setPassword("secret");
|
setPassword("secret");
|
||||||
@ -44,8 +46,9 @@ const SignIn: React.FC = () => {
|
|||||||
actionBtnLoadingState.setLoading();
|
actionBtnLoadingState.setLoading();
|
||||||
const { data: user } = await api.signin(email, password);
|
const { data: user } = await api.signin(email, password);
|
||||||
if (user) {
|
if (user) {
|
||||||
localStorage.setItem("userId", `${user.id}`);
|
userStore.setCurrentUserId(user.id);
|
||||||
window.location.href = "/";
|
await userStore.fetchCurrentUser();
|
||||||
|
navigateTo("/");
|
||||||
} else {
|
} else {
|
||||||
toast.error("Signin failed");
|
toast.error("Signin failed");
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import { toast } from "react-hot-toast";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import useNavigateTo from "@/hooks/useNavigateTo";
|
import useNavigateTo from "@/hooks/useNavigateTo";
|
||||||
|
import useUserStore from "@/stores/v1/user";
|
||||||
import useWorkspaceStore from "@/stores/v1/workspace";
|
import useWorkspaceStore from "@/stores/v1/workspace";
|
||||||
import * as api from "../helpers/api";
|
import * as api from "../helpers/api";
|
||||||
import useLoading from "../hooks/useLoading";
|
import useLoading from "../hooks/useLoading";
|
||||||
@ -12,6 +13,7 @@ const SignUp: React.FC = () => {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const navigateTo = useNavigateTo();
|
const navigateTo = useNavigateTo();
|
||||||
const workspaceStore = useWorkspaceStore();
|
const workspaceStore = useWorkspaceStore();
|
||||||
|
const userStore = useUserStore();
|
||||||
const [email, setEmail] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
const [nickname, setNickname] = useState("");
|
const [nickname, setNickname] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
@ -19,8 +21,6 @@ const SignUp: React.FC = () => {
|
|||||||
const allowConfirm = email.length > 0 && nickname.length > 0 && password.length > 0;
|
const allowConfirm = email.length > 0 && nickname.length > 0 && password.length > 0;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
localStorage.removeItem("userId");
|
|
||||||
|
|
||||||
if (!workspaceStore.profile.enableSignup) {
|
if (!workspaceStore.profile.enableSignup) {
|
||||||
return navigateTo("/auth", {
|
return navigateTo("/auth", {
|
||||||
replace: true,
|
replace: true,
|
||||||
@ -53,8 +53,9 @@ const SignUp: React.FC = () => {
|
|||||||
actionBtnLoadingState.setLoading();
|
actionBtnLoadingState.setLoading();
|
||||||
const { data: user } = await api.signup(email, nickname, password);
|
const { data: user } = await api.signup(email, nickname, password);
|
||||||
if (user) {
|
if (user) {
|
||||||
localStorage.setItem("userId", `${user.id}`);
|
userStore.setCurrentUserId(user.id);
|
||||||
window.location.href = "/";
|
await userStore.fetchCurrentUser();
|
||||||
|
navigateTo("/");
|
||||||
} else {
|
} else {
|
||||||
toast.error("Signup failed");
|
toast.error("Signup failed");
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ interface UserState {
|
|||||||
getOrFetchUserById: (id: number) => Promise<User>;
|
getOrFetchUserById: (id: number) => Promise<User>;
|
||||||
getUserById: (id: number) => User;
|
getUserById: (id: number) => User;
|
||||||
getCurrentUser: () => User;
|
getCurrentUser: () => User;
|
||||||
|
setCurrentUserId: (id: number) => void;
|
||||||
createUser: (create: Partial<User>) => Promise<User>;
|
createUser: (create: Partial<User>) => Promise<User>;
|
||||||
patchUser: (userPatch: Partial<User>) => Promise<void>;
|
patchUser: (userPatch: Partial<User>) => Promise<void>;
|
||||||
deleteUser: (id: number) => Promise<void>;
|
deleteUser: (id: number) => Promise<void>;
|
||||||
@ -109,6 +110,12 @@ const useUserStore = create<UserState>()((set, get) => ({
|
|||||||
const currentUserId = get().currentUserId;
|
const currentUserId = get().currentUserId;
|
||||||
return userMap[currentUserId as number];
|
return userMap[currentUserId as number];
|
||||||
},
|
},
|
||||||
|
setCurrentUserId: (id: number) => {
|
||||||
|
localStorage.setItem("userId", `${id}`);
|
||||||
|
set({
|
||||||
|
currentUserId: id,
|
||||||
|
});
|
||||||
|
},
|
||||||
fetchUserSetting: async (userId: number) => {
|
fetchUserSetting: async (userId: number) => {
|
||||||
const userSetting = (
|
const userSetting = (
|
||||||
await userSettingServiceClient.getUserSetting({
|
await userSettingServiceClient.getUserSetting({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user