|
|
|
@ -1,26 +1,33 @@
|
|
|
|
|
import { useRef, useEffect, useState } from 'react';
|
|
|
|
|
import { useParams, useNavigate } from "react-router-dom";
|
|
|
|
|
import { useParams, useNavigate } from 'react-router-dom';
|
|
|
|
|
import { List, Avatar, Flex } from 'antd';
|
|
|
|
|
import { useAuthContext } from '@/stores/AuthContext';
|
|
|
|
|
import { useConversationState, useConversationDispatch } from '@/stores/ConversationContext';
|
|
|
|
|
import { fetchCustomerProfile, receivedCustomerProfile, setCurrentConversation, addConversationList, postConversationItemClose } from '@/actions/ConversationActions'
|
|
|
|
|
import {
|
|
|
|
|
fetchCustomerProfile,
|
|
|
|
|
receivedCustomerProfile,
|
|
|
|
|
setCurrentConversation,
|
|
|
|
|
addConversationList,
|
|
|
|
|
postConversationItemClose,
|
|
|
|
|
fetchMessages, receivedMessageList,
|
|
|
|
|
} from '@/actions/ConversationActions';
|
|
|
|
|
import { ChatList } from 'react-chat-elements';
|
|
|
|
|
import { isEmpty, pick } from '@/utils/utils';
|
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
/**
|
|
|
|
|
* []
|
|
|
|
|
*/
|
|
|
|
|
const Conversations = (() => {
|
|
|
|
|
const { order_sn } = useParams();
|
|
|
|
|
const Conversations = () => {
|
|
|
|
|
const { order_sn } = useParams();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const { loginUser } = useAuthContext();
|
|
|
|
|
const { userId } = loginUser;
|
|
|
|
|
const {conversationsList} = useConversationState();
|
|
|
|
|
const { conversationsList, activeConversations, currentConversation } = useConversationState();
|
|
|
|
|
const dispatch = useConversationDispatch();
|
|
|
|
|
const [chatlist, setChatlist] = useState([]);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setChatlist(
|
|
|
|
|
(conversationsList).map((item) => ({
|
|
|
|
|
conversationsList.map((item) => ({
|
|
|
|
|
...item,
|
|
|
|
|
avatar: `https://api.dicebear.com/7.x/avataaars/svg?seed=${item.whatsapp_name.trim() || item.whatsapp_phone_number}`,
|
|
|
|
|
id: item.sn,
|
|
|
|
@ -71,8 +78,8 @@ const Conversations = (() => {
|
|
|
|
|
// 'opi_sn': 354,
|
|
|
|
|
'coli_sn': colisn,
|
|
|
|
|
'whatsapp_phone_number': data.contact[0].whatsapp_phone_number,
|
|
|
|
|
"last_received_time": '',
|
|
|
|
|
"last_send_time": '',
|
|
|
|
|
'last_received_time': '',
|
|
|
|
|
'last_send_time': '',
|
|
|
|
|
'unread_msg_count': 0,
|
|
|
|
|
'whatsapp_name': data.contact[0].name,
|
|
|
|
|
'customer_name': data.contact[0].name,
|
|
|
|
@ -81,12 +88,20 @@ const Conversations = (() => {
|
|
|
|
|
const newCurrent = pick(newChat, ['sn', 'coli_sn', 'whatsapp_phone_number', 'whatsapp_name', 'customer_name']);
|
|
|
|
|
dispatch(setCurrentConversation(newCurrent));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const switchConversation = (item) => {
|
|
|
|
|
};
|
|
|
|
|
const switchConversation = async (item) => {
|
|
|
|
|
if (currentConversation.sn === item.sn) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
dispatch(setCurrentConversation(item));
|
|
|
|
|
if (isEmpty(item.coli_sn) || item.coli_sn === '0') {
|
|
|
|
|
dispatch(receivedCustomerProfile({}));
|
|
|
|
|
}
|
|
|
|
|
const messagesList = activeConversations[`${item.sn}`] || [];
|
|
|
|
|
if (isEmpty(messagesList)) {
|
|
|
|
|
const data = await fetchMessages({ opisn: userId, whatsappid: item.whatsapp_phone_number });
|
|
|
|
|
dispatch(receivedMessageList(item.sn, data));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSwitchConversation = (item) => {
|
|
|
|
@ -94,16 +109,19 @@ const Conversations = (() => {
|
|
|
|
|
navigate(`/order/chat/${item.coli_sn}`, { 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='' dataSource={chatlist} onClick={(item) => onSwitchConversation(item)}
|
|
|
|
|
<ChatList
|
|
|
|
|
className=''
|
|
|
|
|
dataSource={chatlist}
|
|
|
|
|
onClick={(item) => onSwitchConversation(item)}
|
|
|
|
|
// onContextMenu={(item, i, e) => {
|
|
|
|
|
// console.log(item, i, e);
|
|
|
|
|
// return ( <p>ppp</p> )
|
|
|
|
@ -112,5 +130,5 @@ const Conversations = (() => {
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
export default Conversations;
|
|
|
|
|