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

feat(clerk-js,types): Move to waitlist with email address in combined flow #4615

Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions packages/clerk-js/sandbox/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ function addCurrentRouteIndicator(currentRoute) {
await Clerk.load({
signInUrl: '/sign-in',
signUpUrl: '/sign-up',
waitlistUrl: '/waitlist',
});
renderCurrentRoute();
}
Expand Down
7 changes: 3 additions & 4 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,14 +1044,13 @@ export class Clerk implements ClerkInterface {
return this.buildUrlWithAuth(this.#options.afterSignOutUrl);
}

public buildWaitlistUrl(): string {
public buildWaitlistUrl(options?: { initialValues?: Record<string, string> }): string {
if (!this.environment || !this.environment.displayConfig) {
return '';
}

const waitlistUrl = this.#options['waitlistUrl'] || this.environment.displayConfig.waitlistUrl;

return buildURL({ base: waitlistUrl }, { stringify: true });
const initValues = new URLSearchParams(options?.initialValues || {});
return buildURL({ base: waitlistUrl, hashSearchParams: [initValues] }, { stringify: true });
}

public buildAfterMultiSessionSingleSignOutUrl(): string {
Expand Down
13 changes: 13 additions & 0 deletions packages/clerk-js/src/ui/components/SignIn/SignInStart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,19 @@ export function _SignInStart(): JSX.Element {
const sid = alreadySignedInError.meta!.sessionId!;
await clerk.setActive({ session: sid, redirectUrl: afterSignInUrl });
} else {
if (isCombinedFlow && userSettings.signUp.mode === SIGN_UP_MODES.WAITLIST) {
const attribute = getSignUpAttributeFromIdentifier(identifierField);
const waitlistUrl = clerk.buildWaitlistUrl(
attribute === 'emailAddress'
? {
initialValues: {
[attribute]: identifierField.value,
},
}
: {},
);
return navigate(waitlistUrl);
}
if (isCombinedFlow) {
const attribute = getSignUpAttributeFromIdentifier(identifierField);
clerk.client.signUp[attribute] = identifierField.value;
Expand Down
4 changes: 3 additions & 1 deletion packages/clerk-js/src/ui/components/Waitlist/Waitlist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ const _Waitlist = () => {
const ctx = useWaitlistContext();
const { signInUrl } = ctx;

const initialValues = ctx.initialValues || {};

const formState = {
emailAddress: useFormControl('emailAddress', '', {
emailAddress: useFormControl('emailAddress', initialValues.emailAddress || '', {
type: 'email',
label: localizationKeys('formFieldLabel__emailAddress'),
placeholder: localizationKeys('formFieldInputPlaceholder__emailAddress'),
Expand Down
14 changes: 13 additions & 1 deletion packages/clerk-js/src/ui/contexts/components/Waitlist.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { createContext, useContext } from 'react';
import { createContext, useContext, useMemo } from 'react';

import { buildURL } from '../../../utils';
import { useEnvironment, useOptions } from '../../contexts';
import { useRouter } from '../../router';
import type { WaitlistCtx } from '../../types';
import { getInitialValuesFromQueryParams } from '../utils';

const WAITLIST_INITIAL_VALUE_KEYS = ['email_address'];

export type WaitlistContextType = WaitlistCtx & {
signInUrl: string;
afterJoinWaitlistUrl?: string;
initialValues: any;
};

export const WaitlistContext = createContext<WaitlistCtx | null>(null);
Expand All @@ -15,6 +20,12 @@ export const useWaitlistContext = (): WaitlistContextType => {
const context = useContext(WaitlistContext);
const { displayConfig } = useEnvironment();
const options = useOptions();
const { queryString } = useRouter();

const initialValuesFromQueryParams = useMemo(
() => getInitialValuesFromQueryParams(queryString, WAITLIST_INITIAL_VALUE_KEYS),
[],
);

if (!context || context.componentName !== 'Waitlist') {
throw new Error('Clerk: useWaitlistContext called outside Waitlist.');
Expand All @@ -29,5 +40,6 @@ export const useWaitlistContext = (): WaitlistContextType => {
...ctx,
componentName,
signInUrl,
initialValues: { ...initialValuesFromQueryParams },
};
};
2 changes: 1 addition & 1 deletion packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ export interface Clerk {
/**
* Returns the configured url where <Waitlist/> is mounted or a custom waitlist page is rendered.
*/
buildWaitlistUrl(): string;
buildWaitlistUrl(opts?: { initialValues?: Record<string, string> }): string;

/**
*
Expand Down
Loading