From 5adb4a7f1d3408aaace235e50b19fba4640112ba Mon Sep 17 00:00:00 2001 From: Impeqq Date: Sat, 20 Nov 2021 22:32:54 +0400 Subject: [PATCH] reset chat list after logout --- .../components/sidebar/chats-list/chats-list.tsx | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/sidebar/chats-list/chats-list.tsx b/frontend/src/components/sidebar/chats-list/chats-list.tsx index 93e77e2..77d9645 100644 --- a/frontend/src/components/sidebar/chats-list/chats-list.tsx +++ b/frontend/src/components/sidebar/chats-list/chats-list.tsx @@ -1,11 +1,13 @@ -import { useState } from "react"; +import { useEffect, useState } from "react"; import styles from "./styles.scss"; import { SiebarItem, UserItem, UserLocations } from "@ui"; -import { useQuery, useSubscription } from "@apollo/client"; +import { useLazyQuery, useQuery, useSubscription } from "@apollo/client"; import { FETCH_ME, FETCH_MY_CHATS, SUBSCRIBE_MY_CHAT } from "@schemas"; import { RouteParams, TChat, TUser } from "@features/types"; import { useHistory, useParams } from "react-router-dom"; import { routePath } from "@pages/routes"; +import { useLocalStorage } from "@features/hooks"; +import { AUTH_TOKEN } from "@features/constants"; type TProps = { toggleSidebar: () => void; @@ -13,7 +15,8 @@ type TProps = { export const ChatsList = ({ toggleSidebar }: TProps) => { const [chats, setChats] = useState([]); - + const { getItem } = useLocalStorage(); + const isGuest = !getItem(AUTH_TOKEN); const { data: userData } = useQuery(FETCH_ME, { fetchPolicy: "cache-only" }); const me = userData?.me; @@ -21,12 +24,16 @@ export const ChatsList = ({ toggleSidebar }: TProps) => { const { id } = useParams(); - const { loading } = useQuery(FETCH_MY_CHATS, { + const [fetchMyChats, { loading }] = useLazyQuery(FETCH_MY_CHATS, { onCompleted: ({ getMyChats: data }) => { setChats(data); }, }); + useEffect(() => { + fetchMyChats(); + }, [fetchMyChats, isGuest]); + useSubscription(SUBSCRIBE_MY_CHAT, { variables: { user_id: me?.id }, onSubscriptionData: ({