Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

chore: migrate update user

Stevene5f660a00686
diff --git a/web/src/components/Settings/MemberSection.tsx b/web/src/components/Settings/MemberSection.tsx
index 6bb0eac33..a132b2237 100644
--- a/web/src/components/Settings/MemberSection.tsx
+++ b/web/src/components/Settings/MemberSection.tsx
@@ -5,6 +5,7 @@ import { userServiceClient } from "@/grpcweb";
import * as api from "@/helpers/api";
import { useUserStore } from "@/store/module";
import { UserNamePrefix } from "@/store/v1";
+import { RowStatus } from "@/types/proto/api/v2/common";
import { User_Role } from "@/types/proto/api/v2/user_service";
import { useTranslate } from "@/utils/i18n";
import showChangeMemberPasswordDialog from "../ChangeMemberPasswordDialog";
@@ -84,9 +85,12 @@ const MemberSection = () => {
style: "danger",
dialogName: "archive-user-dialog",
onConfirm: async () => {
- await userStore.patchUser({
- id: user.id,
- rowStatus: "ARCHIVED",
+ await userServiceClient.updateUser({
+ user: {
+ name: `${UserNamePrefix}${user.username}`,
+ rowStatus: RowStatus.ARCHIVED,
+ },
+ updateMask: ["row_status"],
});
fetchUserList();
},
@@ -94,9 +98,12 @@ const MemberSection = () => {
};
const handleRestoreUserClick = async (user: User) => {
- await userStore.patchUser({
- id: user.id,
- rowStatus: "NORMAL",
+ await userServiceClient.updateUser({
+ user: {
+ name: `${UserNamePrefix}${user.username}`,
+ rowStatus: RowStatus.ACTIVE,
+ },
+ updateMask: ["row_status"],
});
fetchUserList();
};
diff --git a/web/src/components/UpdateAccountDialog.tsx b/web/src/components/UpdateAccountDialog.tsx
index b0274b877..60c2768db 100644
--- a/web/src/components/UpdateAccountDialog.tsx
+++ b/web/src/components/UpdateAccountDialog.tsx
@@ -3,6 +3,8 @@ import { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import { convertFileToBase64 } from "@/helpers/utils";
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 { generateDialog } from "./Dialog";
import Icon from "./Icon";
@@ -94,22 +96,29 @@ const UpdateAccountDialog: React.FC<Props> = ({ destroy }: Props) => {
try {
const user = userStore.getState().user as User;
- const userPatch: UserPatch = {
- id: user.id,
- };
+ const updateMask = [];
if (!isEqual(user.avatarUrl, state.avatarUrl)) {
- userPatch.avatarUrl = state.avatarUrl;
+ updateMask.push("avatar_url");
}
if (!isEqual(user.nickname, state.nickname)) {
- userPatch.nickname = state.nickname;
+ updateMask.push("nickname");
}
if (!isEqual(user.username, state.username)) {
- userPatch.username = state.username;
+ updateMask.push("username");
}
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"));
handleCloseBtnClick();
} catch (error: any) {
diff --git a/web/src/helpers/api.ts b/web/src/helpers/api.ts
index 5c330eaf0..9aed265b1 100644
--- a/web/src/helpers/api.ts
+++ b/web/src/helpers/api.ts
@@ -56,10 +56,6 @@ export function upsertUserSetting(upsert: UserSettingUpsert) {
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) {
const queryList = [];
if (memoFind?.offset) {
diff --git a/web/src/store/module/user.ts b/web/src/store/module/user.ts
index 89848d1a9..394545707 100644
--- a/web/src/store/module/user.ts
+++ b/web/src/store/module/user.ts
@@ -3,6 +3,7 @@ import { userServiceClient } from "@/grpcweb";
import * as api from "@/helpers/api";
import storage from "@/helpers/storage";
import { getSystemColorScheme } from "@/helpers/utils";
+import { User as UserPb } from "@/types/proto/api/v2/user_service";
import store, { useAppSelector } from "..";
import { setAppearance, setLocale } from "../reducer/global";
import { patchUser, setHost, setUser } from "../reducer/user";
@@ -106,10 +107,10 @@ export const useUserStore = () => {
storage.set({ localSetting });
store.dispatch(patchUser({ localSetting }));
},
- patchUser: async (userPatch: UserPatch): Promise<void> => {
- await api.patchUser(userPatch);
+ patchUser: async (user: UserPb, updateMask: string[]): Promise<void> => {
+ await userServiceClient.updateUser({ user, updateMask });
// 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();
}
},