From 046d104bfa6ef323c0b3a91877907e9c8e6fe014 Mon Sep 17 00:00:00 2001 From: swtmply Date: Fri, 9 Jun 2023 01:04:59 +0800 Subject: [PATCH] IO-2208 removed subscriptions to the message --- .../chat-conversation-list.component.jsx | 7 -- .../chat-popup/chat-popup.component.jsx | 64 ++++--------------- client/src/graphql/conversations.queries.js | 44 ------------- yarn.lock | 11 +--- 4 files changed, 17 insertions(+), 109 deletions(-) diff --git a/client/src/components/chat-conversation-list/chat-conversation-list.component.jsx b/client/src/components/chat-conversation-list/chat-conversation-list.component.jsx index ffc087ae2..907f8d5db 100644 --- a/client/src/components/chat-conversation-list/chat-conversation-list.component.jsx +++ b/client/src/components/chat-conversation-list/chat-conversation-list.component.jsx @@ -24,15 +24,8 @@ function ChatConversationListComponent({ conversationList, selectedConversation, setSelectedConversation, - subscribeToMoreConversations, loadMoreConversations, }) { - useEffect( - () => subscribeToMoreConversations(), - // eslint-disable-next-line react-hooks/exhaustive-deps - [] - ); - const rowRenderer = ({ index, key, style }) => { const item = conversationList[index]; diff --git a/client/src/components/chat-popup/chat-popup.component.jsx b/client/src/components/chat-popup/chat-popup.component.jsx index 1c80d7c4c..c179fe4b5 100644 --- a/client/src/components/chat-popup/chat-popup.component.jsx +++ b/client/src/components/chat-popup/chat-popup.component.jsx @@ -4,7 +4,7 @@ import { ShrinkOutlined, SyncOutlined, } from "@ant-design/icons"; -import { useLazyQuery, useSubscription } from "@apollo/client"; +import { useLazyQuery, useQuery } from "@apollo/client"; import { Badge, Card, Col, Row, Space, Tag, Tooltip, Typography } from "antd"; import React, { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; @@ -13,7 +13,7 @@ import { createStructuredSelector } from "reselect"; import { CONVERSATION_LIST_QUERY, CONVERSATION_LIST_SUBSCRIPTION, - UNREAD_CONVERSATION_COUNT_SUBSCRIPTION, + UNREAD_CONVERSATION_COUNT, } from "../../graphql/conversations.queries"; import { toggleChatVisible } from "../../redux/messaging/messaging.actions"; import { @@ -42,19 +42,20 @@ export function ChatPopupComponent({ const { t } = useTranslation(); const [pollInterval, setpollInterval] = useState(0); - const { data: unreadData } = useSubscription( - UNREAD_CONVERSATION_COUNT_SUBSCRIPTION - ); - - const [ - getConversations, - { loading, data, called, refetch, fetchMore, subscribeToMore }, - ] = useLazyQuery(CONVERSATION_LIST_QUERY, { + const { data: unreadData } = useQuery(UNREAD_CONVERSATION_COUNT, { fetchPolicy: "network-only", nextFetchPolicy: "network-only", - skip: !chatVisible, + ...(pollInterval > 0 ? { pollInterval } : {}), }); + const [getConversations, { loading, data, refetch, fetchMore }] = + useLazyQuery(CONVERSATION_LIST_QUERY, { + fetchPolicy: "network-only", + nextFetchPolicy: "network-only", + skip: !chatVisible, + ...(pollInterval > 0 ? { pollInterval } : {}), + }); + const fcmToken = sessionStorage.getItem("fcmtoken"); useEffect(() => { @@ -66,13 +67,13 @@ export function ChatPopupComponent({ }, [fcmToken]); useEffect(() => { - if (called && chatVisible) + if (chatVisible) getConversations({ variables: { offset: 0, }, }); - }, [chatVisible, called, getConversations]); + }, [chatVisible, getConversations]); const loadMoreConversations = useCallback(() => { if (data) @@ -119,43 +120,6 @@ export function ChatPopupComponent({ - subscribeToMore({ - document: CONVERSATION_LIST_SUBSCRIPTION, - variables: { offset: 0 }, - updateQuery: (prev, { subscriptionData }) => { - if ( - !subscriptionData.data || - subscriptionData.data.conversations.length === 0 - ) - return prev; - - let conversations = [...prev.conversations]; - const newConversations = - subscriptionData.data.conversations; - - for (const conversation of newConversations) { - const index = conversations.findIndex( - (prevConversation) => - prevConversation.id === conversation.id - ); - - if (index !== -1) { - conversations.splice(index, 1); - conversations.unshift(conversation); - - continue; - } - - conversations.unshift(conversation); - } - - return Object.assign({}, prev, { - conversations: conversations, - }); - }, - }) - } /> )} diff --git a/client/src/graphql/conversations.queries.js b/client/src/graphql/conversations.queries.js index f9feb3f2f..315ba1a5d 100644 --- a/client/src/graphql/conversations.queries.js +++ b/client/src/graphql/conversations.queries.js @@ -1,37 +1,5 @@ import { gql } from "@apollo/client"; -export const CONVERSATION_LIST_SUBSCRIPTION = gql` - subscription CONVERSATION_LIST_SUBSCRIPTION($offset: Int!) { - conversations( - order_by: { updated_at: desc } - limit: 1 - offset: $offset - where: { archived: { _eq: false } } - ) { - phone_num - id - updated_at - unreadcnt - messages_aggregate( - where: { read: { _eq: false }, isoutbound: { _eq: false } } - ) { - aggregate { - count - } - } - job_conversations { - job { - id - ro_number - ownr_fn - ownr_ln - ownr_co_nm - } - } - } - } -`; - export const UNREAD_CONVERSATION_COUNT = gql` query UNREAD_CONVERSATION_COUNT { messages_aggregate( @@ -44,18 +12,6 @@ export const UNREAD_CONVERSATION_COUNT = gql` } `; -export const UNREAD_CONVERSATION_COUNT_SUBSCRIPTION = gql` - subscription UNREAD_CONVERSATION_COUNT_SUBSCRIPTION { - messages_aggregate( - where: { read: { _eq: false }, isoutbound: { _eq: false } } - ) { - aggregate { - count - } - } - } -`; - export const CONVERSATION_LIST_QUERY = gql` query CONVERSATION_LIST_QUERY($offset: Int!) { conversations( diff --git a/yarn.lock b/yarn.lock index 9659256b2..3226438a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4312,14 +4312,9 @@ tslib@^1.11.1: integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== tslib@^2.0.1, tslib@^2.1.0: - -tslib@^2.3.1, tslib@^2.5.0: - version "2.5.0" - resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.0.tgz#42bfed86f5787aeb41d031866c8f402429e0fddf" - integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg== - version "2.5.0" - resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.0.tgz#42bfed86f5787aeb41d031866c8f402429e0fddf" - integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg== + version "2.5.3" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.3.tgz#24944ba2d990940e6e982c4bea147aba80209913" + integrity sha512-mSxlJJwl3BMEQCUNnxXBU9jP4JBktcEGhURcPR6VQVlnP0FdDEsIaz0C35dXNGLyRfrATNofF0F5p2KPxQgB+w== tslib@^2.3.1, tslib@^2.5.0: version "2.5.0"