feat: lots updates

This commit is contained in:
zerosoul
2022-01-30 21:30:12 +08:00
parent e18c7323a6
commit ceb02d834f
38 changed files with 1943 additions and 1832 deletions
+7 -5
View File
@@ -1,9 +1,8 @@
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import BASE_URL from "../config";
import { createApi } from "@reduxjs/toolkit/query/react";
import baseQuery from "./base.query";
export const authApi = createApi({
reducerPath: "auth",
baseQuery: fetchBaseQuery({ baseUrl: BASE_URL }),
baseQuery,
endpoints: (builder) => ({
login: builder.mutation({
query: (credentials) => ({
@@ -19,7 +18,10 @@ export const authApi = createApi({
// return resp;
// },
}),
logout: builder.query({
query: () => ({ url: `token/logout` }),
}),
}),
});
export const { useLoginMutation } = authApi;
export const { useLoginMutation, useLazyLogoutQuery } = authApi;
+4 -12
View File
@@ -1,20 +1,12 @@
import { fetchBaseQuery } from "@reduxjs/toolkit/query";
import BASE_URL, { tokenHeader } from "../config";
// const whiteList = [
// "/resource/avatar",
// "/resource/company/logo",
// "/resource/thumbnail",
// "/resource/image",
// "/token/login",
// "/token/renew",
// "/user",
// "/admin/system/company",
// ];
const whiteList = ["login"];
const baseQuery = fetchBaseQuery({
baseUrl: BASE_URL,
prepareHeaders: (headers, { getState }) => {
prepareHeaders: (headers, { getState, endpoint }) => {
console.log("req", endpoint);
const { token } = getState().authData;
if (token) {
if (token && !whiteList.includes(endpoint)) {
headers.set(tokenHeader, token);
}
return headers;
+1 -1
View File
@@ -2,7 +2,7 @@ import { createApi } from "@reduxjs/toolkit/query/react";
import baseQuery from "./base.query";
import { REHYDRATE } from "redux-persist";
export const channelApi = createApi({
reducerPath: "groups",
reducerPath: "channel",
baseQuery,
extractRehydrationInfo(action, { reducerPath }) {
if (action.type === REHYDRATE) {
+1 -1
View File
@@ -3,7 +3,7 @@ import baseQuery from "./base.query";
import BASE_URL from "../config";
import { REHYDRATE } from "redux-persist";
export const contactApi = createApi({
reducerPath: "contacts",
reducerPath: "contact",
baseQuery,
extractRehydrationInfo(action, { reducerPath }) {
if (action.type === REHYDRATE) {
+2 -2
View File
@@ -14,9 +14,9 @@ export const serverApi = createApi({
// },
endpoints: (builder) => ({
getServer: builder.query({
query: () => ({ url: `admin/system/company` }),
query: () => ({ url: `admin/system/organization` }),
transformResponse: (data) => {
data.logo = `${BASE_URL}/resource/company/logo`;
data.logo = `${BASE_URL}/resource/organization/logo`;
return data;
},
}),
+7 -1
View File
@@ -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;
+36
View File
@@ -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;
+50 -16
View File
@@ -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;
+16 -4
View File
@@ -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;
+16
View File
@@ -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;
+4
View File
@@ -11,6 +11,8 @@ import {
REGISTER,
} from "redux-persist";
import authDataReducer from "./slices/auth.data";
import uiReducer from "./slices/ui";
import channelsReducer from "./slices/channels";
import channelMsgReducer from "./slices/message.channel";
import userMsgReducer from "./slices/message.user";
import { authApi } from "./services/auth";
@@ -25,6 +27,8 @@ const persistConfig = {
const persistedReducer = persistReducer(
persistConfig,
combineReducers({
ui: uiReducer,
channels: channelsReducer,
userMsg: userMsgReducer,
channelMsg: channelMsgReducer,
authData: authDataReducer,