-
Notifications
You must be signed in to change notification settings - Fork 474
feat(ui): Add phone, delete phone dialogs and set primary action #9717
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
Open
austincalvelage
wants to merge
8
commits into
austin/user-profile-row-reorganization
Choose a base branch
from
austin/user-profile-add-phone
base: austin/user-profile-row-reorganization
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ef0bff4
refactor(ui): extract account contact rows
austincalvelage c81da93
feat(ui): add and verify profile phone numbers
austincalvelage eee317d
feat(ui): support setting a primary phone number
austincalvelage 36dd4e1
feat(ui): confirm phone number removal
austincalvelage 483ddf3
refactor(ui): move phone behavior into the phone row
austincalvelage beaa83b
fix(ui): remove forced focus during phone verification transition
austincalvelage 4a8e560
refactor(ui): name phone removal component as a dialog
austincalvelage 21f9639
refactor(ui): name add phone component as a dialog
austincalvelage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/swingset/src/stories/fixtures/user-profile-add-phone.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { UserProfileAccountSectionViewProps } from '@clerk/ui/mosaic/user-profile/user-profile-account-section/user-profile-account-section.view'; | ||
| import type { UserProfileAddPhoneDialogProps } from '@clerk/ui/mosaic/user-profile/user-profile-account-section/user-profile-add-phone.dialog'; | ||
|
|
||
| interface FixtureOptions { | ||
| failAt?: UserProfileAddPhoneDialogProps['step']; | ||
| onVerified?: (phoneNumber: string) => void; | ||
| } | ||
|
|
||
| export function createUserProfileAddPhoneFixture({ failAt, onVerified }: FixtureOptions = {}): Pick< | ||
| UserProfileAccountSectionViewProps, | ||
| 'onSendPhoneCode' | 'onVerifyPhoneCode' | ||
| > { | ||
| return { | ||
| onSendPhoneCode: async () => { | ||
| await new Promise(resolve => setTimeout(resolve, 700)); | ||
| if (failAt === 'phone') { | ||
| throw new Error('We couldn’t send a code. Try again.'); | ||
| } | ||
| }, | ||
| onVerifyPhoneCode: async (phoneNumber, code) => { | ||
| await new Promise(resolve => setTimeout(resolve, 700)); | ||
| if (failAt === 'verify' || code === '000000') { | ||
| throw new Error('That code is incorrect. Try again.'); | ||
| } | ||
| onVerified?.(phoneNumber); | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
packages/ui/src/mosaic/user-profile/__tests__/user-profile-add-phone.dialog.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { useState } from 'react'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { MosaicProvider } from '../../MosaicProvider'; | ||
| import type { UserProfileAddPhoneDialogProps } from '../user-profile-account-section/user-profile-add-phone.dialog'; | ||
| import { UserProfileAddPhoneDialog } from '../user-profile-account-section/user-profile-add-phone.dialog'; | ||
|
|
||
| function renderView(overrides: Partial<UserProfileAddPhoneDialogProps> = {}) { | ||
| const props: UserProfileAddPhoneDialogProps = { | ||
| open: true, | ||
| onOpenChange: vi.fn(), | ||
| step: 'phone', | ||
| phoneNumber: '+18018888181', | ||
| onPhoneNumberChange: vi.fn(), | ||
| code: '', | ||
| onCodeChange: vi.fn(), | ||
| onSubmit: vi.fn(), | ||
| onResend: vi.fn(), | ||
| ...overrides, | ||
| }; | ||
| return { | ||
| props, | ||
| ...render( | ||
| <MosaicProvider> | ||
| <UserProfileAddPhoneDialog {...props} /> | ||
| </MosaicProvider>, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| function VerificationExample({ onSubmit }: Pick<UserProfileAddPhoneDialogProps, 'onSubmit'>) { | ||
| const [code, setCode] = useState(''); | ||
|
|
||
| return ( | ||
| <MosaicProvider> | ||
| <UserProfileAddPhoneDialog | ||
| open | ||
| onOpenChange={() => undefined} | ||
| step='verify' | ||
| phoneNumber='+18018888181' | ||
| onPhoneNumberChange={() => undefined} | ||
| code={code} | ||
| onCodeChange={setCode} | ||
| onSubmit={onSubmit} | ||
| onResend={() => undefined} | ||
| /> | ||
| </MosaicProvider> | ||
| ); | ||
| } | ||
|
|
||
| describe('UserProfileAddPhoneDialog', () => { | ||
| it.each(['typing', 'pasting'] as const)('automatically submits a complete code after %s', async method => { | ||
| const user = userEvent.setup(); | ||
| const onSubmit = vi.fn(); | ||
| render(<VerificationExample onSubmit={onSubmit} />); | ||
| await waitFor(() => expect(screen.getByRole('textbox', { name: 'Verification code' })).toHaveFocus()); | ||
|
|
||
| if (method === 'typing') { | ||
| await user.keyboard('12345'); | ||
| expect(onSubmit).not.toHaveBeenCalled(); | ||
| await user.keyboard('6'); | ||
| } else { | ||
| await user.paste('123456'); | ||
| } | ||
|
|
||
| expect(onSubmit).toHaveBeenCalledExactlyOnceWith('123456'); | ||
| }); | ||
|
|
||
| it('focuses the phone field and submits through the form or Send code', async () => { | ||
| const user = userEvent.setup(); | ||
| const { props } = renderView(); | ||
|
|
||
| expect(screen.getByRole('dialog', { name: 'Add phone number' })).toBeInTheDocument(); | ||
| const phone = screen.getByRole('textbox', { name: 'Phone' }); | ||
| await waitFor(() => expect(phone).toHaveFocus()); | ||
| const phoneForm = phone.closest('form'); | ||
| if (!phoneForm) { | ||
| throw new Error('Phone form missing'); | ||
| } | ||
| expect(phoneForm).toHaveClass('cl-card-content'); | ||
| phoneForm.requestSubmit(); | ||
| expect(props.onSubmit).toHaveBeenCalledOnce(); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Send code' })); | ||
|
|
||
| expect(props.onSubmit).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('moves to verification inside the same dialog and submits the code', async () => { | ||
| const user = userEvent.setup(); | ||
| const { props, rerender } = renderView(); | ||
| const dialog = screen.getByRole('dialog'); | ||
|
|
||
| rerender( | ||
| <MosaicProvider> | ||
| <UserProfileAddPhoneDialog | ||
| {...props} | ||
| step='verify' | ||
| code='123456' | ||
| /> | ||
| </MosaicProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByRole('dialog', { name: 'Verify your phone number' })).toBe(dialog); | ||
| expect(screen.getByText('Enter the code sent to +1 (801) 888-8181')).toBeInTheDocument(); | ||
| expect(screen.queryByRole('textbox', { name: 'Phone' })).not.toBeInTheDocument(); | ||
| const firstSlot = screen.getByRole('textbox', { name: 'Verification code' }); | ||
| const verifyForm = firstSlot.closest('form'); | ||
| if (!verifyForm) { | ||
| throw new Error('Verification form missing'); | ||
| } | ||
| expect(verifyForm).toHaveClass('cl-card-content'); | ||
| verifyForm.requestSubmit(); | ||
| expect(props.onSubmit).toHaveBeenCalledOnce(); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Verify', exact: true })); | ||
|
|
||
| expect(props.onSubmit).toHaveBeenCalledTimes(2); | ||
| await user.click(screen.getByRole('button', { name: 'Cancel' })); | ||
| expect(props.onOpenChange).toHaveBeenCalledWith(false, expect.anything()); | ||
| }); | ||
|
|
||
| it('blocks submission and resend while verification is pending', async () => { | ||
| const user = userEvent.setup(); | ||
| const { props } = renderView({ step: 'verify', code: '123456', isPending: true }); | ||
|
|
||
| for (const slot of screen.getAllByRole('textbox')) { | ||
| expect(slot).toBeDisabled(); | ||
| } | ||
| const verify = screen.getByRole('button', { name: 'Verify', exact: true }); | ||
| expect(verify).toHaveAttribute('aria-busy', 'true'); | ||
| await user.click(verify); | ||
| await user.click(screen.getByRole('button', { name: 'Didn’t receive a code? Resend' })); | ||
|
|
||
| expect(props.onSubmit).not.toHaveBeenCalled(); | ||
| expect(props.onResend).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each(['phone', 'verify'] as const)('associates a %s error with its input', step => { | ||
| renderView({ step, errorMessage: 'Please try again.' }); | ||
|
|
||
| const field = screen.getByRole('textbox', { name: step === 'phone' ? 'Phone' : 'Verification code' }); | ||
| expect(field).toHaveAttribute('aria-invalid', 'true'); | ||
| const describedControl = step === 'verify' ? screen.getByRole('group', { name: 'Verification code' }) : field; | ||
| expect(describedControl).toHaveAccessibleDescription('Please try again.'); | ||
| }); | ||
|
|
||
| it('allows resending only after the countdown and the current request finish', async () => { | ||
| const user = userEvent.setup(); | ||
| const { props, rerender } = renderView({ step: 'verify', resendSeconds: 12 }); | ||
| await user.click(screen.getByRole('button', { name: 'Didn’t receive a code? Resend (12)' })); | ||
| expect(props.onResend).not.toHaveBeenCalled(); | ||
|
|
||
| rerender( | ||
| <MosaicProvider> | ||
| <UserProfileAddPhoneDialog | ||
| {...props} | ||
| resendSeconds={0} | ||
| /> | ||
| </MosaicProvider>, | ||
| ); | ||
| await user.click(screen.getByRole('button', { name: 'Didn’t receive a code? Resend' })); | ||
| expect(props.onResend).toHaveBeenCalledOnce(); | ||
|
|
||
| rerender( | ||
| <MosaicProvider> | ||
| <UserProfileAddPhoneDialog | ||
| {...props} | ||
| resendSeconds={0} | ||
| isResending | ||
| /> | ||
| </MosaicProvider>, | ||
| ); | ||
| expect(screen.getByRole('button', { name: 'Sending a new code…' })).toBeDisabled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.