From 05908d2ca6379debfb0564fbc6250fd1de45d66b Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Fri, 2 Dec 2022 21:21:10 +0800 Subject: [PATCH] feat: realtime update for login user --- src/app/slices/auth.data.ts | 16 +++++++++++++++- src/common/hook/useStreaming/index.ts | 10 +++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/app/slices/auth.data.ts b/src/app/slices/auth.data.ts index 8e3c4fbf..e5caff1a 100644 --- a/src/app/slices/auth.data.ts +++ b/src/app/slices/auth.data.ts @@ -59,6 +59,20 @@ const authDataSlice = createSlice({ localStorage.setItem(KEY_REFRESH_TOKEN, refresh_token); localStorage.setItem(KEY_UID, `${uid}`); }, + updateLoginUser(state, { payload }: PayloadAction>) { + if (!state.user) return; + console.log("upppp", payload); + + const obj = { ...state.user, ...payload }; + Object.keys(obj).forEach(key => { + // @ts-ignore + if (obj[key] === undefined) { + // @ts-ignore + delete obj[key]; + } + }); + state.user = obj; + }, resetAuthData() { // remove local data localStorage.removeItem(KEY_EXPIRE); @@ -85,5 +99,5 @@ const authDataSlice = createSlice({ } }); -export const { updateInitialized, setAuthData, resetAuthData, updateToken } = authDataSlice.actions; +export const { updateInitialized, updateLoginUser, setAuthData, resetAuthData, updateToken } = authDataSlice.actions; export default authDataSlice.reducer; diff --git a/src/common/hook/useStreaming/index.ts b/src/common/hook/useStreaming/index.ts index 68a508ca..2e3db44d 100644 --- a/src/common/hook/useStreaming/index.ts +++ b/src/common/hook/useStreaming/index.ts @@ -16,10 +16,11 @@ import { updateMute } from "../../../app/slices/footprint"; import { updateUsersByLogs, updateUsersStatus } from "../../../app/slices/users"; -import { resetAuthData } from "../../../app/slices/auth.data"; +import { resetAuthData, updateLoginUser } from "../../../app/slices/auth.data"; import chatMessageHandler from "./chat.handler"; import store, { useAppDispatch, useAppSelector } from "../../../app/store"; import { ServerEvent, UsersStateEvent } from "../../../types/sse"; +import { isNull, omitBy } from "lodash"; const getQueryString = (params: { [key: string]: string }) => { const sp = new URLSearchParams(); @@ -130,6 +131,13 @@ export default function useStreaming() { const { logs } = data; console.info("sse users change logs", logs); dispatch(updateUsersByLogs(logs)); + // 特殊处理当前登录用户的更新 + logs.forEach((log) => { + const { uid, action, log_id, ...rest } = log; + if (uid === loginUid && action === 'update') { + dispatch(updateLoginUser(omitBy(rest, isNull))); + } + }); } break; case "user_settings":