|
|
|
@ -18,7 +18,8 @@ export async function fetchJSON(url, data) {
|
|
|
|
|
return await response.json();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const API_HOST = 'http://202.103.68.144:8888';
|
|
|
|
|
// const API_HOST = 'http://202.103.68.144:8888';
|
|
|
|
|
const API_HOST = 'https://p9axztuwd7x8a7.mycht.cn/whatsapp_callback';
|
|
|
|
|
|
|
|
|
|
export const useConversations = ({loginUser, realtimeAPI}) => {
|
|
|
|
|
const { userId } = loginUser;
|
|
|
|
@ -28,7 +29,7 @@ export const useConversations = ({loginUser, realtimeAPI}) => {
|
|
|
|
|
const [activeConversations, setActiveConversations] = useState({}); // all active conversation
|
|
|
|
|
const [currentID, setCurrentID] = useState();
|
|
|
|
|
const [conversationsList, setConversationsList] = useState([]); // open conversations
|
|
|
|
|
const [currentConversation, setCurrentConversation] = useState({ id: '', customer_name: '', order_sn: '' });
|
|
|
|
|
const [currentConversation, setCurrentConversation] = useState({ sn: '', customer_name: '', coli_sn: '' });
|
|
|
|
|
const currentConversationRef = useRef(currentConversation);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
currentConversationRef.current = currentConversation;
|
|
|
|
@ -67,13 +68,16 @@ export const useConversations = ({loginUser, realtimeAPI}) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getConversationsList = async () => {
|
|
|
|
|
const data = await fetchJSON('http://127.0.0.1:4523/m2/3888351-0-default/142426823');
|
|
|
|
|
const dataMapped = data.reduce((r, v) => ({ ...r, [v.id]: [] }), {});
|
|
|
|
|
setConversationsList(data);
|
|
|
|
|
const { result: data } = await fetchJSON(`${API_HOST}/getconversations`, { opisn: userId });
|
|
|
|
|
// const _data = [];
|
|
|
|
|
const _data = testConversations;
|
|
|
|
|
const list = [..._data, ...data];
|
|
|
|
|
const dataMapped = list.reduce((r, v) => ({ ...r, [v.sn]: [] }), {});
|
|
|
|
|
setConversationsList(list);
|
|
|
|
|
setActiveConversations({...dataMapped, ...activeConversations});
|
|
|
|
|
console.log(data, dataMapped);
|
|
|
|
|
if (data && data.length) {
|
|
|
|
|
switchConversation(data[0]);
|
|
|
|
|
console.log(list, dataMapped);
|
|
|
|
|
if (list && list.length) {
|
|
|
|
|
switchConversation(list[0]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -88,28 +92,43 @@ export const useConversations = ({loginUser, realtimeAPI}) => {
|
|
|
|
|
|
|
|
|
|
const [customerOrderProfile, setCustomerProfile] = useState({});
|
|
|
|
|
const getCustomerProfile = async (colisn) => {
|
|
|
|
|
const data = await fetchJSON(`${API_HOST}/getorderinfo`, { colisn });
|
|
|
|
|
setCustomerProfile(data.result?.[0] || {});
|
|
|
|
|
const { result } = await fetchJSON(`${API_HOST}/getorderinfo`, { colisn });
|
|
|
|
|
const data = result?.[0] || {};
|
|
|
|
|
setCustomerProfile(data);
|
|
|
|
|
|
|
|
|
|
if (!isEmpty(data.conversation)) {
|
|
|
|
|
setConversationsList((pre) => [...data.conversations, ...pre]);
|
|
|
|
|
setCurrentConversation(data.conversation[0]);
|
|
|
|
|
const thisCMapped = data.conversation.reduce((r, v) => ({ ...r, [v.sn]: [] }), {});
|
|
|
|
|
setActiveConversations((pre) => ({ ...pre, ...thisCMapped }));
|
|
|
|
|
setMessages([]); // todo: 获取当前会话的历史消息
|
|
|
|
|
} else {
|
|
|
|
|
// reset chat window
|
|
|
|
|
setMessages([]);
|
|
|
|
|
setCurrentConversation({ sn: '', customer_name: '', coli_sn: '' });
|
|
|
|
|
// todo: 加入新会话
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const switchConversation = (cc) => {
|
|
|
|
|
setCurrentID(cc.id);
|
|
|
|
|
setCurrentConversation(cc);
|
|
|
|
|
// const _all = [];
|
|
|
|
|
const _all = all.map((ele) => receivedMsgTypeMapped['whatsapp.inbound_message.received'].contentToRender(ele)); // debug: 0
|
|
|
|
|
// todo: update msg status
|
|
|
|
|
// const _all = all2.map((ele) => receivedMsgTypeMapped['whatsapp.message.updated'].contentToRender(ele)); // debug: 0
|
|
|
|
|
setMessages([..._all, ...(activeConversations[cc.id] || [])]);
|
|
|
|
|
setCurrentID(cc.sn);
|
|
|
|
|
setCurrentConversation({...cc, id: cc.sn, customer_name: cc.whatsapp_name});
|
|
|
|
|
// Get customer profile when switching conversation
|
|
|
|
|
// getCustomerProfile(??);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Get customer profile when switching conversation
|
|
|
|
|
// useEffect(() => {
|
|
|
|
|
// const colisn = currentConversation.order_sn;
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
console.log('currentConversation', currentConversation);
|
|
|
|
|
// const colisn = currentConversation.coli_sn;
|
|
|
|
|
// getCustomerProfile(colisn);
|
|
|
|
|
// return () => {};
|
|
|
|
|
// }, [currentConversation]);
|
|
|
|
|
const _all = [];
|
|
|
|
|
// const _all = all.map((ele) => receivedMsgTypeMapped['whatsapp.inbound_message.received'].contentToRender(ele)); // debug: 0
|
|
|
|
|
// todo: update msg status
|
|
|
|
|
// const _all = all2.map((ele) => receivedMsgTypeMapped['whatsapp.message.updated'].contentToRender(ele)); // debug: 0
|
|
|
|
|
setMessages([..._all, ...(activeConversations[currentID] || [])]);
|
|
|
|
|
return () => {};
|
|
|
|
|
}, [currentConversation]);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* *****************************************************************************************************
|
|
|
|
@ -149,7 +168,7 @@ export const useConversations = ({loginUser, realtimeAPI}) => {
|
|
|
|
|
|
|
|
|
|
const addMessage = (message) => {
|
|
|
|
|
setMessages((prevMessages) => [...prevMessages, message]);
|
|
|
|
|
addMessageToConversations(currentConversationRef.current.id, message);
|
|
|
|
|
addMessageToConversations(currentConversationRef.current.sn, message);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateMessage = (message) => {
|
|
|
|
@ -164,7 +183,7 @@ export const useConversations = ({loginUser, realtimeAPI}) => {
|
|
|
|
|
return targetMsgs;
|
|
|
|
|
});
|
|
|
|
|
// 更新会话中的消息
|
|
|
|
|
const targetId = currentConversationRef.current.id;
|
|
|
|
|
const targetId = currentConversationRef.current.sn;
|
|
|
|
|
setActiveConversations((prevList) => ({
|
|
|
|
|
...prevList,
|
|
|
|
|
[targetId]: targetMsgs,
|
|
|
|
@ -565,3 +584,38 @@ const AllTemplates = [
|
|
|
|
|
'statusUpdateEvent': 'APPROVED',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const testConversations = [
|
|
|
|
|
{
|
|
|
|
|
'sn': 3001,
|
|
|
|
|
'opi_sn': 354,
|
|
|
|
|
'coli_sn': 0,
|
|
|
|
|
'whatsapp_phone_number': '+8618777396951',
|
|
|
|
|
"last_received_time": new Date().toDateString(),
|
|
|
|
|
"last_send_time": new Date().toDateString(),
|
|
|
|
|
'unread_msg_count': Math.floor(Math.random() * (100 - 2 + 1) + 2),
|
|
|
|
|
'whatsapp_name': 'LiaoYijun',
|
|
|
|
|
'customer_name': 'LiaoYijun',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'sn': 3002,
|
|
|
|
|
'opi_sn': 354,
|
|
|
|
|
'coli_sn': 0,
|
|
|
|
|
'whatsapp_phone_number': '+8613317835586',
|
|
|
|
|
"last_received_time": new Date().toDateString(),
|
|
|
|
|
"last_send_time": new Date().toDateString(),
|
|
|
|
|
'unread_msg_count': Math.floor(Math.random() * (100 - 2 + 1) + 2),
|
|
|
|
|
'whatsapp_name': 'QinQianSheng',
|
|
|
|
|
'customer_name': 'QinQianSheng',
|
|
|
|
|
},
|
|
|
|
|
// {
|
|
|
|
|
// 'sn': 3003,
|
|
|
|
|
// 'opi_sn': 354,
|
|
|
|
|
// 'coli_sn': 240129003,
|
|
|
|
|
// 'whatsapp_phone_number': '+8618777396951',
|
|
|
|
|
// "last_received_time": new Date().toDateString(),
|
|
|
|
|
// "last_send_time": new Date().toDateString(),
|
|
|
|
|
// 'unread_msg_count': Math.floor(Math.random() * (100 - 2 + 1) + 2),
|
|
|
|
|
// 'whatsapp_name': 'LeiYuanTing',
|
|
|
|
|
// },
|
|
|
|
|
];
|
|
|
|
|