Skip to content

Commit d8d5ef0

Browse files
committed
04/03: fix api in problem, add waitForLoadState in solution
1 parent 42b1b0b commit d8d5ef0

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

exercises/03.guides/04.problem.api-mocking/app/routes/users+/$username_+/__note-editor.tsx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ export const NoteEditorSchema = z.object({
5757
location: z.string().optional(),
5858
})
5959

60-
export type GoogleFindPlaceApiResponse = {
61-
candidates: Array<{
62-
place_id: string
63-
formatted_address: string
64-
}>
65-
}
60+
export type NominatimSearchResponse = Array<{
61+
place_id: number
62+
addresstype: 'country' | 'city'
63+
display_name: string
64+
}>
6665

6766
export function NoteEditor({
6867
note,
@@ -89,22 +88,23 @@ export function NoteEditor({
8988
const imageList = fields.images.getFieldList()
9089

9190
const { data: locationSuggestions } = useQuery<
92-
GoogleFindPlaceApiResponse | undefined
91+
NominatimSearchResponse | undefined
9392
>({
94-
queryKey: ['location-suggestions'],
93+
queryKey: ['location-suggestions', fields.location.value],
9594
queryFn: async ({ signal }) => {
9695
await new Promise((resolve) => setTimeout(resolve, 300))
9796

9897
if (!signal.aborted && fields.location.value) {
99-
const url = new URL(
100-
'https://maps.googleapis.com/maps/api/place/findplacefromtext/json',
101-
)
102-
url.searchParams.set('key', 'FOO')
103-
url.searchParams.set('input', fields.location.value)
104-
url.searchParams.set('inputtype', 'textquery')
105-
url.searchParams.set('fields', 'place_id,formatted_address')
98+
const url = new URL('https://nominatim.openstreetmap.org/search')
99+
url.searchParams.set('q', fields.location.value)
100+
url.searchParams.set('format', 'jsonv2')
106101
const response = await fetch(url)
107-
return response.json() as Promise<GoogleFindPlaceApiResponse>
102+
const locations = (await response.json()) as NominatimSearchResponse
103+
return locations.filter(
104+
(location) =>
105+
location['addresstype'] === 'city' ||
106+
location['addresstype'] === 'country',
107+
)
108108
}
109109
},
110110
enabled: !!fields.location.value,
@@ -144,9 +144,10 @@ export function NoteEditor({
144144
/>
145145
<ComboboxField
146146
options={
147-
locationSuggestions?.candidates.map((candidate) => ({
148-
label: candidate.formatted_address,
149-
value: candidate.formatted_address,
147+
locationSuggestions?.map((location) => ({
148+
id: location.place_id,
149+
label: location.display_name,
150+
value: location.display_name,
150151
})) || []
151152
}
152153
labelProps={{ children: 'Location' }}

exercises/03.guides/04.problem.api-mocking/tests/e2e/notes-create.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test('displays location suggestions when creating a new note', async ({
5353
// 🐨 Assert that the full location name has been filled into the `locationInput`.
5454
// 💰 expect(locator).toHaveValue(value)
5555

56-
// 🐨 CLick on the "Submit" button to create a new note.
56+
// 🐨 Click on the "Submit" button to create a new note.
5757

5858
// 🐨 Finally, assert that a heading with the new note's title is visible on the page.
5959
// 💰 expect(locator)toBeVisible()

exercises/03.guides/04.solution.api-mocking/app/routes/users+/$username_+/__note-editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ export const NoteEditorSchema = z.object({
5858
})
5959

6060
export type NominatimSearchResponse = Array<{
61-
addresstype: 'country' | 'city'
6261
place_id: number
62+
addresstype: 'country' | 'city'
6363
display_name: string
6464
}>
6565

exercises/03.guides/04.solution.api-mocking/playwright.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export default defineConfig({
1313
forbidOnly: !!process.env.CI,
1414
retries: process.env.CI ? 2 : 0,
1515
workers: process.env.CI ? 1 : undefined,
16-
reporter: 'html',
16+
reporter: 'list',
1717
use: {
1818
baseURL: `http://localhost:${PORT}/`,
19-
trace: 'on-first-retry',
19+
screenshot: 'on-first-failure',
2020
contextOptions: {
2121
reducedMotion: 'reduce',
2222
},

exercises/03.guides/04.solution.api-mocking/tests/e2e/notes-create.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ test('displays location suggestions when creating a new note', async ({
3030

3131
const { user } = await authenticate({ as: 'user' })
3232
await navigate('/users/:username/notes/new', { username: user.username })
33+
await page.waitForLoadState('networkidle')
3334

3435
await page.getByLabel('Title').fill('My note')
3536
await page.getByLabel('Content').fill('Hello world')
3637

3738
const locationInput = page.getByLabel('Location')
38-
await locationInput.waitFor({ state: 'visible' })
39-
4039
await locationInput.fill('San')
4140
await expect(page.getByRole('option')).toHaveText([
4241
'San Francisco',

0 commit comments

Comments
 (0)