refactor: more TS code
This commit is contained in:
+29
-12
@@ -1,9 +1,12 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
import BASE_URL, { ContentTypes } from "../config";
|
||||
import { isImage } from "../../common/utils";
|
||||
|
||||
export interface State {
|
||||
replying: {};
|
||||
[key: number]: object;
|
||||
replying: {
|
||||
[key: string | number]: number;
|
||||
};
|
||||
}
|
||||
|
||||
const initialState: State = {
|
||||
@@ -20,19 +23,34 @@ const messageSlice = createSlice({
|
||||
fillMessage(state, action) {
|
||||
return Object.assign({ ...initialState }, action.payload);
|
||||
},
|
||||
updateMessage(state, action) {
|
||||
updateMessage(state, action: PayloadAction<{ mid: number; [key: string | number]: any }>) {
|
||||
const { mid, ...rest } = action.payload;
|
||||
state[mid] = { ...state[mid], ...rest };
|
||||
},
|
||||
addMessage(state, action) {
|
||||
addMessage(
|
||||
state,
|
||||
action: PayloadAction<{
|
||||
mid: number;
|
||||
sending: boolean;
|
||||
content_type: string;
|
||||
content: string;
|
||||
properties?: {
|
||||
content_type: string;
|
||||
size: number;
|
||||
};
|
||||
file_path?: string;
|
||||
download?: string;
|
||||
thumbnail?: string;
|
||||
}>
|
||||
) {
|
||||
const data = action.payload;
|
||||
const { mid, sending, content_type, content, properties = {} } = data;
|
||||
const { mid, sending, content_type, content, properties } = data;
|
||||
// 如果是正发送,并且已存在,则不覆盖
|
||||
if (sending && state[mid]) return;
|
||||
const isFile = content_type == ContentTypes.file;
|
||||
// image
|
||||
const props = properties ?? {};
|
||||
const isPic = isImage(props.content_type, props.size);
|
||||
const props = properties;
|
||||
const isPic = isImage(props?.content_type, props?.size);
|
||||
if (isFile) {
|
||||
if (!sending) {
|
||||
data.file_path = content;
|
||||
@@ -53,18 +71,18 @@ const messageSlice = createSlice({
|
||||
}
|
||||
state[mid] = data;
|
||||
},
|
||||
removeMessage(state, action) {
|
||||
removeMessage(state, action: PayloadAction<number | number[]>) {
|
||||
const mids = Array.isArray(action.payload) ? action.payload : [action.payload];
|
||||
mids.forEach((id) => {
|
||||
mids.forEach((id: number) => {
|
||||
delete state[id];
|
||||
});
|
||||
},
|
||||
addReplyingMessage(state, action) {
|
||||
addReplyingMessage(state, action: PayloadAction<{ key: string | number; mid: number }>) {
|
||||
const { key, mid } = action.payload;
|
||||
console.log("to ", key, mid);
|
||||
state.replying[key] = mid;
|
||||
},
|
||||
removeReplyingMessage(state, action) {
|
||||
removeReplyingMessage(state, action: PayloadAction<string | number>) {
|
||||
const key = action.payload;
|
||||
if (state.replying[key]) {
|
||||
delete state.replying[key];
|
||||
@@ -76,7 +94,6 @@ const messageSlice = createSlice({
|
||||
export const {
|
||||
resetMessage,
|
||||
fillMessage,
|
||||
setMessage,
|
||||
updateMessage,
|
||||
addMessage,
|
||||
removeMessage,
|
||||
|
||||
Reference in New Issue
Block a user