-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elements): Add SafeIdentifier & Salutation [SDK-1501] (#3004)
* feat(elements): Add SafeIdentifier & Salutation [SDK-1501] * chore(elements): Add requested changes * Update verification code copy Co-authored-by: Bryce Kalow <[email protected]> --------- Co-authored-by: Bryce Kalow <[email protected]>
- Loading branch information
Showing
9 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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
16 changes: 16 additions & 0 deletions
16
packages/elements/src/internals/machines/sign-in/selectors/sign-in.selectors.ts
This file contains 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,16 @@ | ||
import type { SignInRouterSnapshot } from '~/internals/machines/sign-in/types'; | ||
import { formatSalutation } from '~/internals/machines/utils/formatters'; | ||
|
||
export function SignInSafeIdentifierSelector(s: SignInRouterSnapshot): string { | ||
return s.context.clerk?.client.signIn.identifier || ''; | ||
} | ||
|
||
export function SignInSalutationSelector(s: SignInRouterSnapshot): string { | ||
const signIn = s.context.clerk?.client.signIn; | ||
|
||
return formatSalutation({ | ||
firstName: signIn?.userData?.firstName, | ||
identifier: signIn?.identifier, | ||
lastName: signIn?.userData?.lastName, | ||
}); | ||
} |
This file contains 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
47 changes: 47 additions & 0 deletions
47
packages/elements/src/internals/machines/utils/__tests__/formatters.test.ts
This file contains 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,47 @@ | ||
import { formatName, formatSalutation } from '../formatters'; | ||
|
||
describe('formatName', () => { | ||
test('returns undefined when no arguments are provided', () => { | ||
expect(formatName()).toBeUndefined(); | ||
}); | ||
|
||
test('returns the titleized version of the single argument', () => { | ||
expect(formatName('john')).toBe('John'); | ||
}); | ||
|
||
test('returns the titleized version of multiple arguments joined by space', () => { | ||
expect(formatName('john', 'doe')).toBe('John Doe'); | ||
}); | ||
|
||
test('ignores undefined arguments and returns the titleized version of the rest', () => { | ||
expect(formatName(undefined, 'john', undefined, 'doe')).toBe('John Doe'); | ||
}); | ||
}); | ||
|
||
describe('formatSalutation', () => { | ||
test('returns the formatted salutation based on firstName', () => { | ||
expect(formatSalutation({ firstName: 'John', lastName: undefined, identifier: undefined })).toBe('John'); | ||
}); | ||
|
||
test('returns the formatted salutation based on lastName', () => { | ||
expect(formatSalutation({ firstName: undefined, lastName: 'Doe', identifier: undefined })).toBe('Doe'); | ||
}); | ||
|
||
test('returns the formatted salutation based on identifier', () => { | ||
expect(formatSalutation({ firstName: undefined, lastName: undefined, identifier: '[email protected]' })).toBe( | ||
'[email protected]', | ||
); | ||
}); | ||
|
||
test('returns an empty string when no arguments are provided', () => { | ||
expect(formatSalutation({ firstName: undefined, lastName: undefined, identifier: undefined })).toBe(''); | ||
}); | ||
|
||
test('returns the formatted salutation based on firstName and lastName', () => { | ||
expect(formatSalutation({ firstName: 'John', lastName: 'Doe', identifier: undefined })).toBe('John'); | ||
}); | ||
|
||
test('returns the formatted salutation based on firstName, lastName, and identifier', () => { | ||
expect(formatSalutation({ firstName: 'John', lastName: 'Doe', identifier: '[email protected]' })).toBe('John'); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/elements/src/internals/machines/utils/formatters.ts
This file contains 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,26 @@ | ||
import { titleize } from '@clerk/shared'; | ||
|
||
// TODO: ideally the derivation of these values lives in FAPI and comes back directly from the API | ||
|
||
export function formatName(...args: (string | undefined)[]): string | undefined { | ||
switch (args.length) { | ||
case 0: | ||
return undefined; | ||
case 1: | ||
return titleize(args[0]); | ||
default: | ||
return args.filter(Boolean).map(titleize).join(' '); | ||
} | ||
} | ||
|
||
export function formatSalutation({ | ||
firstName, | ||
lastName, | ||
identifier, | ||
}: { | ||
firstName: string | undefined; | ||
lastName: string | undefined; | ||
identifier: string | undefined | null; | ||
}): string { | ||
return (firstName && formatName(firstName)) || (lastName && formatName(lastName)) || identifier || ''; | ||
} |
This file contains 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,14 @@ | ||
import { | ||
SignInSafeIdentifierSelector, | ||
SignInSalutationSelector, | ||
} from '~/internals/machines/sign-in/selectors/sign-in.selectors'; | ||
|
||
import { SignInRouterCtx } from './context'; | ||
|
||
export function SignInSafeIdentifier(): string { | ||
return SignInRouterCtx.useSelector(SignInSafeIdentifierSelector); | ||
} | ||
|
||
export function SignInSalutation(): string { | ||
return SignInRouterCtx.useSelector(SignInSalutationSelector); | ||
} |
This file contains 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