Skip to content

Commit

Permalink
fix loading page issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zuies committed Oct 3, 2023
1 parent 32ee1c4 commit 31aea1c
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions src/pages/growth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function growth() {
},
});

const [loading, setLoading] = useState(false);
const [loading, setLoading] = useState<boolean>(true);

const {
twitterActivityAccount,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 31aea1c

Please sign in to comment.