From 8674dd4ba6fbdca44fc2d13e1ceb2ab387e3fa70 Mon Sep 17 00:00:00 2001 From: zuies Date: Tue, 3 Oct 2023 12:55:24 +0300 Subject: [PATCH] fix issue on update twitter status --- .../pages/settings/ConnectedTwitter.tsx | 35 +++++++++++-------- src/layouts/defaultLayout.tsx | 9 +++++ src/pages/settings.tsx | 1 - src/store/slices/twitterSlice.ts | 3 ++ src/store/types/ITwitter.ts | 1 + 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/components/pages/settings/ConnectedTwitter.tsx b/src/components/pages/settings/ConnectedTwitter.tsx index e6968be7..de5be7b0 100644 --- a/src/components/pages/settings/ConnectedTwitter.tsx +++ b/src/components/pages/settings/ConnectedTwitter.tsx @@ -15,21 +15,28 @@ function ConnectedTwitter({ twitter }: IConnectedTwitter) { const { disconnectTwitter, getUserInfo } = useAppStore(); const handleDisconnect = async () => { - await disconnectTwitter(); - const { - twitterConnectedAt, - twitterId, - twitterProfileImageUrl, - twitterUsername, - } = await getUserInfo(); + try { + await disconnectTwitter(); - StorageService.updateLocalStorageWithObject('user', 'twitter', { - twitterConnectedAt, - twitterId, - twitterProfileImageUrl, - twitterUsername, - }); - StorageService.removeLocalStorage('lastTwitterMetricsRefreshDate'); + const userInfo = await getUserInfo(); + const { + twitterConnectedAt, + twitterId, + twitterProfileImageUrl, + twitterUsername, + } = userInfo; + + StorageService.updateLocalStorageWithObject('user', 'twitter', { + twitterConnectedAt, + twitterId, + twitterProfileImageUrl, + twitterUsername, + }); + + StorageService.removeLocalStorage('lastTwitterMetricsRefreshDate'); + } catch (error) { + console.error('Error handling disconnect:', error); + } }; return ( diff --git a/src/layouts/defaultLayout.tsx b/src/layouts/defaultLayout.tsx index fe71bd8e..983d9df9 100644 --- a/src/layouts/defaultLayout.tsx +++ b/src/layouts/defaultLayout.tsx @@ -37,6 +37,7 @@ export const defaultLayout = ({ children }: IDefaultLayoutProps) => { if (guildId) { getGuildInfoByDiscord(guildId); } + const fetchUserInfo = async () => { const { twitterConnectedAt, @@ -52,7 +53,15 @@ export const defaultLayout = ({ children }: IDefaultLayoutProps) => { twitterUsername, }); }; + + // Immediately call fetchUserInfo once fetchUserInfo(); + + // Set up the interval to call fetchUserInfo every 5 seconds + const intervalId = setInterval(fetchUserInfo, 5000); + + // Return a cleanup function to clear the interval when the component unmounts + return () => clearInterval(intervalId); } }, []); diff --git a/src/pages/settings.tsx b/src/pages/settings.tsx index 33b38f62..86d5d0cf 100644 --- a/src/pages/settings.tsx +++ b/src/pages/settings.tsx @@ -48,7 +48,6 @@ function Settings(): JSX.Element { fetchEmail(); const intervalId = setInterval(() => { getGuilds(); - getUserInfo(); }, 5000); // Clean up the interval when the component unmounts diff --git a/src/store/slices/twitterSlice.ts b/src/store/slices/twitterSlice.ts index 6175ea82..24f718f9 100644 --- a/src/store/slices/twitterSlice.ts +++ b/src/store/slices/twitterSlice.ts @@ -6,6 +6,7 @@ import { conf } from '../../configs'; const BASE_URL = conf.API_BASE_URL; const createTwitterSlice: StateCreator = (set, get) => ({ + isLoading: false, authorizeTwitter: async (discordId: string) => { try { location.replace(`${BASE_URL}/auth/twitter/login/user/${discordId}`); @@ -15,7 +16,9 @@ const createTwitterSlice: StateCreator = (set, get) => ({ }, disconnectTwitter: async () => { try { + set(() => ({ isLoading: true })); await axiosInstance.post(`twitter/disconnect`); + set(() => ({ isLoading: false })); } catch (error) {} }, refreshTwitterMetrics: async () => { diff --git a/src/store/types/ITwitter.ts b/src/store/types/ITwitter.ts index a817cb73..7bacc8cc 100644 --- a/src/store/types/ITwitter.ts +++ b/src/store/types/ITwitter.ts @@ -1,4 +1,5 @@ export default interface ITwitter { + isLoading: boolean; authorizeTwitter: (discordId: string) => void; disconnectTwitter: () => void; refreshTwitterMetrics: () => void;