Repository navigation
RC-T48 Login Fixes #80
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { act, fireEvent, render, screen } from '@testing-library/react-native'; | ||
| import React from 'react'; | ||
|
|
||
| import { useServerUrlStore } from '@/stores/app/server-url-store'; | ||
|
|
||
| import { LoginServerBadge } from '../login-server-badge'; | ||
|
|
||
| jest.mock('react-i18next', () => ({ | ||
| useTranslation: () => ({ | ||
| t: (key: string, options?: Record<string, string>) => | ||
| ({ | ||
| 'login.server.label': 'Server:', | ||
| 'login.server.us_west': 'US West', | ||
| 'login.server.eu_central': 'EU Central', | ||
| })[key] ?? (key === 'login.server.custom_host' ? `Custom (${options?.host})` : key), | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock('nativewind', () => ({ useColorScheme: () => ({ colorScheme: 'light' }) })); | ||
| jest.mock('@/lib/storage/app', () => ({ | ||
| getBaseApiUrl: jest.fn(() => 'https://api.resgrid.com/api/v4'), | ||
| setBaseApiUrl: jest.fn(), | ||
| })); | ||
|
|
||
| describe('LoginServerBadge', () => { | ||
| beforeEach(() => { | ||
| useServerUrlStore.setState({ url: '' }); | ||
| }); | ||
|
|
||
| it('names the stored server before anything changes it', () => { | ||
| render(<LoginServerBadge />); | ||
| expect(screen.getByText('US West')).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('follows a server change', async () => { | ||
| render(<LoginServerBadge />); | ||
| await act(async () => { | ||
| await useServerUrlStore.getState().setUrl('https://api-eu-central.resgrid.com/api/v4'); | ||
| }); | ||
| expect(screen.getByText('EU Central')).toBeTruthy(); | ||
|
|
||
| await act(async () => { | ||
| await useServerUrlStore.getState().setUrl('https://rg.example.org/api/v4'); | ||
| }); | ||
| expect(screen.getByText('Custom (rg.example.org)')).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('opens the server selector when pressed', () => { | ||
| const onPress = jest.fn(); | ||
| render(<LoginServerBadge onPress={onPress} />); | ||
| fireEvent.press(screen.getByTestId('login-server-badge')); | ||
| expect(onPress).toHaveBeenCalled(); | ||
| }); | ||
| }); |
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,37 @@ | ||
| import { Server } from 'lucide-react-native'; | ||
| import { useColorScheme } from 'nativewind'; | ||
| import React from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { Pressable } from 'react-native'; | ||
|
|
||
| import { Text } from '@/components/ui/text'; | ||
| import { useLoginServer } from '@/hooks/use-login-server'; | ||
|
|
||
| interface LoginServerBadgeProps { | ||
| /** Opens the server selector; without it the badge is a plain label. */ | ||
| onPress?: () => void; | ||
| } | ||
|
|
||
| /** Names the server the login form signs in to (US West, EU Central or Custom), so a wrong server is spotted before signing in. */ | ||
| export const LoginServerBadge = ({ onPress }: LoginServerBadgeProps) => { | ||
| const { t } = useTranslation(); | ||
| const { colorScheme } = useColorScheme(); | ||
| const { name } = useLoginServer(); | ||
|
|
||
| return ( | ||
| <Pressable | ||
| onPress={onPress} | ||
| disabled={!onPress} | ||
| accessibilityRole={onPress ? 'button' : 'text'} | ||
| accessibilityLabel={t('login.server.signing_in_to', { server: name })} | ||
| accessibilityHint={onPress ? t('login.server.change_hint') : undefined} | ||
| testID="login-server-badge" | ||
| className="flex-row items-center self-center rounded-full border border-gray-300 px-3 py-1.5 dark:border-gray-700" | ||
| > | ||
| <Server size={14} color={colorScheme === 'dark' ? '#9ca3af' : '#6b7280'} /> | ||
| <Text className="ml-1.5 text-sm text-gray-600 dark:text-gray-300"> | ||
| {t('login.server.label')} <Text className="text-sm font-semibold text-gray-900 dark:text-white">{name}</Text> | ||
| </Text> | ||
| </Pressable> | ||
| ); | ||
| }; | ||
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,22 @@ | ||
| import { useMemo } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import { describeLoginServer, type LoginServer } from '@/lib/login-server'; | ||
| import { getBaseApiUrl } from '@/lib/storage/app'; | ||
| import { useServerUrlStore } from '@/stores/app/server-url-store'; | ||
|
|
||
| /** | ||
| * The server this device signs in to, and its display name ("US West", "EU Central" or "Custom (host)"). Follows a | ||
| * server change made from the login screen, since saving one updates the server URL store. | ||
| */ | ||
| export const useLoginServer = (): LoginServer & { name: string } => { | ||
| const { t } = useTranslation(); | ||
| const storeUrl = useServerUrlStore((state) => state.url); | ||
| const apiUrl = storeUrl || getBaseApiUrl(); | ||
|
|
||
| return useMemo(() => { | ||
| const server = describeLoginServer(apiUrl); | ||
| const name = server.kind === 'custom' ? t('login.server.custom_host', { host: server.host }) : t(`login.server.${server.kind}`); | ||
| return { ...server, name }; | ||
| }, [apiUrl, t]); | ||
| }; |
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,15 @@ | ||
| import { describeLoginServer } from '../login-server'; | ||
|
|
||
| describe('describeLoginServer', () => { | ||
| it('names the Resgrid hosted servers with or without the API suffix', () => { | ||
| expect(describeLoginServer('https://api.resgrid.com/api/v4')).toEqual({ kind: 'us_west', host: 'api.resgrid.com' }); | ||
| expect(describeLoginServer('https://API.resgrid.com/')).toEqual({ kind: 'us_west', host: 'api.resgrid.com' }); | ||
| expect(describeLoginServer('https://api-eu-central.resgrid.com/api/v4')).toEqual({ kind: 'eu_central', host: 'api-eu-central.resgrid.com' }); | ||
| }); | ||
|
|
||
| it('labels any other server custom, with its host', () => { | ||
| expect(describeLoginServer('https://qaapi.resgrid.dev/api/v4')).toEqual({ kind: 'custom', host: 'qaapi.resgrid.dev' }); | ||
| expect(describeLoginServer('http://localhost:5000/api/v4')).toEqual({ kind: 'custom', host: 'localhost:5000' }); | ||
| expect(describeLoginServer('https://resgrid.example.org/rg/api/v4')).toEqual({ kind: 'custom', host: 'resgrid.example.org' }); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constrain the badge on narrow screens.
If a custom hostname is long, the badge’s horizontal layout and intrinsic width can extend beyond a phone screen. Limit the badge to its parent width and allow the text to shrink or wrap. As per coding guidelines, “All components must be mobile-friendly and responsive” across iOS and Android.
Source: Coding guidelines