Skip to content

Commit bc4508d

Browse files
authored
fix(clerk-js): Revalidate when user deletes organization (#2473) (#2504)
* fix(clerk-js): Revalidate when user deletes organization (#2473) * fix(clerk-js): Fix issues after resolving conflicts
1 parent 71b4b9c commit bc4508d

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

.changeset/giant-rice-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Update user memberships when user creates, leaves or deletes an organization.

packages/clerk-js/src/ui/components/CreateOrganization/CreateOrganizationForm.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { colors, createSlug, handleError, useFormControl } from '../../utils';
1212
import { InviteMembersForm } from '../OrganizationProfile/InviteMembersForm';
1313
import { InvitationsSentMessage } from '../OrganizationProfile/InviteMembersPage';
1414
import { OrganizationProfileAvatarUploader } from '../OrganizationProfile/OrganizationProfileAvatarUploader';
15+
import { organizationListParams } from '../OrganizationSwitcher/utils';
1516

1617
type CreateOrganizationFormProps = {
1718
skipInvitationScreen: boolean;
@@ -30,7 +31,9 @@ export const CreateOrganizationForm = (props: CreateOrganizationFormProps) => {
3031
const wizard = useWizard({ onNextStep: () => card.setError(undefined) });
3132

3233
const lastCreatedOrganizationRef = React.useRef<OrganizationResource | null>(null);
33-
const { createOrganization, isLoaded, setActive } = useCoreOrganizationList();
34+
const { createOrganization, isLoaded, setActive, userMemberships } = useCoreOrganizationList({
35+
userMemberships: organizationListParams.userMemberships,
36+
});
3437
const { organization } = useCoreOrganization();
3538
const [file, setFile] = React.useState<File | null>();
3639

@@ -68,6 +71,8 @@ export const CreateOrganizationForm = (props: CreateOrganizationFormProps) => {
6871
lastCreatedOrganizationRef.current = organization;
6972
await setActive({ organization });
7073

74+
void userMemberships.revalidate?.();
75+
7176
if (props.skipInvitationScreen ?? organization.maxAllowedMemberships === 1) {
7277
return completeFlow();
7378
}

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

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { useWizard, Wizard } from '../../common';
2-
import { useCoreOrganization, useCoreUser, useOrganizationProfileContext } from '../../contexts';
2+
import {
3+
useCoreOrganization,
4+
useCoreOrganizationList,
5+
useCoreUser,
6+
useOrganizationProfileContext,
7+
} from '../../contexts';
38
import type { LocalizationKey } from '../../customizables';
49
import { localizationKeys, Text } from '../../customizables';
510
import {
@@ -12,22 +17,39 @@ import {
1217
} from '../../elements';
1318
import { useRouter } from '../../router';
1419
import { handleError, useFormControl } from '../../utils';
20+
import { organizationListParams } from '../OrganizationSwitcher/utils';
1521
import { OrganizationProfileBreadcrumbs } from './OrganizationProfileNavbar';
1622

17-
export const LeaveOrganizationPage = () => {
23+
const useLeaveWithRevalidations = (leavePromise: (() => Promise<any>) | undefined) => {
1824
const card = useCardState();
1925
const { navigateAfterLeaveOrganization } = useOrganizationProfileContext();
26+
const { userMemberships, userInvitations } = useCoreOrganizationList({
27+
userMemberships: organizationListParams.userMemberships,
28+
userInvitations: organizationListParams.userInvitations,
29+
});
30+
31+
return () =>
32+
card
33+
.runAsync(async () => {
34+
await leavePromise?.();
35+
})
36+
.then(() => {
37+
void userMemberships.revalidate?.();
38+
void userInvitations.revalidate?.();
39+
void navigateAfterLeaveOrganization();
40+
});
41+
};
42+
43+
export const LeaveOrganizationPage = () => {
2044
const { organization } = useCoreOrganization();
2145
const user = useCoreUser();
2246

23-
if (!organization) {
47+
const leaveOrg = useLeaveWithRevalidations(organization ? () => user.leaveOrganization(organization.id) : undefined);
48+
49+
if (!organization || !user) {
2450
return null;
2551
}
2652

27-
const leave = () => {
28-
return card.runAsync(user.leaveOrganization(organization.id)).then(navigateAfterLeaveOrganization);
29-
};
30-
3153
return (
3254
<ActionConfirmationPage
3355
organizationName={organization?.name}
@@ -42,24 +64,20 @@ export const LeaveOrganizationPage = () => {
4264
successMessage={localizationKeys(
4365
'organizationProfile.profilePage.dangerSection.leaveOrganization.successMessage',
4466
)}
45-
onConfirmation={leave}
67+
onConfirmation={leaveOrg}
4668
/>
4769
);
4870
};
4971

5072
export const DeleteOrganizationPage = () => {
51-
const card = useCardState();
52-
const { navigateAfterLeaveOrganization } = useOrganizationProfileContext();
53-
const { organization } = useCoreOrganization();
73+
const { organization, membership } = useCoreOrganization();
74+
75+
const deleteOrg = useLeaveWithRevalidations(organization?.destroy);
5476

55-
if (!organization) {
77+
if (!organization || !membership) {
5678
return null;
5779
}
5880

59-
const deleteOrg = () => {
60-
return card.runAsync(organization.destroy()).then(navigateAfterLeaveOrganization);
61-
};
62-
6381
return (
6482
<ActionConfirmationPage
6583
organizationName={organization?.name}

0 commit comments

Comments
 (0)