Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: refine data fetching flow #3614

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading