mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: migrate update user
This commit is contained in:
@ -5,6 +5,7 @@ import { userServiceClient } from "@/grpcweb";
|
|||||||
import * as api from "@/helpers/api";
|
import * as api from "@/helpers/api";
|
||||||
import { useUserStore } from "@/store/module";
|
import { useUserStore } from "@/store/module";
|
||||||
import { UserNamePrefix } from "@/store/v1";
|
import { UserNamePrefix } from "@/store/v1";
|
||||||
|
import { RowStatus } from "@/types/proto/api/v2/common";
|
||||||
import { User_Role } from "@/types/proto/api/v2/user_service";
|
import { User_Role } from "@/types/proto/api/v2/user_service";
|
||||||
import { useTranslate } from "@/utils/i18n";
|
import { useTranslate } from "@/utils/i18n";
|
||||||
import showChangeMemberPasswordDialog from "../ChangeMemberPasswordDialog";
|
import showChangeMemberPasswordDialog from "../ChangeMemberPasswordDialog";
|
||||||
@ -84,9 +85,12 @@ const MemberSection = () => {
|
|||||||
style: "danger",
|
style: "danger",
|
||||||
dialogName: "archive-user-dialog",
|
dialogName: "archive-user-dialog",
|
||||||
onConfirm: async () => {
|
onConfirm: async () => {
|
||||||
await userStore.patchUser({
|
await userServiceClient.updateUser({
|
||||||
id: user.id,
|
user: {
|
||||||
rowStatus: "ARCHIVED",
|
name: `${UserNamePrefix}${user.username}`,
|
||||||
|
rowStatus: RowStatus.ARCHIVED,
|
||||||
|
},
|
||||||
|
updateMask: ["row_status"],
|
||||||
});
|
});
|
||||||
fetchUserList();
|
fetchUserList();
|
||||||
},
|
},
|
||||||
@ -94,9 +98,12 @@ const MemberSection = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleRestoreUserClick = async (user: User) => {
|
const handleRestoreUserClick = async (user: User) => {
|
||||||
await userStore.patchUser({
|
await userServiceClient.updateUser({
|
||||||
id: user.id,
|
user: {
|
||||||
rowStatus: "NORMAL",
|
name: `${UserNamePrefix}${user.username}`,
|
||||||
|
rowStatus: RowStatus.ACTIVE,
|
||||||
|
},
|
||||||
|
updateMask: ["row_status"],
|
||||||
});
|
});
|
||||||
fetchUserList();
|
fetchUserList();
|
||||||
};
|
};
|
||||||
|
@ -3,6 +3,8 @@ import { useEffect, useState } from "react";
|
|||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import { convertFileToBase64 } from "@/helpers/utils";
|
import { convertFileToBase64 } from "@/helpers/utils";
|
||||||
import { useUserStore } from "@/store/module";
|
import { useUserStore } from "@/store/module";
|
||||||
|
import { UserNamePrefix } from "@/store/v1";
|
||||||
|
import { User as UserPb } from "@/types/proto/api/v2/user_service";
|
||||||
import { useTranslate } from "@/utils/i18n";
|
import { useTranslate } from "@/utils/i18n";
|
||||||
import { generateDialog } from "./Dialog";
|
import { generateDialog } from "./Dialog";
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
@ -94,22 +96,29 @@ const UpdateAccountDialog: React.FC<Props> = ({ destroy }: Props) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const user = userStore.getState().user as User;
|
const user = userStore.getState().user as User;
|
||||||
const userPatch: UserPatch = {
|
const updateMask = [];
|
||||||
id: user.id,
|
|
||||||
};
|
|
||||||
if (!isEqual(user.avatarUrl, state.avatarUrl)) {
|
if (!isEqual(user.avatarUrl, state.avatarUrl)) {
|
||||||
userPatch.avatarUrl = state.avatarUrl;
|
updateMask.push("avatar_url");
|
||||||
}
|
}
|
||||||
if (!isEqual(user.nickname, state.nickname)) {
|
if (!isEqual(user.nickname, state.nickname)) {
|
||||||
userPatch.nickname = state.nickname;
|
updateMask.push("nickname");
|
||||||
}
|
}
|
||||||
if (!isEqual(user.username, state.username)) {
|
if (!isEqual(user.username, state.username)) {
|
||||||
userPatch.username = state.username;
|
updateMask.push("username");
|
||||||
}
|
}
|
||||||
if (!isEqual(user.email, state.email)) {
|
if (!isEqual(user.email, state.email)) {
|
||||||
userPatch.email = state.email;
|
updateMask.push("email");
|
||||||
}
|
}
|
||||||
await userStore.patchUser(userPatch);
|
await userStore.patchUser(
|
||||||
|
UserPb.fromPartial({
|
||||||
|
name: `${UserNamePrefix}${state.username}`,
|
||||||
|
id: user.id,
|
||||||
|
nickname: state.nickname,
|
||||||
|
email: state.email,
|
||||||
|
avatarUrl: state.avatarUrl,
|
||||||
|
}),
|
||||||
|
updateMask
|
||||||
|
);
|
||||||
toast.success(t("message.update-succeed"));
|
toast.success(t("message.update-succeed"));
|
||||||
handleCloseBtnClick();
|
handleCloseBtnClick();
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
@ -56,10 +56,6 @@ export function upsertUserSetting(upsert: UserSettingUpsert) {
|
|||||||
return axios.post<UserSetting>(`/api/v1/user/setting`, upsert);
|
return axios.post<UserSetting>(`/api/v1/user/setting`, upsert);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function patchUser(userPatch: UserPatch) {
|
|
||||||
return axios.patch<User>(`/api/v1/user/${userPatch.id}`, userPatch);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getAllMemos(memoFind?: MemoFind) {
|
export function getAllMemos(memoFind?: MemoFind) {
|
||||||
const queryList = [];
|
const queryList = [];
|
||||||
if (memoFind?.offset) {
|
if (memoFind?.offset) {
|
||||||
|
@ -3,6 +3,7 @@ import { userServiceClient } from "@/grpcweb";
|
|||||||
import * as api from "@/helpers/api";
|
import * as api from "@/helpers/api";
|
||||||
import storage from "@/helpers/storage";
|
import storage from "@/helpers/storage";
|
||||||
import { getSystemColorScheme } from "@/helpers/utils";
|
import { getSystemColorScheme } from "@/helpers/utils";
|
||||||
|
import { User as UserPb } from "@/types/proto/api/v2/user_service";
|
||||||
import store, { useAppSelector } from "..";
|
import store, { useAppSelector } from "..";
|
||||||
import { setAppearance, setLocale } from "../reducer/global";
|
import { setAppearance, setLocale } from "../reducer/global";
|
||||||
import { patchUser, setHost, setUser } from "../reducer/user";
|
import { patchUser, setHost, setUser } from "../reducer/user";
|
||||||
@ -106,10 +107,10 @@ export const useUserStore = () => {
|
|||||||
storage.set({ localSetting });
|
storage.set({ localSetting });
|
||||||
store.dispatch(patchUser({ localSetting }));
|
store.dispatch(patchUser({ localSetting }));
|
||||||
},
|
},
|
||||||
patchUser: async (userPatch: UserPatch): Promise<void> => {
|
patchUser: async (user: UserPb, updateMask: string[]): Promise<void> => {
|
||||||
await api.patchUser(userPatch);
|
await userServiceClient.updateUser({ user, updateMask });
|
||||||
// If the user is the current user and the username is changed, reload the page.
|
// If the user is the current user and the username is changed, reload the page.
|
||||||
if (userPatch.id === store.getState().user.user?.id && userPatch.username) {
|
if (user.id === store.getState().user.user?.id) {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user