会话窗口刷新和获取消息记录

dev/chat
Lei OT 2 years ago
parent 92c5a8636d
commit e2499e9901

@ -111,8 +111,8 @@ export const fetchTemplates = async () => {
return canUseTemplates; return canUseTemplates;
}; };
export const fetchConversationsList = async (opisn) => { export const fetchConversationsList = async (params) => {
const { result: data } = await fetchJSON(`${API_HOST}/getconversations`, { opisn }); const { result: data } = await fetchJSON(`${API_HOST}/getconversations`, params);
const list = data.map((ele) => ({ ...ele, customer_name: ele.whatsapp_name.trim() })); const list = data.map((ele) => ({ ...ele, customer_name: ele.whatsapp_name.trim() }));
return list; return list;
}; };

@ -5,8 +5,7 @@ import { SendOutlined, MessageOutlined, SmileOutlined, PictureOutlined, CommentO
import { useAuthContext } from '@/stores/AuthContext'; import { useAuthContext } from '@/stores/AuthContext';
import { useConversationState, useConversationDispatch } from '@/stores/ConversationContext'; import { useConversationState, useConversationDispatch } from '@/stores/ConversationContext';
import { import {
fetchCustomerProfile, fetchConversationsList,
receivedCustomerProfile,
setCurrentConversation, setCurrentConversation,
addConversationList, delConversationitem, addConversationList, delConversationitem,
fetchConversationItemClose, fetchConversationItemClose,
@ -83,45 +82,23 @@ const Conversations = () => {
useEffect(() => { useEffect(() => {
if (order_sn) { if (order_sn) {
// getCustomerProfile(order_sn); getOrderConversationList(order_sn);
// reset chat window
dispatch(setCurrentConversation({ sn: '', customer_name: '', coli_sn: order_sn }));
} }
return () => {}; return () => {};
}, [order_sn]); }, [order_sn]);
const getCustomerProfile = async (colisn) => { const getOrderConversationList = async (colisn) => {
const data = await fetchCustomerProfile(colisn); const data = await fetchConversationsList({ opisn: userId, colisn });
dispatch(receivedCustomerProfile(data)); if (!isEmpty(data)) {
if (isEmpty(data)) { dispatch(addConversationList(data));
return false; switchConversation(data[0]);
} // dispatch(setCurrentConversation(data[0]));
if (!isEmpty(data.conversations)) {
dispatch(addConversationList(data.conversations));
dispatch(setCurrentConversation(data.conversations[0]));
} else { } else {
// reset chat window // reset chat window
if (isEmpty(data.contact?.[0]?.whatsapp_phone_number)) { dispatch(setCurrentConversation({ sn: '', customer_name: '', coli_sn: order_sn }));
dispatch(setCurrentConversation({ sn: '', customer_name: '', coli_sn: '' }));
return false; return false;
} }
//
const newChat = {
'sn': uuid(),
// 'opi_sn': 354,
'coli_sn': colisn,
'whatsapp_phone_number': data.contact[0].whatsapp_phone_number,
'last_received_time': '',
'last_send_time': '',
'unread_msg_count': 0,
'whatsapp_name': data.contact[0].name,
'customer_name': data.contact[0].name,
};
dispatch(addConversationList([newChat]));
const newCurrent = pick(newChat, ['sn', 'coli_sn', 'whatsapp_phone_number', 'whatsapp_name', 'customer_name']);
dispatch(setCurrentConversation(newCurrent));
}
}; };
const switchConversation = async (item) => { const switchConversation = async (item) => {
console.log('invoke switch'); console.log('invoke switch');
@ -134,9 +111,6 @@ const Conversations = () => {
const data = await fetchMessages({ opisn: userId, whatsappid: item.whatsapp_phone_number }); const data = await fetchMessages({ opisn: userId, whatsappid: item.whatsapp_phone_number });
dispatch(receivedMessageList(item.sn, data)); dispatch(receivedMessageList(item.sn, data));
} }
if (isEmpty(item.coli_sn) || item.coli_sn === '0') {
dispatch(receivedCustomerProfile({}));
}
}; };
const onSwitchConversation = (item) => { const onSwitchConversation = (item) => {

@ -56,10 +56,9 @@ const ConversationProvider = ({ children }) => {
}, []); }, []);
useEffect(() => { useEffect(() => {
fetchConversationsList(userId).then((data) => { fetchConversationsList({ opisn: userId }).then((data) => {
console.log(data, 'llllllllllllllllllllllll');
dispatch(receivedConversationList(data)); dispatch(receivedConversationList(data));
}); // todo: });
fetchTemplates().then((data) => dispatch(receivedTemplates(data))); fetchTemplates().then((data) => dispatch(receivedTemplates(data)));

Loading…
Cancel
Save