From 31aea1cb589c75c6bbdec832553e5424275a8006 Mon Sep 17 00:00:00 2001 From: zuies Date: Tue, 3 Oct 2023 18:19:28 +0300 Subject: [PATCH] fix loading page issue --- src/pages/growth.tsx | 70 ++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/src/pages/growth.tsx b/src/pages/growth.tsx index ac297dc3..8cd3cee8 100644 --- a/src/pages/growth.tsx +++ b/src/pages/growth.tsx @@ -42,7 +42,7 @@ function growth() { }, }); - const [loading, setLoading] = useState(false); + const [loading, setLoading] = useState(true); const { twitterActivityAccount, @@ -76,42 +76,48 @@ function growth() { new Date().toISOString() ); } + return; }; useEffect(() => { - const twitterId = user?.twitter?.twitterId; + const fetchTwitterMetrics = async () => { + const twitterId = user?.twitter?.twitterId; + + if (!twitterId) { + setLoading(false); + return; + } + + setLoading(true); + + try { + const [ + activityResponse, + audienceResponse, + engagementResponse, + accountResponse, + ] = await Promise.all([ + twitterActivityAccount(), + twitterAudienceAccount(), + twitterEngagementAccount(), + twitterAccount(), + ]); + + setData({ + activity: activityResponse, + audience: audienceResponse, + engagement: engagementResponse, + account: accountResponse, + }); + } catch (err) { + console.error(err); + } finally { + setLoading(false); + } + }; updateTwitterMetrics(); - - setLoading(true); - if (twitterId) { - Promise.all([ - twitterActivityAccount(), - twitterAudienceAccount(), - twitterEngagementAccount(), - twitterAccount(), - ]) - .then( - ([ - activityResponse, - audienceResponse, - engagementResponse, - accountResponse, - ]) => { - setData({ - activity: activityResponse, - audience: audienceResponse, - engagement: engagementResponse, - account: accountResponse, - }); - setLoading(false); - } - ) - .catch((err) => { - setLoading(false); - }); - } - setLoading(false); + fetchTwitterMetrics(); }, []); if (loading) {