Skip to content

Commit

Permalink
Merge pull request #201 from TogetherCrew/fix/twitter-growth-loading
Browse files Browse the repository at this point in the history
fix loading page issue
  • Loading branch information
mehdi-torabiv authored Oct 3, 2023
2 parents 32ee1c4 + 31aea1c commit dd1d950
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 dd1d950

Please sign in to comment.