Skip to content

Commit 42c4bce

Browse files
committed
Extract force-an-org flow to separate component
1 parent bc7bb65 commit 42c4bce

File tree

1 file changed

+75
-18
lines changed

1 file changed

+75
-18
lines changed

packages/clerk-js/src/ui/components/OrganizationList/OrganizationListPage.tsx

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useOrganizationList, useSessionContext, useUser } from '@clerk/shared/react';
1+
import { useOrganizationList, useUser } from '@clerk/shared/react';
22
import { useContext, useState } from 'react';
33

44
import { Action, Actions } from '@/ui/elements/Actions';
@@ -84,6 +84,11 @@ export const OrganizationListPage = withCardStateProvider(() => {
8484

8585
const { hidePersonal } = useOrganizationListContext();
8686

87+
const sessionTasksContext = useContext(SessionTasksContext);
88+
if (sessionTasksContext) {
89+
return <ForceOrganizationSelectionFlow />;
90+
}
91+
8792
return (
8893
<Card.Root>
8994
<Card.Content sx={t => ({ padding: `${t.space.$8} ${t.space.$none} ${t.space.$none}` })}>
@@ -116,8 +121,6 @@ export const OrganizationListPage = withCardStateProvider(() => {
116121
const OrganizationListFlows = ({ showListInitially }: { showListInitially: boolean }) => {
117122
const { navigateAfterCreateOrganization, skipInvitationScreen, hideSlug } = useOrganizationListContext();
118123
const [isCreateOrganizationFlow, setCreateOrganizationFlow] = useState(!showListInitially);
119-
const sessionTasksContext = useContext(SessionTasksContext);
120-
const session = useSessionContext();
121124

122125
return (
123126
<>
@@ -133,23 +136,9 @@ const OrganizationListFlows = ({ showListInitially }: { showListInitially: boole
133136
>
134137
<CreateOrganizationForm
135138
flow='organizationList'
136-
onComplete={sessionTasksContext?.nextTask}
137139
startPage={{ headerTitle: localizationKeys('organizationList.createOrganization') }}
138140
skipInvitationScreen={skipInvitationScreen}
139-
navigateAfterCreateOrganization={org =>
140-
navigateAfterCreateOrganization(org).then(() => {
141-
const isForceOrganizationSelectionFlow = sessionTasksContext && session?.currentTask.key === 'org';
142-
143-
// During a force organization selection flow, keep displaying the creation form in a loading state
144-
// rather than showing the organization list. This allows the client-side navigation to complete
145-
// before transitioning away from this view.
146-
if (isForceOrganizationSelectionFlow) {
147-
return;
148-
}
149-
150-
setCreateOrganizationFlow(false);
151-
})
152-
}
141+
navigateAfterCreateOrganization={org => navigateAfterCreateOrganization(org)}
153142
onCancel={
154143
showListInitially && isCreateOrganizationFlow ? () => setCreateOrganizationFlow(false) : undefined
155144
}
@@ -240,3 +229,71 @@ const OrganizationListPageList = (props: { onCreateOrganizationClick: () => void
240229
</>
241230
);
242231
};
232+
233+
const ForceOrganizationSelectionFlow = () => {
234+
const card = useCardState();
235+
const sessionTasksContext = useContext(SessionTasksContext);
236+
const { navigateAfterCreateOrganization, hideSlug } = useOrganizationListContext();
237+
const { userMemberships, userSuggestions, userInvitations } = useOrganizationListInView();
238+
239+
const [isNavigating, setIsNavigating] = useState(false);
240+
const isLoading = isNavigating || !!(userMemberships?.count || userInvitations?.count || userSuggestions?.count);
241+
242+
const [isCreateOrganizationFlow, setIsCreateOrganizationFlow] = useState(!userMemberships?.data?.length);
243+
244+
if (isLoading) {
245+
return (
246+
<Card.Root>
247+
<Card.Content sx={t => ({ padding: `${t.space.$8} ${t.space.$none} ${t.space.$none}` })}>
248+
<Card.Alert sx={t => ({ margin: `${t.space.$none} ${t.space.$5}` })}>{card.error}</Card.Alert>
249+
<Flex
250+
direction={'row'}
251+
align={'center'}
252+
justify={'center'}
253+
sx={t => ({
254+
height: '100%',
255+
minHeight: t.sizes.$60,
256+
})}
257+
>
258+
<Spinner
259+
size={'lg'}
260+
colorScheme={'primary'}
261+
elementDescriptor={descriptors.spinner}
262+
/>
263+
</Flex>
264+
</Card.Content>
265+
<Card.Footer />
266+
</Card.Root>
267+
);
268+
}
269+
270+
return (
271+
<Card.Root>
272+
<Card.Content sx={t => ({ padding: `${t.space.$8} ${t.space.$none} ${t.space.$none}` })}>
273+
<Card.Alert sx={t => ({ margin: `${t.space.$none} ${t.space.$5}` })}>{card.error}</Card.Alert>
274+
{isCreateOrganizationFlow ? (
275+
<Box
276+
sx={t => ({
277+
padding: `${t.space.$none} ${t.space.$5} ${t.space.$5}`,
278+
})}
279+
>
280+
<CreateOrganizationForm
281+
flow='organizationList'
282+
onComplete={sessionTasksContext?.nextTask}
283+
startPage={{ headerTitle: localizationKeys('organizationList.createOrganization') }}
284+
skipInvitationScreen
285+
navigateAfterCreateOrganization={org => {
286+
setIsNavigating(true);
287+
return navigateAfterCreateOrganization(org);
288+
}}
289+
hideSlug={hideSlug}
290+
/>
291+
</Box>
292+
) : (
293+
<OrganizationListPageList onCreateOrganizationClick={() => setIsCreateOrganizationFlow(true)} />
294+
)}
295+
</Card.Content>
296+
<Card.Footer />
297+
</Card.Root>
298+
);
299+
};

0 commit comments

Comments
 (0)