feat: users online status

This commit is contained in:
zerosoul
2022-02-19 15:57:25 +08:00
parent f96ca44841
commit 656df7c670
10 changed files with 79 additions and 19 deletions
+26
View File
@@ -0,0 +1,26 @@
import { createSlice } from "@reduxjs/toolkit";
const initialState = [];
const contactsSlice = createSlice({
name: `contacts`,
initialState,
reducers: {
setContacts(state, action) {
console.log("set Contacts store", state);
const contacts = action.payload || [];
return contacts;
},
updateUsersStatus(state, action) {
const onlines = action.payload;
onlines.forEach((item) => {
const { uid, online = false } = item;
const curr = state.find(({ uid: id }) => id == uid);
console.log("update user status", curr, online);
if (curr) {
curr.online = online;
}
});
},
},
});
export const { setContacts, updateUsersStatus } = contactsSlice.actions;
export default contactsSlice.reducer;
+25
View File
@@ -13,6 +13,7 @@ import {
import authDataReducer from "./slices/auth.data";
import uiReducer from "./slices/ui";
import channelsReducer from "./slices/channels";
import contactsReducer from "./slices/contacts";
import channelMsgReducer from "./slices/message.channel";
import userMsgReducer from "./slices/message.user";
import { authApi } from "./services/auth";
@@ -28,6 +29,7 @@ const persistedReducer = persistReducer(
persistConfig,
combineReducers({
ui: uiReducer,
contacts: contactsReducer,
channels: channelsReducer,
userMsg: userMsgReducer,
channelMsg: channelMsgReducer,
@@ -53,4 +55,27 @@ const store = configureStore({
),
});
setupListeners(store.dispatch);
// export function swapToUserStore(userId) {
// console.log({ userId });
// const newConfig = {
// ...persistConfig,
// keyPrefix: userId,
// };
// store.replaceReducer(
// persistReducer(
// newConfig,
// combineReducers({
// ui: uiReducer,
// channels: channelsReducer,
// userMsg: userMsgReducer,
// channelMsg: channelMsgReducer,
// authData: authDataReducer,
// [authApi.reducerPath]: authApi.reducer,
// [contactApi.reducerPath]: contactApi.reducer,
// [channelApi.reducerPath]: channelApi.reducer,
// [serverApi.reducerPath]: serverApi.reducer,
// })
// )
// );
// }
export default store;