Skip to content

Commit 3b32297

Browse files
committed
Do not display error screen if there are existing resources
1 parent b800900 commit 3b32297

File tree

2 files changed

+78
-17
lines changed

2 files changed

+78
-17
lines changed

packages/clerk-js/src/ui/components/SessionTasks/tasks/TaskChooseOrganization/__tests__/TaskChooseOrganization.test.tsx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,74 @@ describe('TaskChooseOrganization', () => {
240240
expect(queryByLabelText(/Slug/i)).toBeInTheDocument();
241241
});
242242
});
243+
244+
describe('when users are not allowed to create organizations', () => {
245+
it('does not display create organization screen', async () => {
246+
const { wrapper } = await createFixtures(f => {
247+
f.withOrganizations();
248+
f.withForceOrganizationSelection();
249+
f.withUser({
250+
create_organization_enabled: false,
251+
tasks: [{ key: 'choose-organization' }],
252+
});
253+
});
254+
255+
const { queryByText } = render(<TaskChooseOrganization />, { wrapper });
256+
257+
expect(queryByText(/create new organization/i)).not.toBeInTheDocument();
258+
expect(queryByText(/you must belong to an organization/i)).toBeInTheDocument();
259+
expect(queryByText(/contact your organization admin for an invitation/i)).toBeInTheDocument();
260+
});
261+
262+
it('with existing memberships or suggestions, displays create organization screen', async () => {
263+
const { wrapper, fixtures } = await createFixtures(f => {
264+
f.withOrganizations();
265+
f.withForceOrganizationSelection();
266+
f.withUser({
267+
create_organization_enabled: false,
268+
});
269+
});
270+
271+
fixtures.clerk.user?.getOrganizationMemberships.mockReturnValueOnce(
272+
Promise.resolve({
273+
data: [
274+
createFakeUserOrganizationMembership({
275+
id: '1',
276+
organization: {
277+
id: '1',
278+
name: 'Existing Org',
279+
slug: 'org1',
280+
membersCount: 1,
281+
adminDeleteEnabled: false,
282+
maxAllowedMemberships: 1,
283+
pendingInvitationsCount: 1,
284+
},
285+
}),
286+
],
287+
total_count: 1,
288+
}),
289+
);
290+
291+
fixtures.clerk.user?.getOrganizationSuggestions.mockReturnValueOnce(
292+
Promise.resolve({
293+
data: [
294+
createFakeUserOrganizationSuggestion({
295+
id: '2',
296+
emailAddress: '[email protected]',
297+
publicOrganizationData: {
298+
name: 'OrgTwoSuggestion',
299+
},
300+
}),
301+
],
302+
total_count: 1,
303+
}),
304+
);
305+
306+
const { findByText, queryByRole } = render(<TaskChooseOrganization />, { wrapper });
307+
308+
expect(await findByText('Existing Org')).toBeInTheDocument();
309+
expect(await findByText('Create new organization')).toBeInTheDocument();
310+
expect(queryByRole('textbox', { name: /name/i })).not.toBeInTheDocument();
311+
});
312+
});
243313
});

packages/clerk-js/src/ui/components/SessionTasks/tasks/TaskChooseOrganization/index.tsx

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useClerk, useSession, useUser } from '@clerk/shared/react';
2-
import { type ComponentType, useState } from 'react';
2+
import { useState } from 'react';
33

44
import { useSignOutContext, withCoreSessionSwitchGuard } from '@/ui/contexts';
55
import { descriptors, Flex, Flow, localizationKeys, Spinner } from '@/ui/customizables';
@@ -14,10 +14,16 @@ import { ChooseOrganizationScreen } from './ChooseOrganizationScreen';
1414
import { CreateOrganizationScreen } from './CreateOrganizationScreen';
1515

1616
const TaskChooseOrganizationInternal = () => {
17+
const { user } = useUser();
1718
const { userMemberships, userSuggestions, userInvitations } = useOrganizationListInView();
1819

1920
const isLoading = userMemberships?.isLoading || userInvitations?.isLoading || userSuggestions?.isLoading;
2021
const hasExistingResources = !!(userMemberships?.count || userInvitations?.count || userSuggestions?.count);
22+
const isOrganizationCreationDisabled = !isLoading && !user?.createOrganizationEnabled && !hasExistingResources;
23+
24+
if (isOrganizationCreationDisabled) {
25+
return <OrganizationCreationDisabledScreen />;
26+
}
2127

2228
return (
2329
<Flow.Root flow='taskChooseOrganization'>
@@ -113,18 +119,6 @@ const TaskChooseOrganizationFlows = withCardStateProvider((props: TaskChooseOrga
113119
return <ChooseOrganizationScreen onCreateOrganizationClick={() => setCurrentFlow('create')} />;
114120
});
115121

116-
export const withOrganizationCreationEnabledGuard = <T extends object>(Component: ComponentType<T>) => {
117-
return (props: T) => {
118-
const { user } = useUser();
119-
120-
if (!user?.createOrganizationEnabled) {
121-
return <OrganizationCreationDisabledScreen />;
122-
}
123-
124-
return <Component {...props} />;
125-
};
126-
};
127-
128122
function OrganizationCreationDisabledScreen() {
129123
return (
130124
<Flow.Root flow='taskChooseOrganization'>
@@ -149,8 +143,5 @@ function OrganizationCreationDisabledScreen() {
149143
}
150144

151145
export const TaskChooseOrganization = withCoreSessionSwitchGuard(
152-
withTaskGuard(
153-
withCardStateProvider(withOrganizationCreationEnabledGuard(TaskChooseOrganizationInternal)),
154-
'choose-organization',
155-
),
146+
withTaskGuard(withCardStateProvider(TaskChooseOrganizationInternal), 'choose-organization'),
156147
);

0 commit comments

Comments
 (0)