From 05251d9a68b586ae07cf91b12018a444ea5404d2 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 8 Sep 2021 16:16:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=A5=BD=E5=8F=8B=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared/i18n/langs/en-US/translation.json | 4 ++ shared/i18n/langs/zh-CN/translation.json | 4 ++ shared/index.tsx | 1 + shared/manager/ui.ts | 2 +- shared/model/friend.ts | 9 ++++ shared/redux/slices/user.ts | 7 +++ .../Content/Personal/Friends/FriendList.tsx | 51 +++++++++++++++++-- .../Main/Content/Personal/Friends/index.tsx | 4 +- 8 files changed, 76 insertions(+), 6 deletions(-) diff --git a/shared/i18n/langs/en-US/translation.json b/shared/i18n/langs/en-US/translation.json index 9328ade4..24d4fcbb 100644 --- a/shared/i18n/langs/en-US/translation.json +++ b/shared/i18n/langs/en-US/translation.json @@ -90,6 +90,7 @@ "kcefdbe2d": "Modify avatar success", "kd2c1a316": "Login", "kd2e5e126": "Panel Manage", + "kd417f93a": "Do you want to delete the other party from your friend list? Note: You will not disappear from the other party's friend list", "kd4ff36fa": "Search Friends", "kd637a30": "Group Invite Service", "kda67b115": "Unknown panel type", @@ -98,11 +99,14 @@ "kdd4c838c": "Jump to Group", "kdd6c18f8": "Service exception", "ke187440d": "Panel type cannot be empty", + "kecbd7449": "Delete", "ked2baf28": "Loading...", "ked5385d5": "Create Panel", "keda14478": "You are the group manager, leaving the group will cause the group to be dissolved", "kef25594f": "Nickname#0000", "kefc07278": "Back to login", + "kf02c6db": "Friend List", + "kf0d97e0b": "Friend deleted successfully", "kf15499b4": "Logout", "kf1eac01c": "Failed to load group information", "kf22210a": "Loading plugin list", diff --git a/shared/i18n/langs/zh-CN/translation.json b/shared/i18n/langs/zh-CN/translation.json index 64b761f1..fe80620d 100644 --- a/shared/i18n/langs/zh-CN/translation.json +++ b/shared/i18n/langs/zh-CN/translation.json @@ -90,6 +90,7 @@ "kcefdbe2d": "修改头像成功", "kd2c1a316": "登录", "kd2e5e126": "面板管理", + "kd417f93a": "是否要从自己的好友列表中删除对方? 注意:你不会从对方的好友列表消失", "kd4ff36fa": "查找好友", "kd637a30": "群组邀请服务", "kda67b115": "未知的面板类型", @@ -98,11 +99,14 @@ "kdd4c838c": "跳转到群组", "kdd6c18f8": "服务异常", "ke187440d": "面板类型不能为空", + "kecbd7449": "删除", "ked2baf28": "加载中...", "ked5385d5": "创建面板", "keda14478": "您是群组管理者,退出群组会导致解散群组", "kef25594f": "用户昵称#0000", "kefc07278": "返回登录", + "kf02c6db": "好友列表", + "kf0d97e0b": "好友删除成功", "kf15499b4": "退出登录", "kf1eac01c": "群组信息加载失败", "kf22210a": "正在加载插件列表", diff --git a/shared/index.tsx b/shared/index.tsx index 5ab3c31b..8f7f8d11 100644 --- a/shared/index.tsx +++ b/shared/index.tsx @@ -69,6 +69,7 @@ export { cancelFriendRequest, acceptFriendRequest, denyFriendRequest, + removeFriend, } from './model/friend'; export type { FriendRequest } from './model/friend'; export { diff --git a/shared/manager/ui.ts b/shared/manager/ui.ts index 850563cc..6d9709b7 100644 --- a/shared/manager/ui.ts +++ b/shared/manager/ui.ts @@ -12,7 +12,7 @@ export const [showToasts, setToasts] = * 一个封装方法, 用于直接抛出错误 * @param error 错误信息 */ -export function showErrorToasts(error: Error) { +export function showErrorToasts(error: unknown) { let msg = ''; if (error instanceof Error) { msg = error.message; diff --git a/shared/model/friend.ts b/shared/model/friend.ts index c577c1d1..45eb1945 100644 --- a/shared/model/friend.ts +++ b/shared/model/friend.ts @@ -50,3 +50,12 @@ export async function cancelFriendRequest(requestId: string): Promise { requestId, }); } + +/** + * 移除好友(单项) + */ +export async function removeFriend(friendUserId: string): Promise { + await request.post('/api/friend/removeFriend', { + friendUserId, + }); +} diff --git a/shared/redux/slices/user.ts b/shared/redux/slices/user.ts index 31781549..5d4915bc 100644 --- a/shared/redux/slices/user.ts +++ b/shared/redux/slices/user.ts @@ -44,6 +44,13 @@ const userSlice = createSlice({ state.friends.push(action.payload); }, + removeFriend(state, action: PayloadAction) { + const friendId = action.payload; + const index = state.friends.indexOf(friendId); + if (index >= 0) { + state.friends.splice(index, 1); + } + }, appendFriendRequest(state, action: PayloadAction) { if (state.friendRequests.some(({ _id }) => _id === action.payload._id)) { return; diff --git a/web/src/routes/Main/Content/Personal/Friends/FriendList.tsx b/web/src/routes/Main/Content/Personal/Friends/FriendList.tsx index 174fc40b..b9a79cdb 100644 --- a/web/src/routes/Main/Content/Personal/Friends/FriendList.tsx +++ b/web/src/routes/Main/Content/Personal/Friends/FriendList.tsx @@ -1,13 +1,19 @@ -import React from 'react'; +import React, { useCallback } from 'react'; import { createDMConverse, + removeFriend, + showAlert, + showErrorToasts, + showToasts, t, + useAppDispatch, useAppSelector, useAsyncRequest, + userActions, } from 'tailchat-shared'; import { UserListItem } from '@/components/UserListItem'; import { IconBtn } from '@/components/IconBtn'; -import { Tooltip } from 'antd'; +import { Dropdown, Menu, Tooltip } from 'antd'; import { useHistory } from 'react-router'; /** @@ -16,6 +22,7 @@ import { useHistory } from 'react-router'; export const FriendList: React.FC = React.memo(() => { const friends = useAppSelector((state) => state.user.friends); const history = useHistory(); + const dispatch = useAppDispatch(); const [, handleCreateConverse] = useAsyncRequest( async (targetId: string) => { @@ -25,9 +32,26 @@ export const FriendList: React.FC = React.memo(() => { [history] ); + const handleRemoveFriend = useCallback(async (targetId: string) => { + showAlert({ + message: t( + '是否要从自己的好友列表中删除对方? 注意:你不会从对方的好友列表消失' + ), + onConfirm: async () => { + try { + await removeFriend(targetId); + showToasts(t('好友删除成功'), 'success'); + dispatch(userActions.removeFriend(targetId)); + } catch (err) { + showErrorToasts(err); + } + }, + }); + }, []); + return (
-
好友列表
+
{t('好友列表')}
{friends.map((friendId) => ( { />
, +
+ + handleRemoveFriend(friendId)} + > + {t('删除')} + + + } + trigger={['click']} + placement="bottomRight" + > +
+ +
+
+
, ]} /> ))} diff --git a/web/src/routes/Main/Content/Personal/Friends/index.tsx b/web/src/routes/Main/Content/Personal/Friends/index.tsx index 6ad3d8c6..6413021b 100644 --- a/web/src/routes/Main/Content/Personal/Friends/index.tsx +++ b/web/src/routes/Main/Content/Personal/Friends/index.tsx @@ -20,7 +20,7 @@ export const FriendPanel: React.FC = React.memo(() => { return (
- + { 添加好友} + tab={{t('添加好友')}} key="4" >