From d19c70968cb57495352320020202f3c9e7b1bed0 Mon Sep 17 00:00:00 2001 From: Lei OT Date: Wed, 21 Feb 2024 15:37:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=B7=E6=96=B0=E9=A1=B5=E9=9D=A2,=20?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E6=8C=87=E5=AE=9A=E7=9A=84=E4=BC=9A=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/ConversationStore.js | 6 +++++- .../Conversations/Components/ConversationsList.jsx | 13 ++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/stores/ConversationStore.js b/src/stores/ConversationStore.js index 6943c66..c815290 100644 --- a/src/stores/ConversationStore.js +++ b/src/stores/ConversationStore.js @@ -16,6 +16,7 @@ const initialConversationState = { // websocketRetrytimes: null, errors: [], // 错误信息 + initialState: false, // templates: [], @@ -252,16 +253,19 @@ export const useConversationStore = create(devtools((set, get) => ({ // state actions addError: (error) => set((state) => ({ errors: [...state.errors, error] })), + setInitial: (v) => set({ initialState: v }), // side effects fetchInitialData: async (userId) => { - const { addToConversationList, setTemplates } = get(); + const { addToConversationList, setTemplates, setInitial } = get(); const conversationsList = await fetchConversationsList({ opisn: userId }); addToConversationList(conversationsList); const templates = await fetchTemplates(); setTemplates(templates); + + setInitial(true); }, }))); diff --git a/src/views/Conversations/Components/ConversationsList.jsx b/src/views/Conversations/Components/ConversationsList.jsx index ba773fc..a778263 100644 --- a/src/views/Conversations/Components/ConversationsList.jsx +++ b/src/views/Conversations/Components/ConversationsList.jsx @@ -46,7 +46,7 @@ const Conversations = () => { const navigate = useNavigate(); const { loginUser } = useAuthContext(); const { userId } = loginUser; - const { activeConversations, currentConversation, conversationsList, addToConversationList, setCurrentConversation, receivedMessageList, setMsgLoading } = useConversationStore(); + const { initialState, activeConversations, currentConversation, conversationsList, addToConversationList, setCurrentConversation, receivedMessageList, setMsgLoading } = useConversationStore(); const [chatlist, setChatlist] = useState([]); useEffect(() => { setChatlist( @@ -76,12 +76,12 @@ const Conversations = () => { const [shouldFetchCList, setShouldFetchCList] = useState(true); useEffect(() => { - if (order_sn && shouldFetchCList) { + if (order_sn && shouldFetchCList && initialState) { getOrderConversationList(order_sn); } return () => {}; - }, [order_sn, shouldFetchCList]); + }, [order_sn, shouldFetchCList, initialState]); const getOrderConversationList = async (colisn) => { const { whatsapp_phone_number } = switchToC; @@ -89,8 +89,11 @@ const Conversations = () => { const data = await fetchOrderConversationsList({ opisn: userId, colisn: colisn, whatsappid: whatsappID }); if (!isEmpty(data)) { addToConversationList(data); - const ifCurrent = data.findIndex((item) => item.sn === currentConversation.sn); - switchConversation(data[ifCurrent === -1 ? 0 : ifCurrent]); + } + // const ifCurrent = data.findIndex((item) => item.sn === currentConversation.sn); + const ifCurrent = conversationsList.findIndex((item) => item.coli_sn === Number(colisn)); + if (ifCurrent !== -1) { + switchConversation(conversationsList[ifCurrent === -1 ? 0 : ifCurrent]); } else { // reset chat window setCurrentConversation({ sn: '', customer_name: '', coli_sn: order_sn });