Skip to content

Commit

Permalink
refine data fetching flow (#3614)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-adams authored Oct 11, 2024
1 parent 767c92b commit 7b7249c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
56 changes: 36 additions & 20 deletions packages/client/src/components/GrantActivity.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
size="lg"
:variant="followBtnVariant"
data-follow-btn
:disabled="!followStateLoaded"
:disabled="!userFollowStateLoaded"
@click="toggleFollowState"
>
<span class="h4">
Expand Down Expand Up @@ -85,7 +85,7 @@ export default {
data() {
return {
userIsFollowing: false,
followStateLoaded: false,
userFollowStateLoaded: false,
followers: [],
notes: [],
grantFollowersModalKey: 0,
Expand Down Expand Up @@ -147,21 +147,29 @@ export default {
unfollowGrantForCurrentUser: 'grants/unfollowGrantForCurrentUser',
}),
fetchFollowAndNotes() {
this.followStateLoaded = false;
this.fetchFollowState();
this.fetchAllFollowState();
this.fetchAllNotes();
},
async fetchFollowState() {
const followCalls = [
this.getFollowerForGrant({ grantId: this.currentGrant.grant_id }),
this.getFollowersForGrant({ grantId: this.currentGrant.grant_id, limit: 51 }),
];
async fetchAllFollowState() {
const [userResult, followersResult] = await Promise.all([
this.fetchUserFollowState(),
this.fetchAllFollowersState(),
]);
const [userFollowsResult, followersResult] = await Promise.all(followCalls);
this.userIsFollowing = Boolean(userFollowsResult);
this.followers = followersResult ? followersResult.followers : [];
this.followStateLoaded = true;
this.setFollowState({ user: userResult, followers: followersResult });
},
async fetchUserFollowState() {
const userResult = await this.getFollowerForGrant({ grantId: this.currentGrant.grant_id });
this.userFollowStateLoaded = true;
return userResult;
},
async fetchAllFollowersState() {
const followers = await this.getFollowersForGrant({ grantId: this.currentGrant.grant_id, limit: 51 });
return followers;
},
setFollowState({ user, followers }) {
this.userIsFollowing = Boolean(user);
this.followers = (followers?.followers || []);
},
async fetchAllNotes() {
const result = await this.getNotesForGrant({ grantId: this.currentGrant.grant_id, limit: 51 });
Expand All @@ -171,13 +179,21 @@ export default {
}
},
async toggleFollowState() {
this.followStateLoaded = false;
if (this.userIsFollowing) {
await this.unfollowGrantForCurrentUser({ grantId: this.currentGrant.grant_id });
} else {
await this.followGrantForCurrentUser({ grantId: this.currentGrant.grant_id });
this.userFollowStateLoaded = false;
try {
if (this.userIsFollowing) {
await this.unfollowGrantForCurrentUser({ grantId: this.currentGrant.grant_id });
} else {
await this.followGrantForCurrentUser({ grantId: this.currentGrant.grant_id });
}
const followers = await this.fetchAllFollowersState();
this.setFollowState({ user: !this.userIsFollowing, followers });
} catch (e) {
// Error is logged already, catch to allow recovery
}
await this.fetchFollowState();
this.userFollowStateLoaded = true;
},
handleModalClose() {
// Reset modal
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/store/modules/grants.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export default {
const text = `Error following grant: ${e.message}`;
commit('alerts/addAlert', { text, level: 'err' }, { root: true });
datadogRum.addError(e, { grantId, text });
throw e;
}
},
async unfollowGrantForCurrentUser({ rootGetters, commit }, { grantId }) {
Expand All @@ -298,6 +299,7 @@ export default {
const text = `Error unfollowing grant: ${e.message}`;
commit('alerts/addAlert', { text, level: 'err' }, { root: true });
datadogRum.addError(e, { grantId, text });
throw e;
}
},
async setEligibilityCodeEnabled({ rootGetters }, { code, enabled }) {
Expand Down

0 comments on commit 7b7249c

Please sign in to comment.