feat: lots updates

This commit is contained in:
zerosoul
2022-03-10 10:30:38 +08:00
parent e6f5b6e428
commit d497e7df4f
22 changed files with 415 additions and 174 deletions
+15
View File
@@ -6,6 +6,9 @@ import {
msgClearUnread,
msgUpdate,
msgDelete,
msgAddPending,
msgRemovePending,
msgReplacePending,
} from "./message.handler";
const initialState = {};
@@ -37,6 +40,15 @@ const channelMsgSlice = createSlice({
clearChannelMsgUnread(state, action) {
msgClearUnread(state, action.payload);
},
addChannelPendingMsg(state, action) {
msgAddPending(state, action.payload);
},
replaceChannelPendingMsg(state, action) {
msgReplacePending(state, action.payload);
},
removeChannelPendingMsg(state, action) {
msgRemovePending(state, action.payload);
},
},
});
export const {
@@ -48,5 +60,8 @@ export const {
clearChannelMsgUnread,
setChannelMsgRead,
addChannelMsg,
addChannelPendingMsg,
replaceChannelPendingMsg,
removeChannelPendingMsg,
} = channelMsgSlice.actions;
export default channelMsgSlice.reducer;
+44 -1
View File
@@ -58,10 +58,53 @@ export const msgAdd = (state, payload) => {
state[id] = { [mid]: newMsg };
}
};
export const msgAddPending = (state, payload) => {
const {
id,
content,
created_at,
local_mid,
reply_mid = null,
from_uid,
content_type,
unread = false,
} = payload;
const newMsg = { content, content_type, created_at, from_uid, unread };
if (reply_mid && state[id][reply_mid]) {
newMsg.reply = { mid: reply_mid, ...state[id][reply_mid] };
}
if (state[id]) {
state[id][local_mid] = newMsg;
} else {
state[id] = { [local_mid]: newMsg };
}
};
export const msgReplacePending = (state, payload) => {
const { id, local_mid, server_mid } = payload;
if (state[id] && state[id][local_mid]) {
// 先赋值,再去delete
state[id] = Object.fromEntries(
Object.entries(state[id]).map(([key, obj]) => {
return key == local_mid ? [server_mid, obj] : [key, obj];
})
);
// state[id][server_mid] = { ...state[id][local_mid] };
// delete state[id][local_mid];
}
};
export const msgRemovePending = (state, payload) => {
const { id, local_mid } = payload;
if (state[id] && state[id][local_mid]) {
delete state[id][local_mid];
}
};
export const msgSetRead = (state, payload) => {
const { id, mid } = payload;
console.log("set read", id, mid);
state[id][mid].unread = false;
if (state[id] && state[id][mid]) {
state[id][mid].unread = false;
}
};
export const msgDelete = (state, payload) => {
const { id, mid } = payload;
+15
View File
@@ -5,6 +5,9 @@ import {
msgSetRead,
msgUpdate,
msgDelete,
msgAddPending,
msgRemovePending,
msgReplacePending,
} from "./message.handler";
const initialState = {};
const userMsgSlice = createSlice({
@@ -32,6 +35,15 @@ const userMsgSlice = createSlice({
setUserMsgRead(state, action) {
msgSetRead(state, action.payload);
},
addUserPendingMsg(state, action) {
msgAddPending(state, action.payload);
},
replaceUserPendingMsg(state, action) {
msgReplacePending(state, action.payload);
},
removeUserPendingMsg(state, action) {
msgRemovePending(state, action.payload);
},
},
});
export const {
@@ -42,5 +54,8 @@ export const {
initUserMsg,
addUserMsg,
setUserMsgRead,
addUserPendingMsg,
replaceUserPendingMsg,
removeUserPendingMsg,
} = userMsgSlice.actions;
export default userMsgSlice.reducer;