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

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

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

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

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

Loading…
Cancel
Save