feat: lots updates
This commit is contained in:
@@ -15,7 +15,13 @@ const authDataSlice = createSlice({
|
||||
state.token = token;
|
||||
state.refreshToken = refresh_token;
|
||||
},
|
||||
clearAuthData(state) {
|
||||
console.log("clear auth data");
|
||||
state.user = null;
|
||||
state.token = null;
|
||||
state.refreshToken = null;
|
||||
},
|
||||
},
|
||||
});
|
||||
export const { setAuthData } = authDataSlice.actions;
|
||||
export const { setAuthData, clearAuthData } = authDataSlice.actions;
|
||||
export default authDataSlice.reducer;
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
|
||||
const initialState = {};
|
||||
const channelsSlice = createSlice({
|
||||
name: "channels",
|
||||
initialState,
|
||||
reducers: {
|
||||
setChannels(state, action) {
|
||||
// console.log("set channels store", action);
|
||||
const chs = action.payload;
|
||||
chs.forEach((c) => {
|
||||
const { gid, ...rest } = c;
|
||||
console.log("wtf", gid, rest);
|
||||
state[gid] = rest;
|
||||
});
|
||||
},
|
||||
addChannel(state, action) {
|
||||
// console.log("set channels store", action);
|
||||
const ch = action.payload;
|
||||
const { gid, ...rest } = ch;
|
||||
state[gid] = rest;
|
||||
},
|
||||
deleteChannel(state, action) {
|
||||
const gid = action.payload;
|
||||
delete state[gid];
|
||||
},
|
||||
// clearAuthData(state) {
|
||||
// console.log("clear auth data");
|
||||
// state.user = null;
|
||||
// state.token = null;
|
||||
// state.refreshToken = null;
|
||||
// },
|
||||
},
|
||||
});
|
||||
export const { setChannels, addChannel, deleteChannel } = channelsSlice.actions;
|
||||
export default channelsSlice.reducer;
|
||||
@@ -1,40 +1,74 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
import { isObjectEqual } from "../../common/utils";
|
||||
|
||||
const initialState = {};
|
||||
const initialState = {
|
||||
// accessLogs: {},
|
||||
pendingMsgs: [],
|
||||
};
|
||||
const channelMsgSlice = createSlice({
|
||||
name: "channelMessage",
|
||||
initialState,
|
||||
reducers: {
|
||||
addChannelMsg(state, action) {
|
||||
const { id, content, created_at, mid, from_uid } = action.payload;
|
||||
const newMsg = { content, created_at, from_uid };
|
||||
const {
|
||||
id,
|
||||
content,
|
||||
created_at,
|
||||
mid,
|
||||
from_uid,
|
||||
unread = true,
|
||||
} = action.payload;
|
||||
const newMsg = { content, created_at, from_uid, unread };
|
||||
if (state[id]) {
|
||||
let replaceMsg = state[id][mid];
|
||||
// 如果存在,并且新消息和缓存消息不一样,则替换掉
|
||||
// 如果存在,并且新消息和缓存消息不一样,则替换掉,并且改为已读(可能有问题)
|
||||
if (replaceMsg) {
|
||||
const copyMsg = { ...replaceMsg };
|
||||
if (!isObjectEqual(copyMsg, newMsg)) {
|
||||
state[id][mid] = newMsg;
|
||||
state[id][mid] = { ...newMsg, unread: false };
|
||||
}
|
||||
} else {
|
||||
state[id][mid] = newMsg;
|
||||
}
|
||||
// let replaceIdx = state[id].findIndex((m) => m.mid == mid);
|
||||
// if (replaceIdx > -1) {
|
||||
// const copyMsg = { ...state[id][replaceIdx] };
|
||||
// console.log("current channel msg", copyMsg, newMsg);
|
||||
// if (!isObjectEqual(copyMsg, newMsg)) {
|
||||
// state[id][replaceIdx] = newMsg;
|
||||
// }
|
||||
// } else {
|
||||
// state[id] = [...state[id], newMsg];
|
||||
// }
|
||||
} else {
|
||||
state[id] = { [mid]: newMsg };
|
||||
}
|
||||
},
|
||||
setChannelMsgRead(state, action) {
|
||||
const { id, mid } = action.payload;
|
||||
console.log("set unread", id, mid);
|
||||
state[id][mid].unread = false;
|
||||
},
|
||||
clearChannelMsgUnread(state, action) {
|
||||
const gid = action.payload;
|
||||
console.log("set channel all unread", gid);
|
||||
Object.entries(state[gid]).forEach(([key, obj]) => {
|
||||
obj.unread = false;
|
||||
});
|
||||
},
|
||||
setLastAccessTime(state, action) {
|
||||
// let gid = action.payload;
|
||||
// delete state[gid].lastAccess;
|
||||
// const gid = action.payload;
|
||||
// state.accessLogs[gid] = new Date().getTime();
|
||||
},
|
||||
addPendingMsg(state, action) {
|
||||
state.pendingMsgs.push(action.payload);
|
||||
},
|
||||
removePendingMsg(state, action) {
|
||||
const timestamp = action.payload;
|
||||
state.pendingMsgs = state.pendingMsgs.filter(
|
||||
(m) => m.timestamp != timestamp
|
||||
);
|
||||
},
|
||||
},
|
||||
});
|
||||
export const { addChannelMsg } = channelMsgSlice.actions;
|
||||
export const {
|
||||
setLastAccessTime,
|
||||
clearChannelMsgUnread,
|
||||
setChannelMsgRead,
|
||||
addChannelMsg,
|
||||
addPendingMsg,
|
||||
removePendingMsg,
|
||||
} = channelMsgSlice.actions;
|
||||
export default channelMsgSlice.reducer;
|
||||
|
||||
@@ -7,15 +7,22 @@ const userMsgSlice = createSlice({
|
||||
initialState,
|
||||
reducers: {
|
||||
addUserMsg(state, action) {
|
||||
const { id, content, created_at, mid, from_uid } = action.payload;
|
||||
const newMsg = { content, created_at, from_uid };
|
||||
const {
|
||||
id,
|
||||
content,
|
||||
created_at,
|
||||
mid,
|
||||
from_uid,
|
||||
unread = true,
|
||||
} = action.payload;
|
||||
const newMsg = { content, created_at, from_uid, unread };
|
||||
if (state[id]) {
|
||||
let replaceMsg = state[id][mid];
|
||||
// 如果存在,并且新消息和缓存消息不一样,则替换掉
|
||||
if (replaceMsg) {
|
||||
const copyMsg = { ...replaceMsg };
|
||||
if (!isObjectEqual(copyMsg, newMsg)) {
|
||||
state[id][mid] = newMsg;
|
||||
state[id][mid] = { ...newMsg, unread: false };
|
||||
}
|
||||
} else {
|
||||
state[id][mid] = newMsg;
|
||||
@@ -34,7 +41,12 @@ const userMsgSlice = createSlice({
|
||||
state[id] = { [mid]: newMsg };
|
||||
}
|
||||
},
|
||||
setUserMsgRead(state, action) {
|
||||
const { id, mid } = action.payload;
|
||||
console.log("set unread", id, mid);
|
||||
state[id][mid].unread = false;
|
||||
},
|
||||
},
|
||||
});
|
||||
export const { addUserMsg } = userMsgSlice.actions;
|
||||
export const { addUserMsg, setUserMsgRead } = userMsgSlice.actions;
|
||||
export default userMsgSlice.reducer;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
|
||||
const initialState = {
|
||||
menuExpand: true,
|
||||
};
|
||||
const uiSlice = createSlice({
|
||||
name: "ui",
|
||||
initialState,
|
||||
reducers: {
|
||||
toggleMenuExpand(state) {
|
||||
state.menuExpand = !state.menuExpand;
|
||||
},
|
||||
},
|
||||
});
|
||||
export const { toggleMenuExpand } = uiSlice.actions;
|
||||
export default uiSlice.reducer;
|
||||
Reference in New Issue
Block a user