You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Global-sales/src/reducers/ConversationReducer.js

155 lines
6.7 KiB
JavaScript

import initialState from '@/records/ConversationState';
const NAME_SPACE = 'CONVERSATION/';
const ConversationReducer = (state = initialState, action) => {
switch (action.type) {
case NAME_SPACE + 'INIT_WEBSOCKET':
return { ...state, websocket: action.payload };
case NAME_SPACE + 'MODIFY_WEBSOCKET_STATE':
return { ...state, websocketOpened: action.payload, };
case NAME_SPACE + 'MODIFY_WEBSOCKET_RETRYTIMES':
return { ...state, websocketRetrytimes: action.payload, websocketRetrying: action.payload > 0 };
case NAME_SPACE + 'SET_CONVERSATION_LIST':{
const conversationsMapped = action.payload.reduce((r, v) => ({ ...r, [`${v.sn}`]: [] }), {});
return { ...state, conversationsList: action.payload, activeConversations: conversationsMapped };
}
case NAME_SPACE + 'ADD_TO_CONVERSATIONS_LIST':{
const { activeConversations } = state;
const conversationsIds = Object.keys(activeConversations);
const newConversations = action.payload.filter((conversation) => !conversationsIds.includes(`${conversation.sn}`));
const newConversationsMapped = newConversations.reduce((r, v) => ({ ...r, [`${v.sn}`]: [] }), {});
return {
...state,
conversationsList: [...newConversations, ...state.conversationsList],
activeConversations: { ...activeConversations, ...newConversationsMapped },
};
}
case NAME_SPACE + 'DEL_CONVERSATIONS_ITEM': {
const { conversationsList, activeConversations, currentConversation, customerOrderProfile } = state;
const targetId = action.payload.sn;
const targetIndex = conversationsList.findIndex((ele) => String(ele.sn) === String(targetId));
conversationsList.splice(targetIndex, 1);
return {
...state,
conversationsList: [...conversationsList],
activeConversations: { ...activeConversations, [`${targetId}`]: [] },
currentConversation: {},
customerOrderProfile: {},
};
}
case NAME_SPACE + 'SET_TEMPLATE_LIST':
return { ...state, templates: action.payload };
case NAME_SPACE + 'SET_CUSTOMER_ORDER_PROFILE':
return { ...state, customerOrderProfile: action.payload };
case NAME_SPACE + 'SET_CURRENT_CONVERSATION': {
// 清空未读
const { conversationsList } = state;
const targetId = action.payload.sn;
const targetIndex = conversationsList.findIndex((ele) => String(ele.sn) === String(targetId));
targetIndex !== -1 ? conversationsList.splice(targetIndex, 1, {
...conversationsList[targetIndex],
unread_msg_count: 0,
}) : null;
return { ...state, currentConversation: action.payload, conversationsList: [...conversationsList] };
}
case NAME_SPACE + 'SET_ACTIVE_CONVERSATIONS': {
const { activeConversations } = state;
return { ...state, activeConversations: { ...activeConversations, ...action.payload } };
}
case NAME_SPACE + 'UPDATE_SENT_MESSAGE_ITEM': {
console.log('UPDATE_SENT_MESSAGE_ITEM-----------------------------------------------------------------', action);
// 更新会话中的消息
const { activeConversations, conversationsList, } = state;
const message = action.payload;
const targetId = message.conversationid;
const targetMsgs = (activeConversations[String(targetId)] || []).map((ele) => {
if (ele.id === ele.actionId && ele.actionId === message.actionId) {
return { ...ele, id: message.id, status: message.status, dateString: message.dateString };
} else if (ele.id === message.id) {
return { ...ele, id: message.id, status: message.status, dateString: message.dateString };
}
return ele;
});
// 显示会话中其他客户端发送的消息
const targetMsgsIds = targetMsgs.map((ele) => ele.id);
if ( ! targetMsgsIds.includes(message.id)) {
targetMsgs.push(message);
}
// 更新列表的时间
if (message.type !== 'error') {
const targetIndex = conversationsList.findIndex((ele) => String(ele.sn) === String(targetId));
conversationsList.splice(targetIndex, 1, {
...conversationsList[targetIndex],
last_received_time: message.date,
});
}
return {
...state,
activeConversations: { ...state.activeConversations, [String(targetId)]: targetMsgs },
conversationsList: [...conversationsList],
};
}
case NAME_SPACE + 'RECEIVED_MESSAGE_LIST': {
const { targetId, data } = action.payload;
return {
...state,
activeConversations: { ...state.activeConversations, [String(targetId)]: data },
};
}
case NAME_SPACE + 'SENT_NEW_MESSAGE':
case NAME_SPACE + 'RECEIVED_NEW_MESSAGE': {
const { activeConversations, conversationsList, currentConversation } = state;
const { targetId, message } = action.payload;
const targetMsgs = activeConversations[String(targetId)] || [];
const targetIndex = conversationsList.findIndex((ele) => String(ele.sn) === String(targetId));
const newConversation =
targetId !== -1
? {
...conversationsList[targetIndex],
last_received_time: message.date,
unread_msg_count:
String(targetId) !== String(currentConversation.sn) && message.sender !== 'me'
? conversationsList[targetIndex].unread_msg_count + 1
: conversationsList[targetIndex].unread_msg_count,
}
: {
...message,
sn: targetId,
last_received_time: message.date,
unread_msg_count: message.sender === 'me' ? 0 : 1,
};
conversationsList.splice(targetIndex, 1);
conversationsList.unshift(newConversation);
return {
...state,
activeConversations: { ...activeConversations, [String(targetId)]: [...targetMsgs, message] },
conversationsList: [...conversationsList],
currentConversation: {
...state.currentConversation,
last_received_time: String(targetId) === String(currentConversation.sn) ? message.date : currentConversation.last_received_time,
},
};
}
case NAME_SPACE + 'SET_REFERENCE_MSG':
return {...state, referenceMsg: action.payload};
case NAME_SPACE + 'ADD_ERROR': {
console.log('add error', state.errors, action.payload);
const prelist = state.errors || [];
return { ...state, errors: [...prelist, action.payload] };
}
default:
// throw new Error(`Unknown action: ${action.type}`);
return state;
}
};
export default ConversationReducer;