From 732202f68fab403b4826101deaaf26a5b3085a6a Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Thu, 7 Nov 2024 12:32:26 +0800 Subject: [PATCH] feat: user setting --- package.json | 2 +- public/locales/en/setting.json | 8 ++- public/locales/zh/setting.json | 6 +- src/app/services/user.ts | 11 +++- ...ailNotify.tsx => ServerMsgEmailNotify.tsx} | 0 .../setting/Overview/UserMsgEmailNotify.tsx | 57 +++++++++++++++++++ src/routes/setting/Overview/index.tsx | 8 ++- src/types/user.ts | 4 +- 8 files changed, 86 insertions(+), 10 deletions(-) rename src/routes/setting/Overview/{MsgEmailNotify.tsx => ServerMsgEmailNotify.tsx} (100%) create mode 100644 src/routes/setting/Overview/UserMsgEmailNotify.tsx diff --git a/package.json b/package.json index 886b9eef..ceb6e1cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vocechat-web", - "version": "0.7.49", + "version": "0.7.50", "homepage": "https://voce.chat", "dependencies": { "@metamask/onboarding": "^1.0.1", diff --git a/public/locales/en/setting.json b/public/locales/en/setting.json index 92c7fafa..b0b74ac2 100644 --- a/public/locales/en/setting.json +++ b/public/locales/en/setting.json @@ -61,11 +61,13 @@ "everyone": "Everyone", "invite": "Invitation Link Only" }, - "server_msg_notify": { + "user_msg_notify": { "title": "New Message Notification Through Email", + "desc": "you can control new message if send through email." + }, + "server_msg_notify": { + "title": "New Message Notification Through Email (Server)", "desc": "users can control new message if send through email, SMTP enabled first.", - "enable": "On", - "disable": "Off", "delay_5_min": "Every 5 mins", "delay_15_min": "Every 15 mins", "delay_1_hour": "Every 1 hour", diff --git a/public/locales/zh/setting.json b/public/locales/zh/setting.json index ccb68ba7..43d1cbf4 100644 --- a/public/locales/zh/setting.json +++ b/public/locales/zh/setting.json @@ -58,8 +58,12 @@ "everyone": "每个人", "invite": "邀请链接" }, - "server_msg_notify": { + "user_msg_notify": { "title": "邮件通知新消息", + "desc": "是否允许当前账号通过邮件接收新消息" + }, + "server_msg_notify": { + "title": "邮件通知新消息 (服务器)", "desc": "用户可以自定义是否通过邮件通知新消息,前提:SMTP 已配置并开启", "enable": "开启", "disable": "关闭", diff --git a/src/app/services/user.ts b/src/app/services/user.ts index 9109c835..09a525ed 100644 --- a/src/app/services/user.ts +++ b/src/app/services/user.ts @@ -21,6 +21,7 @@ import { RootState } from "../store"; import baseQuery from "./base.query"; import { onMessageSendStarted } from "./handlers"; import { encodeBase64 } from "@/utils"; +import { updateLoginUser } from "../slices/auth.data"; export const userApi = createApi({ reducerPath: "userApi", @@ -222,7 +223,15 @@ export const userApi = createApi({ url: `/user`, method: "PUT", body: data - }) + }), + async onQueryStarted(params, { dispatch, queryFulfilled }) { + try { + const { data } = await queryFulfilled; + dispatch(updateLoginUser(data)); + } catch (error) { + console.log("update login user failed", error); + } + } }), sendMsg: builder.mutation< number, diff --git a/src/routes/setting/Overview/MsgEmailNotify.tsx b/src/routes/setting/Overview/ServerMsgEmailNotify.tsx similarity index 100% rename from src/routes/setting/Overview/MsgEmailNotify.tsx rename to src/routes/setting/Overview/ServerMsgEmailNotify.tsx diff --git a/src/routes/setting/Overview/UserMsgEmailNotify.tsx b/src/routes/setting/Overview/UserMsgEmailNotify.tsx new file mode 100644 index 00000000..854716a6 --- /dev/null +++ b/src/routes/setting/Overview/UserMsgEmailNotify.tsx @@ -0,0 +1,57 @@ +import { useAppSelector } from "@/app/store"; +import SettingBlock from "@/components/SettingBlock"; +import { useTranslation } from "react-i18next"; +import { useGetSystemCommonQuery } from "@/app/services/server"; +import { useEffect } from "react"; +import toast from "react-hot-toast"; +import { shallowEqual } from "react-redux"; +import ServerVersionChecker from "@/components/ServerVersionChecker"; +import Toggle from "@/components/styled/Toggle"; +import useConfig from "@/hooks/useConfig"; +import { SMTPConfig } from "@/types/server"; +import { useUpdateInfoMutation } from "@/app/services/user"; + +type Props = {}; +const UserMsgEmailNotify = ({}: Props) => { + const [updateInfo, { isSuccess }] = useUpdateInfoMutation(); + const { values } = useConfig("smtp"); + const { t } = useTranslation("setting"); + const { t: ct } = useTranslation(); + const { refetch } = useGetSystemCommonQuery(); + const ServerMsgNotify = useAppSelector( + (store) => store.server.msg_smtp_notify_enable ?? false, + shallowEqual + ); + const msgNotify = useAppSelector( + (store) => store.authData.user?.msg_smtp_notify_enable ?? false, + shallowEqual + ); + useEffect(() => { + if (isSuccess) { + refetch(); + toast.success(ct("tip.update")); + } + }, [isSuccess]); + const toggleEnable = () => { + if (!msgNotify) { + // 检查下 SMTP 开关 + if (!(values as SMTPConfig).enabled) { + toast.error("Enable SMTP first!"); + return; + } + } + updateInfo({ msg_smtp_notify_enable: !msgNotify }); + }; + if (!ServerMsgNotify) return null; + return ( + + } + > + + ); +}; + +export default UserMsgEmailNotify; diff --git a/src/routes/setting/Overview/index.tsx b/src/routes/setting/Overview/index.tsx index c4e6e7c8..0ad05f28 100644 --- a/src/routes/setting/Overview/index.tsx +++ b/src/routes/setting/Overview/index.tsx @@ -14,9 +14,10 @@ import { shallowEqual } from "react-redux"; import OnlyAdminCreateGroup from "./OnlyAdminCreateGroup"; import OnlyAdminCanSeeChannelMembers from "./OnlyAdminSeeChannelMembers"; import EnableURLPreviewInMsg from "./URLPreview"; -import MsgNotify from "./MsgEmailNotify"; +import ServerMsgNotify from "./ServerMsgEmailNotify"; import GuestMode from "./GuestMode"; import WhoCanSignUpSetting from "./WhoCanSignUpSetting"; +import UserMsgEmailNotify from "./UserMsgEmailNotify"; export default function Overview() { const { t } = useTranslation("setting"); @@ -25,10 +26,11 @@ export default function Overview() { return (
+ {/* 全局性的邮件消息通知 */} + {isAdmin && } + {isAdmin && ( <> - {/* 全局性的邮件消息通知 */} - {/* 设置前端 url */}
diff --git a/src/types/user.ts b/src/types/user.ts index af26b8ac..cf23850d 100644 --- a/src/types/user.ts +++ b/src/types/user.ts @@ -61,7 +61,9 @@ export interface UserForAdminDTO extends Partial { id?: number; } export interface UserDTO - extends Partial> { + extends Partial< + Pick + > { password?: string; } export interface UserCreateDTO