From 3b65a3cdc0d57b901885b68277df03c844d5e411 Mon Sep 17 00:00:00 2001 From: hyrious Date: Mon, 20 Jun 2022 12:52:45 +0800 Subject: [PATCH 1/2] feat(project): support delete account --- .../src/api-middleware/flatServer/index.ts | 4 +++ .../GeneralSettingPage/index.tsx | 30 +++++++++++++++++-- .../GeneralSettingPage/style.less | 3 +- packages/flat-i18n/locales/en.json | 4 ++- packages/flat-i18n/locales/zh-CN.json | 4 ++- .../src/api-middleware/flatServer/index.ts | 4 +++ .../GeneralSettingPage/index.less | 3 +- .../GeneralSettingPage/index.tsx | 30 +++++++++++++++++-- 8 files changed, 74 insertions(+), 8 deletions(-) diff --git a/desktop/renderer-app/src/api-middleware/flatServer/index.ts b/desktop/renderer-app/src/api-middleware/flatServer/index.ts index 178ddf51c97..bb475432ad1 100644 --- a/desktop/renderer-app/src/api-middleware/flatServer/index.ts +++ b/desktop/renderer-app/src/api-middleware/flatServer/index.ts @@ -681,3 +681,7 @@ export async function removeBinding(target: LoginPlatform): Promise { + await post<{}, {}>("user/deleteAccount", {}); +} diff --git a/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/index.tsx b/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/index.tsx index c9b14971151..1e8306cdece 100644 --- a/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/index.tsx +++ b/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/index.tsx @@ -1,7 +1,7 @@ import "./style.less"; import React, { useContext, useEffect, useState } from "react"; -import { Checkbox, Input, message, Radio, RadioChangeEvent } from "antd"; +import { Button, Checkbox, Input, message, Modal, Radio, RadioChangeEvent } from "antd"; import { observer } from "mobx-react-lite"; import { useTranslation } from "react-i18next"; import { AppearancePicker, FlatPrefersColorScheme } from "flat-components"; @@ -10,12 +10,14 @@ import { UserSettingLayoutContainer } from "../UserSettingLayoutContainer"; import { ipcSyncByApp, ipcAsyncByApp } from "../../../utils/ipc"; import { ConfigStoreContext, GlobalStoreContext } from "../../../components/StoreProvider"; import { useSafePromise } from "../../../utils/hooks/lifecycle"; -import { loginCheck, rename } from "../../../api-middleware/flatServer"; +import { deleteAccount, loginCheck, rename } from "../../../api-middleware/flatServer"; import { ConfirmButtons } from "./ConfirmButtons"; import { UploadAvatar, uploadAvatar } from "./UploadAvatar"; import { useBindingList } from "./binding"; import { BindWeChat } from "./binding/WeChat"; import { BindGitHub } from "./binding/GitHub"; +import { RouteNameType, usePushHistory } from "../../../utils/routes"; +import { errorTips } from "../../../components/Tips/ErrorTips"; enum SelectLanguage { Chinese, @@ -24,6 +26,7 @@ enum SelectLanguage { export const GeneralSettingPage = observer(function GeneralSettingPage() { const sp = useSafePromise(); + const pushHistory = usePushHistory(); const { t, i18n } = useTranslation(); const [openAtLogin, setOpenAtLogin] = useState(false); const configStore = useContext(ConfigStoreContext); @@ -81,6 +84,21 @@ export const GeneralSettingPage = observer(function GeneralSettingPage() { configStore.updatePrefersColorScheme(prefersColorScheme); }; + function removeAccount(): void { + Modal.confirm({ + content: t("confirm-delete-account"), + onOk: async () => { + try { + await sp(deleteAccount()); + globalStore.updateUserInfo(null); + pushHistory(RouteNameType.LoginPage); + } catch (err) { + errorTips(err); + } + }, + }); + } + return (
@@ -146,6 +164,14 @@ export const GeneralSettingPage = observer(function GeneralSettingPage() { defaultValue={configStore.prefersColorScheme} />
+
+ {t("delete-account")} +
+ +
+
); diff --git a/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/style.less b/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/style.less index 166cdc564ff..78fa355c8ad 100644 --- a/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/style.less +++ b/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/style.less @@ -108,7 +108,8 @@ } } -.general-setting-binding-profile { +.general-setting-binding-profile, +.general-setting-user-account { padding-top: 12px; } diff --git a/packages/flat-i18n/locales/en.json b/packages/flat-i18n/locales/en.json index ac8de6e73c7..1138471bb31 100644 --- a/packages/flat-i18n/locales/en.json +++ b/packages/flat-i18n/locales/en.json @@ -465,5 +465,7 @@ "is-bind": "is bind", "not-bind": "not bind", "unbind-confirm": "Are you sure to unbind?", - "unbind-success": "Unbind success" + "unbind-success": "Unbind success", + "delete-account": "Delete Account", + "confirm-delete-account": "Are you sure to delete your account?" } diff --git a/packages/flat-i18n/locales/zh-CN.json b/packages/flat-i18n/locales/zh-CN.json index b5c81dab5eb..7dc610085eb 100644 --- a/packages/flat-i18n/locales/zh-CN.json +++ b/packages/flat-i18n/locales/zh-CN.json @@ -465,5 +465,7 @@ "is-bind": "已绑定", "not-bind": "未绑定", "unbind-confirm": "确定解除绑定吗?", - "unbind-success": "解绑成功" + "unbind-success": "解绑成功", + "delete-account": "账号注销", + "confirm-delete-account": "确定注销账号吗?" } diff --git a/web/flat-web/src/api-middleware/flatServer/index.ts b/web/flat-web/src/api-middleware/flatServer/index.ts index 5af0deb3a73..447bb6fabc3 100644 --- a/web/flat-web/src/api-middleware/flatServer/index.ts +++ b/web/flat-web/src/api-middleware/flatServer/index.ts @@ -681,3 +681,7 @@ export async function removeBinding(target: LoginPlatform): Promise { + await post<{}, {}>("user/deleteAccount", {}); +} diff --git a/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.less b/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.less index 8d08c339134..5f985d068fe 100644 --- a/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.less +++ b/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.less @@ -112,7 +112,8 @@ padding-bottom: 3px; } -.general-setting-binding-profile { +.general-setting-binding-profile, +.general-setting-user-account { padding-top: 12px; } diff --git a/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.tsx b/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.tsx index e51f5e12c10..11073fa680e 100644 --- a/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.tsx +++ b/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.tsx @@ -4,19 +4,21 @@ import "./index.less"; import React, { useContext, useState } from "react"; import { observer } from "mobx-react-lite"; -import { Checkbox, Input, message, Radio } from "antd"; +import { Button, Checkbox, Input, message, Modal, Radio } from "antd"; import { FlatPrefersColorScheme, AppearancePicker } from "flat-components"; import { UserSettingLayoutContainer } from "../UserSettingLayoutContainer"; import { useTranslation } from "react-i18next"; import { ConfigStoreContext, GlobalStoreContext } from "../../../components/StoreProvider"; import { useSafePromise } from "../../../utils/hooks/lifecycle"; -import { loginCheck, rename } from "../../../api-middleware/flatServer"; +import { deleteAccount, loginCheck, rename } from "../../../api-middleware/flatServer"; import { ConfirmButtons } from "./ConfirmButtons"; import { uploadAvatar, UploadAvatar } from "./UploadAvatar"; import { BindWeChat } from "./binding/WeChat"; import { useBindingList } from "./binding"; import { BindGitHub } from "./binding/GitHub"; +import { errorTips } from "../../../components/Tips/ErrorTips"; +import { RouteNameType, usePushHistory } from "../../../utils/routes"; enum SelectLanguage { Chinese, @@ -28,6 +30,7 @@ export const GeneralSettingPage = observer(function GeneralSettingPage() { const configStore = useContext(ConfigStoreContext); const sp = useSafePromise(); + const pushHistory = usePushHistory(); const { t, i18n } = useTranslation(); const [name, setName] = useState(globalStore.userName || ""); @@ -65,6 +68,21 @@ export const GeneralSettingPage = observer(function GeneralSettingPage() { configStore.updatePrefersColorScheme(prefersColorScheme); }; + function removeAccount(): void { + Modal.confirm({ + content: t("confirm-delete-account"), + onOk: async () => { + try { + await sp(deleteAccount()); + globalStore.updateUserInfo(null); + pushHistory(RouteNameType.LoginPage); + } catch (err) { + errorTips(err); + } + }, + }); + } + return (
@@ -134,6 +152,14 @@ export const GeneralSettingPage = observer(function GeneralSettingPage() { {t("turn-on-device-test")}
+
+ {t("delete-account")} +
+ +
+
); From 3209033064afc68d6e59690d426a5b125bec1df5 Mon Sep 17 00:00:00 2001 From: hyrious Date: Mon, 20 Jun 2022 13:13:49 +0800 Subject: [PATCH 2/2] validate before deleting account --- .../src/api-middleware/flatServer/index.ts | 8 ++++++++ .../UserSettingPage/GeneralSettingPage/index.tsx | 14 ++++++++++++-- packages/flat-i18n/locales/en.json | 3 ++- packages/flat-i18n/locales/zh-CN.json | 3 ++- .../src/api-middleware/flatServer/index.ts | 8 ++++++++ .../UserSettingPage/GeneralSettingPage/index.tsx | 14 ++++++++++++-- 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/desktop/renderer-app/src/api-middleware/flatServer/index.ts b/desktop/renderer-app/src/api-middleware/flatServer/index.ts index bb475432ad1..b34e9b824ed 100644 --- a/desktop/renderer-app/src/api-middleware/flatServer/index.ts +++ b/desktop/renderer-app/src/api-middleware/flatServer/index.ts @@ -682,6 +682,14 @@ export async function removeBinding(target: LoginPlatform): Promise { + return await post<{}, DeleteAccountValidateResult>("user/deleteAccount/validate", {}); +} + export async function deleteAccount(): Promise { await post<{}, {}>("user/deleteAccount", {}); } diff --git a/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/index.tsx b/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/index.tsx index 1e8306cdece..efaeee4ad15 100644 --- a/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/index.tsx +++ b/desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/index.tsx @@ -10,7 +10,12 @@ import { UserSettingLayoutContainer } from "../UserSettingLayoutContainer"; import { ipcSyncByApp, ipcAsyncByApp } from "../../../utils/ipc"; import { ConfigStoreContext, GlobalStoreContext } from "../../../components/StoreProvider"; import { useSafePromise } from "../../../utils/hooks/lifecycle"; -import { deleteAccount, loginCheck, rename } from "../../../api-middleware/flatServer"; +import { + deleteAccount, + deleteAccountValidate, + loginCheck, + rename, +} from "../../../api-middleware/flatServer"; import { ConfirmButtons } from "./ConfirmButtons"; import { UploadAvatar, uploadAvatar } from "./UploadAvatar"; import { useBindingList } from "./binding"; @@ -84,7 +89,12 @@ export const GeneralSettingPage = observer(function GeneralSettingPage() { configStore.updatePrefersColorScheme(prefersColorScheme); }; - function removeAccount(): void { + async function removeAccount(): Promise { + const { alreadyJoinedRoomCount } = await sp(deleteAccountValidate()); + if (alreadyJoinedRoomCount > 0) { + message.error(t("quit-all-rooms-before-delete-account")); + return; + } Modal.confirm({ content: t("confirm-delete-account"), onOk: async () => { diff --git a/packages/flat-i18n/locales/en.json b/packages/flat-i18n/locales/en.json index 1138471bb31..9532c1eba43 100644 --- a/packages/flat-i18n/locales/en.json +++ b/packages/flat-i18n/locales/en.json @@ -467,5 +467,6 @@ "unbind-confirm": "Are you sure to unbind?", "unbind-success": "Unbind success", "delete-account": "Delete Account", - "confirm-delete-account": "Are you sure to delete your account?" + "confirm-delete-account": "Are you sure to delete your account?", + "quit-all-rooms-before-delete-account": "Please quit all rooms before deleting account." } diff --git a/packages/flat-i18n/locales/zh-CN.json b/packages/flat-i18n/locales/zh-CN.json index 7dc610085eb..5679ae5fc19 100644 --- a/packages/flat-i18n/locales/zh-CN.json +++ b/packages/flat-i18n/locales/zh-CN.json @@ -467,5 +467,6 @@ "unbind-confirm": "确定解除绑定吗?", "unbind-success": "解绑成功", "delete-account": "账号注销", - "confirm-delete-account": "确定注销账号吗?" + "confirm-delete-account": "确定注销账号吗?", + "quit-all-rooms-before-delete-account": "请先退出所有房间" } diff --git a/web/flat-web/src/api-middleware/flatServer/index.ts b/web/flat-web/src/api-middleware/flatServer/index.ts index 447bb6fabc3..ae48205805a 100644 --- a/web/flat-web/src/api-middleware/flatServer/index.ts +++ b/web/flat-web/src/api-middleware/flatServer/index.ts @@ -682,6 +682,14 @@ export async function removeBinding(target: LoginPlatform): Promise { + return await post<{}, DeleteAccountValidateResult>("user/deleteAccount/validate", {}); +} + export async function deleteAccount(): Promise { await post<{}, {}>("user/deleteAccount", {}); } diff --git a/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.tsx b/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.tsx index 11073fa680e..6edb29750e1 100644 --- a/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.tsx +++ b/web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.tsx @@ -11,7 +11,12 @@ import { useTranslation } from "react-i18next"; import { ConfigStoreContext, GlobalStoreContext } from "../../../components/StoreProvider"; import { useSafePromise } from "../../../utils/hooks/lifecycle"; -import { deleteAccount, loginCheck, rename } from "../../../api-middleware/flatServer"; +import { + deleteAccount, + deleteAccountValidate, + loginCheck, + rename, +} from "../../../api-middleware/flatServer"; import { ConfirmButtons } from "./ConfirmButtons"; import { uploadAvatar, UploadAvatar } from "./UploadAvatar"; import { BindWeChat } from "./binding/WeChat"; @@ -68,7 +73,12 @@ export const GeneralSettingPage = observer(function GeneralSettingPage() { configStore.updatePrefersColorScheme(prefersColorScheme); }; - function removeAccount(): void { + async function removeAccount(): Promise { + const { alreadyJoinedRoomCount } = await sp(deleteAccountValidate()); + if (alreadyJoinedRoomCount > 0) { + message.error(t("quit-all-rooms-before-delete-account")); + return; + } Modal.confirm({ content: t("confirm-delete-account"), onOk: async () => {