Repository navigation
Expand file tree
/
Copy pathapi.ts
More file actions
96 lines (83 loc) · 3.14 KB
/
Copy pathapi.ts
File metadata and controls
96 lines (83 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// A pretend backend: every call resolves after a delay so the UI has real
// latency to render around. Nothing here knows about Solid.
export interface Region {
id: string;
name: string;
}
export interface City {
id: string;
name: string;
country: string;
population: number;
}
export interface Weather {
temp: number;
sky: string;
icon: string;
}
export const regions: Region[] = [
{ id: 'europe', name: 'Europe' },
{ id: 'asia', name: 'Asia' },
{ id: 'americas', name: 'Americas' },
{ id: 'oceania', name: 'Oceania' },
];
const cities: Record<string, City[]> = {
europe: [
{ id: 'lisbon', name: 'Lisbon', country: 'Portugal', population: 545000 },
{ id: 'paris', name: 'Paris', country: 'France', population: 2100000 },
{ id: 'oslo', name: 'Oslo', country: 'Norway', population: 709000 },
{ id: 'rome', name: 'Rome', country: 'Italy', population: 2750000 },
],
asia: [
{ id: 'tokyo', name: 'Tokyo', country: 'Japan', population: 14000000 },
{ id: 'seoul', name: 'Seoul', country: 'South Korea', population: 9400000 },
{ id: 'hanoi', name: 'Hanoi', country: 'Vietnam', population: 8400000 },
],
americas: [
{ id: 'portland', name: 'Portland', country: 'USA', population: 630000 },
{ id: 'mexico-city', name: 'Mexico City', country: 'Mexico', population: 9200000 },
{ id: 'lima', name: 'Lima', country: 'Peru', population: 10100000 },
],
oceania: [
{ id: 'sydney', name: 'Sydney', country: 'Australia', population: 5300000 },
{ id: 'auckland', name: 'Auckland', country: 'New Zealand', population: 1700000 },
],
};
const weather: Record<string, Weather> = {
lisbon: { temp: 22, sky: 'Sunny', icon: '☀️' },
paris: { temp: 18, sky: 'Partly cloudy', icon: '⛅️' },
oslo: { temp: 9, sky: 'Light rain', icon: '🌧️' },
rome: { temp: 25, sky: 'Clear', icon: '☀️' },
tokyo: { temp: 21, sky: 'Cloudy', icon: '☁️' },
seoul: { temp: 17, sky: 'Breezy', icon: '🌬️' },
hanoi: { temp: 31, sky: 'Humid', icon: '🌤️' },
portland: { temp: 14, sky: 'Drizzle', icon: '🌦️' },
'mexico-city': { temp: 23, sky: 'Sunny', icon: '☀️' },
lima: { temp: 19, sky: 'Overcast', icon: '☁️' },
sydney: { temp: 24, sky: 'Clear', icon: '☀️' },
auckland: { temp: 16, sky: 'Showers', icon: '🌦️' },
};
const favorites = new Set<string>(['paris']);
const wait = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
// Oceania fails on its first request so the error boundary has something to catch.
let oceaniaRequests = 0;
export async function fetchCities(regionId: string): Promise<City[]> {
await wait(700);
if (regionId === 'oceania' && oceaniaRequests++ === 0) {
throw new Error('Oceania timed out.');
}
return cities[regionId];
}
export async function fetchWeather(cityId: string): Promise<Weather> {
await wait(450);
return weather[cityId];
}
export async function fetchFavorites(): Promise<string[]> {
await wait(300);
return [...favorites];
}
export async function saveFavorite(cityId: string, isFavorite: boolean): Promise<void> {
await wait(900);
if (isFavorite) favorites.add(cityId);
else favorites.delete(cityId);
}