diff --git a/client/src/components/chat-affix/registerMessagingSocketHandlers.js b/client/src/components/chat-affix/registerMessagingSocketHandlers.js index 49bce2ff8..8034353c8 100644 --- a/client/src/components/chat-affix/registerMessagingSocketHandlers.js +++ b/client/src/components/chat-affix/registerMessagingSocketHandlers.js @@ -250,6 +250,7 @@ export const registerMessagingHandlers = ({ socket, client }) => { const { conversationId, type, job_conversations, ...fields } = data; logLocal("handleConversationChanged", data); + console.log(`--------------${type}-----------------`); const updatedAt = new Date().toISOString(); @@ -296,85 +297,18 @@ export const registerMessagingHandlers = ({ socket, client }) => { return; } - if (type === "conversation-archived") { + if (type === "conversation-unarchived" || type === "conversation-archived") { try { - // Evict messages associated with the conversation - const messageRefs = client.cache.readFragment({ - id: cacheId, - fragment: gql` - fragment ConversationMessages on conversations { - messages { - id - } - } - ` - }); - - messageRefs?.messages?.forEach((message) => { - const messageCacheId = client.cache.identify({ - __typename: "messages", - id: message.id - }); - if (messageCacheId) { - client.cache.evict({ id: messageCacheId }); - } - }); - - // Evict the conversation itself - client.cache.evict({ id: cacheId }); - client.cache.gc(); // Trigger garbage collection - } catch (error) { - console.error("Error archiving conversation:", error); - } - return; - } - - if (type === "conversation-unarchived") { - try { - // Fetch the conversation from the database if not already in the cache - const existingConversation = client.cache.readQuery({ - query: GET_CONVERSATION_DETAILS, - variables: { conversationId } - }); - - if (!existingConversation) { - const { data: fetchedData } = await client.query({ - query: GET_CONVERSATION_DETAILS, - variables: { conversationId }, - fetchPolicy: "network-only" - }); - - if (fetchedData?.conversations_by_pk) { - const conversationData = fetchedData.conversations_by_pk; - - // Enrich conversation data - const enrichedConversation = { - ...conversationData, - messages_aggregate: { - __typename: "messages_aggregate", - aggregate: { - __typename: "messages_aggregate_fields", - count: conversationData.messages.filter((message) => !message.read && !message.isoutbound).length - } - }, - updated_at: updatedAt - }; - - updateConversationList(enrichedConversation); - } - } - - // Mark the conversation as unarchived in the cache - client.cache.modify({ - id: cacheId, - fields: { - archived: () => false, - updated_at: () => updatedAt - } + // Refetch the conversation list query to update the UI + const queryVariables = { offset: 0 }; + await client.refetchQueries({ + include: [CONVERSATION_LIST_QUERY], + variables: queryVariables }); } catch (error) { - console.error("Error unarchiving conversation:", error); + console.error("Error refetching conversation list after unarchiving:", error); } + return; } diff --git a/client/src/redux/messaging/messaging.sagas.js b/client/src/redux/messaging/messaging.sagas.js index fa17c6db5..2ee7a8bac 100644 --- a/client/src/redux/messaging/messaging.sagas.js +++ b/client/src/redux/messaging/messaging.sagas.js @@ -34,6 +34,7 @@ export function* onOpenChatByPhone() { export function* openChatByPhone({ payload }) { logImEXEvent("messaging_open_by_phone"); const { socket, phone_num, jobid } = payload; + if (!socket || !phone_num) return; const p = parsePhoneNumber(phone_num, "CA"); const bodyshop = yield select(selectBodyshop); @@ -67,7 +68,7 @@ export function* openChatByPhone({ payload }) { const createdConversation = newConversations[0]; // Get the newly created conversation - // Emit event for new conversation with full details + // // Emit event for new conversation with full details if (socket) { socket.emit("conversation-modified", { bodyshopId: bodyshop.id,