-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
48bfaa0
refactor(ui): extract account contact rows
austincalvelage 9469be4
feat(ui): add and verify profile phone numbers
austincalvelage 498432a
feat(ui): support setting a primary phone number
austincalvelage d710d5b
feat(ui): confirm phone number removal
austincalvelage faa6579
refactor(ui): move phone behavior into the phone row
austincalvelage 2d2df93
fix(ui): remove forced focus during phone verification transition
austincalvelage 0f83150
refactor(ui): name phone removal component as a dialog
austincalvelage 83cabba
refactor(ui): name add phone component as a dialog
austincalvelage 9069371
test(ui): use compatible deferred promises in phone tests
austincalvelage 4bae1a8
refactor(ui): simplify phone dialog event handling and tests
austincalvelage 884f6a5
refactor(ui): rely on shared focus restoration for phone removal
austincalvelage 8e521b2
fix(ui): retain contact manage fallback when explicit actions do not …
austincalvelage 4749d0a
refactor(ui): remove generic manage action from contact menus
austincalvelage 21b9d15
Merge branch 'main' into austin/user-profile-add-phone
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
177 changes: 177 additions & 0 deletions
177
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,177 @@ | ||
| 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'); | ||
| } | ||
| 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'); | ||
| } | ||
| 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()); | ||
| expect(props.onSubmit).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| 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.