Skip to content

Commit 2a21dd8

Browse files
committed
feat(clerk-js,shared): Introduce private unstable__mutate to force mutate swr state
1 parent 7874110 commit 2a21dd8

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

packages/clerk-js/src/ui/components/OrganizationProfile/ActiveMembersList.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ export const ActiveMembersList = () => {
1616
organization,
1717
membershipList,
1818
membership: currentUserMembership,
19+
...rest
1920
} = useCoreOrganization({
2021
membershipList: { offset: (page - 1) * ITEMS_PER_PAGE, limit: ITEMS_PER_PAGE },
2122
});
2223
const isAdmin = currentUserMembership?.role === 'admin';
2324

25+
const mutateSwrState = () => {
26+
const unstable__mutate = (rest as any).unstable__mutate;
27+
if (unstable__mutate && typeof unstable__mutate === 'function') {
28+
unstable__mutate();
29+
}
30+
};
31+
2432
if (!organization) {
2533
return null;
2634
}
@@ -36,7 +44,10 @@ export const ActiveMembersList = () => {
3644
if (!isAdmin) {
3745
return;
3846
}
39-
return card.runAsync(membership.destroy()).catch(err => handleError(err, [], card.setError));
47+
return card
48+
.runAsync(membership.destroy())
49+
.then(mutateSwrState)
50+
.catch(err => handleError(err, [], card.setError));
4051
};
4152

4253
return (

packages/clerk-js/src/ui/components/OrganizationProfile/InvitedMembersList.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ const ITEMS_PER_PAGE = 10;
1212
export const InvitedMembersList = () => {
1313
const card = useCardState();
1414
const { page, changePage } = usePagination();
15-
const { organization, invitationList } = useCoreOrganization({
15+
const { organization, invitationList, ...rest } = useCoreOrganization({
1616
invitationList: { offset: (page - 1) * ITEMS_PER_PAGE, limit: ITEMS_PER_PAGE },
1717
});
1818

19+
const mutateSwrState = () => {
20+
const unstable__mutate = (rest as any).unstable__mutate;
21+
if (unstable__mutate && typeof unstable__mutate === 'function') {
22+
unstable__mutate();
23+
}
24+
};
25+
1926
if (!organization) {
2027
return null;
2128
}
2229

2330
const revoke = (invitation: OrganizationInvitationResource) => () => {
2431
return card
2532
.runAsync(invitation.revoke)
33+
.then(mutateSwrState)
2634
.then(() => changePage(1))
2735
.catch(err => handleError(err, [], card.setError));
2836
};

packages/shared/src/hooks/useOrganization.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,22 @@ export const useOrganization: UseOrganization = params => {
5858
? () => [] as OrganizationMembershipResource[]
5959
: () => clerk.organization?.getMemberships(membershipListParams);
6060

61-
const { data: invitationList, isValidating: isInvitationsLoading } = useSWR(
61+
const {
62+
data: invitationList,
63+
isValidating: isInvitationsLoading,
64+
mutate: mutateInvitationList,
65+
} = useSWR(
6266
shouldFetch && invitationListParams
6367
? cacheKey('invites', organization, lastOrganizationInvitation, invitationListParams)
6468
: null,
6569
pendingInvitations,
6670
);
6771

68-
const { data: membershipList, isValidating: isMembershipsLoading } = useSWR(
72+
const {
73+
data: membershipList,
74+
isValidating: isMembershipsLoading,
75+
mutate: mutateMembershipList,
76+
} = useSWR(
6977
shouldFetch && membershipListParams
7078
? cacheKey('memberships', organization, lastOrganizationMember, membershipListParams)
7179
: null,
@@ -109,6 +117,10 @@ export const useOrganization: UseOrganization = params => {
109117
membershipList,
110118
membership: getCurrentOrganizationMembership(session!.user.organizationMemberships, organization.id), // your membership in the current org
111119
invitationList,
120+
unstable__mutate: () => {
121+
void mutateMembershipList();
122+
void mutateInvitationList();
123+
},
112124
};
113125
};
114126

0 commit comments

Comments
 (0)