|
1 | | -import { Flow, Text } from '@/customizables'; |
| 1 | +import { useReverification, useUser } from '@clerk/shared/react'; |
| 2 | +import React from 'react'; |
| 3 | + |
| 4 | +import { Col, Flow, Heading, Icon, Input, localizationKeys, Text, useLocalizations } from '@/customizables'; |
| 5 | +import { useFieldOTP } from '@/elements/CodeControl'; |
| 6 | +import { useCardState } from '@/elements/contexts'; |
| 7 | +import { Form } from '@/elements/Form'; |
| 8 | +import { handleError } from '@/utils/errorHandler'; |
2 | 9 |
|
3 | 10 | import { useConfigureSSOWizard, useRegisterContinueAction } from '../wizard'; |
4 | 11 | import { StepLayout } from './StepLayout'; |
| 12 | +import { DuotoneAtSymbol } from '@/icons'; |
5 | 13 |
|
6 | 14 | export const VerifyDomainStep = (): JSX.Element => { |
7 | 15 | const { goNext } = useConfigureSSOWizard(); |
| 16 | + const card = useCardState(); |
| 17 | + const { t } = useLocalizations(); |
| 18 | + const { user } = useUser(); |
| 19 | + |
| 20 | + const primaryEmailAddress = user?.primaryEmailAddress; |
| 21 | + const isVerified = primaryEmailAddress?.verification.status === 'verified'; |
| 22 | + |
| 23 | + const prepareEmailVerification = useReverification(() => |
| 24 | + primaryEmailAddress?.prepareVerification({ strategy: 'email_code' }), |
| 25 | + ); |
| 26 | + const attemptEmailVerification = useReverification((code: string) => |
| 27 | + primaryEmailAddress?.attemptVerification({ code }), |
| 28 | + ); |
| 29 | + |
| 30 | + const prepare = React.useCallback( |
| 31 | + () => prepareEmailVerification()?.catch(err => handleError(err, [], card.setError)), |
| 32 | + [prepareEmailVerification, card], |
| 33 | + ); |
8 | 34 |
|
9 | | - useRegisterContinueAction({ |
10 | | - handler: () => goNext(), |
11 | | - // TODO: Implement verification |
12 | | - isDisabled: true, |
| 35 | + const codeSubmittedRef = React.useRef(false); |
| 36 | + |
| 37 | + const otp = useFieldOTP({ |
| 38 | + onCodeEntryFinished: (code, resolve, reject) => { |
| 39 | + codeSubmittedRef.current = true; |
| 40 | + attemptEmailVerification(code) |
| 41 | + .then(() => resolve()) |
| 42 | + .catch(reject); |
| 43 | + }, |
| 44 | + onResendCodeClicked: () => { |
| 45 | + void prepare(); |
| 46 | + }, |
| 47 | + onResolve: () => { |
| 48 | + void goNext(); |
| 49 | + }, |
13 | 50 | }); |
14 | 51 |
|
| 52 | + const { values, length } = otp.otpControl.otpInputProps; |
| 53 | + const isCodeComplete = values.filter(Boolean).length === length; |
| 54 | + const showVerifiedView = isVerified && !codeSubmittedRef.current; |
| 55 | + |
| 56 | + useRegisterContinueAction( |
| 57 | + showVerifiedView |
| 58 | + ? { |
| 59 | + handler: () => { |
| 60 | + void goNext(); |
| 61 | + }, |
| 62 | + } |
| 63 | + : { |
| 64 | + handler: otp.onFakeContinue, |
| 65 | + isDisabled: !isCodeComplete, |
| 66 | + isLoading: otp.isLoading, |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + // Send the first code on mount (only when there's something to verify), |
| 71 | + // and clear any stale card error that could be lingering from a previous step |
| 72 | + React.useEffect(() => { |
| 73 | + if (!isVerified) { |
| 74 | + void prepare(); |
| 75 | + } |
| 76 | + card.setError(undefined); |
| 77 | + return () => card.setError(undefined); |
| 78 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 79 | + }, []); |
| 80 | + |
15 | 81 | return ( |
16 | 82 | <Flow.Part part='verifyDomain'> |
17 | 83 | <StepLayout |
18 | | - title='Verify your domain' |
19 | | - subtitle='Verify the domain you want to enable the enterprise connection on.' |
| 84 | + title={localizationKeys('configureSSO.verifyEmailDomainStep.title')} |
| 85 | + subtitle={localizationKeys('configureSSO.verifyEmailDomainStep.subtitle')} |
20 | 86 | > |
21 | | - <Text>UI goes here</Text> |
| 87 | + <Col |
| 88 | + align='center' |
| 89 | + sx={t => ({ |
| 90 | + flex: 1, |
| 91 | + justifyContent: 'center', |
| 92 | + gap: t.space.$2, |
| 93 | + paddingBlock: t.space.$8, |
| 94 | + })} |
| 95 | + > |
| 96 | + {showVerifiedView && primaryEmailAddress ? ( |
| 97 | + <> |
| 98 | + <Icon |
| 99 | + icon={DuotoneAtSymbol} |
| 100 | + sx={t => ({ |
| 101 | + width: t.sizes.$8, |
| 102 | + height: t.sizes.$8, |
| 103 | + color: t.colors.$neutralAlpha600, |
| 104 | + })} |
| 105 | + /> |
| 106 | + <Col sx={t => ({ gap: t.space.$1, textAlign: 'center', maxWidth: t.sizes.$66 })}> |
| 107 | + <Heading |
| 108 | + textVariant='h1' |
| 109 | + sx={t => ({ color: t.colors.$colorForeground, fontSize: t.fontSizes.$sm })} |
| 110 | + localizationKey={localizationKeys('configureSSO.verifyEmailDomainStep.emailCode.verified.title')} |
| 111 | + /> |
| 112 | + <Text |
| 113 | + as='p' |
| 114 | + variant='body' |
| 115 | + sx={t => ({ color: t.colors.$colorMutedForeground })} |
| 116 | + localizationKey={localizationKeys('configureSSO.verifyEmailDomainStep.emailCode.verified.subtitle')} |
| 117 | + /> |
| 118 | + </Col> |
| 119 | + <Input |
| 120 | + type='email' |
| 121 | + value={primaryEmailAddress.emailAddress} |
| 122 | + readOnly |
| 123 | + aria-label={t(localizationKeys('configureSSO.verifyEmailDomainStep.emailCode.verified.inputLabel'))} |
| 124 | + sx={t => ({ width: '100%', maxWidth: t.sizes.$66, backgroundColor: t.colors.$neutralAlpha50 })} |
| 125 | + /> |
| 126 | + </> |
| 127 | + ) : ( |
| 128 | + <> |
| 129 | + <Col sx={t => ({ gap: t.space.$1, textAlign: 'center' })}> |
| 130 | + <Heading |
| 131 | + textVariant='h1' |
| 132 | + sx={t => ({ color: t.colors.$colorForeground, fontSize: t.fontSizes.$sm })} |
| 133 | + localizationKey={localizationKeys('configureSSO.verifyEmailDomainStep.emailCode.formTitle')} |
| 134 | + /> |
| 135 | + <Text |
| 136 | + as='p' |
| 137 | + variant='body' |
| 138 | + sx={t => ({ color: t.colors.$colorMutedForeground })} |
| 139 | + localizationKey={localizationKeys('configureSSO.verifyEmailDomainStep.emailCode.formSubtitle', { |
| 140 | + identifier: primaryEmailAddress?.emailAddress ?? '', |
| 141 | + })} |
| 142 | + /> |
| 143 | + </Col> |
| 144 | + |
| 145 | + <Form.OTPInput |
| 146 | + {...otp} |
| 147 | + resendButton={localizationKeys('configureSSO.verifyEmailDomainStep.emailCode.resendButton')} |
| 148 | + /> |
| 149 | + </> |
| 150 | + )} |
| 151 | + </Col> |
22 | 152 | </StepLayout> |
23 | 153 | </Flow.Part> |
24 | 154 | ); |
|
0 commit comments