|
|
|
@ -1,10 +1,10 @@
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
import { useParams, useNavigate } from 'react-router-dom';
|
|
|
|
|
import { useParams, useNavigate, useLocation } from 'react-router-dom';
|
|
|
|
|
import { Button, Dropdown } from 'antd';
|
|
|
|
|
import { MoreOutlined } from '@ant-design/icons';
|
|
|
|
|
import { useAuthContext } from '@/stores/AuthContext';
|
|
|
|
|
import {
|
|
|
|
|
fetchConversationsList,
|
|
|
|
|
fetchOrderConversationsList,
|
|
|
|
|
fetchConversationItemClose,
|
|
|
|
|
fetchMessages,
|
|
|
|
|
} from '@/actions/ConversationActions';
|
|
|
|
@ -44,6 +44,8 @@ const CDropdown = (props) => {
|
|
|
|
|
* []
|
|
|
|
|
*/
|
|
|
|
|
const Conversations = () => {
|
|
|
|
|
const { state: orderRow } = useLocation()
|
|
|
|
|
const { coli_guest_WhatsApp } = orderRow || {};
|
|
|
|
|
const { order_sn } = useParams();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const { loginUser } = useAuthContext();
|
|
|
|
@ -60,7 +62,7 @@ const Conversations = () => {
|
|
|
|
|
title: item.whatsapp_name.trim() || item.whatsapp_phone_number,
|
|
|
|
|
// subtitle: item.whatsapp_phone_number,
|
|
|
|
|
// subtitle: item.lastMessage,
|
|
|
|
|
date: item.last_received_time, // last_send_time // todo: GMT+8
|
|
|
|
|
date: item.last_received_time, // last_send_time
|
|
|
|
|
unread: item.unread_msg_count,
|
|
|
|
|
// showMute: true,
|
|
|
|
|
// muted: false,
|
|
|
|
@ -76,21 +78,23 @@ const Conversations = () => {
|
|
|
|
|
return () => {};
|
|
|
|
|
}, [conversationsList, currentConversation]);
|
|
|
|
|
|
|
|
|
|
const [shouldFetchCList, setShouldFetchCList] = useState(true);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (order_sn) {
|
|
|
|
|
// getOrderConversationList(order_sn);
|
|
|
|
|
// debug: reset chat window
|
|
|
|
|
setCurrentConversation({ sn: '', customer_name: '', coli_sn: order_sn });
|
|
|
|
|
if (order_sn && shouldFetchCList) {
|
|
|
|
|
getOrderConversationList(order_sn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {};
|
|
|
|
|
}, [order_sn]);
|
|
|
|
|
}, [order_sn, shouldFetchCList]);
|
|
|
|
|
|
|
|
|
|
const getOrderConversationList = async (colisn) => {
|
|
|
|
|
const data = await fetchConversationsList({ opisn: userId, colisn });
|
|
|
|
|
const { whatsapp_phone_number } = switchToC;
|
|
|
|
|
const whatsappID = coli_guest_WhatsApp || whatsapp_phone_number || '';
|
|
|
|
|
const data = await fetchOrderConversationsList({ opisn: userId, colisn: colisn, whatsappid: whatsappID });
|
|
|
|
|
if (!isEmpty(data)) {
|
|
|
|
|
addToConversationList(data);
|
|
|
|
|
switchConversation(data[0]);
|
|
|
|
|
const ifCurrent = data.findIndex((item) => item.sn === currentConversation.sn);
|
|
|
|
|
switchConversation(data[ifCurrent === -1 ? 0 : ifCurrent]);
|
|
|
|
|
} else {
|
|
|
|
|
// reset chat window
|
|
|
|
|
setCurrentConversation({ sn: '', customer_name: '', coli_sn: order_sn });
|
|
|
|
@ -109,31 +113,24 @@ const Conversations = () => {
|
|
|
|
|
setCurrentConversation(item);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const [switchToC, setSwitchToC] = useState({});
|
|
|
|
|
const onSwitchConversation = (item) => {
|
|
|
|
|
if (item.coli_sn) {
|
|
|
|
|
navigate(`/order/chat/${item.coli_sn}`, { replace: true });
|
|
|
|
|
setSwitchToC(item);
|
|
|
|
|
setShouldFetchCList(false);
|
|
|
|
|
} else {
|
|
|
|
|
navigate(`/order/chat`, { replace: true });
|
|
|
|
|
}
|
|
|
|
|
switchConversation(item);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// const handleConversationItemClose = async (item) => {
|
|
|
|
|
// console.log('invoke close', item);
|
|
|
|
|
// const data = await postConversationItemClose({ conversationid: item.sn, opisn: userId });
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ChatList
|
|
|
|
|
className=' overflow-x-hidden'
|
|
|
|
|
dataSource={chatlist}
|
|
|
|
|
onClick={(item) => onSwitchConversation(item)}
|
|
|
|
|
// onContextMenu={(item, i, e) => {
|
|
|
|
|
// console.log(item, i, e);
|
|
|
|
|
// return ( <p>ppp</p> )
|
|
|
|
|
// }}
|
|
|
|
|
// onClickMute={handleConversationItemClose}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|