feat: lots of updates

This commit is contained in:
zerosoul
2022-01-27 22:06:44 +08:00
commit e18c7323a6
72 changed files with 12432 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
user: null,
token: null,
refreshToken: null,
};
const authDataSlice = createSlice({
name: "authData",
initialState,
reducers: {
setAuthData(state, action) {
const { user, token, refresh_token } = action.payload;
state.user = user;
state.token = token;
state.refreshToken = refresh_token;
},
},
});
export const { setAuthData } = authDataSlice.actions;
export default authDataSlice.reducer;
+40
View File
@@ -0,0 +1,40 @@
import { createSlice } from "@reduxjs/toolkit";
import { isObjectEqual } from "../../common/utils";
const initialState = {};
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 };
if (state[id]) {
let replaceMsg = state[id][mid];
// 如果存在,并且新消息和缓存消息不一样,则替换掉
if (replaceMsg) {
const copyMsg = { ...replaceMsg };
if (!isObjectEqual(copyMsg, newMsg)) {
state[id][mid] = newMsg;
}
} 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 };
}
},
},
});
export const { addChannelMsg } = channelMsgSlice.actions;
export default channelMsgSlice.reducer;
+40
View File
@@ -0,0 +1,40 @@
import { createSlice } from "@reduxjs/toolkit";
import { isObjectEqual } from "../../common/utils";
const initialState = {};
const userMsgSlice = createSlice({
name: "userMessage",
initialState,
reducers: {
addUserMsg(state, action) {
const { id, content, created_at, mid, from_uid } = action.payload;
const newMsg = { content, created_at, from_uid };
if (state[id]) {
let replaceMsg = state[id][mid];
// 如果存在,并且新消息和缓存消息不一样,则替换掉
if (replaceMsg) {
const copyMsg = { ...replaceMsg };
if (!isObjectEqual(copyMsg, newMsg)) {
state[id][mid] = newMsg;
}
} 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 user msg", copyMsg, newMsg);
// if (!isObjectEqual(copyMsg, newMsg)) {
// state[id][replaceIdx] = newMsg;
// }
// } else {
// state[id] = [...state[id], newMsg];
// }
} else {
state[id] = { [mid]: newMsg };
}
},
},
});
export const { addUserMsg } = userMsgSlice.actions;
export default userMsgSlice.reducer;