Skip to content

Commit 9ffc559

Browse files
authored
feat(popular-locations): implement popular cities (#22)
* feat(popular-locations): implement popular cities * change property name
1 parent a85d9a4 commit 9ffc559

File tree

5 files changed

+69
-32
lines changed

5 files changed

+69
-32
lines changed

client/src/App.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,18 @@ main {
361361
.popular-cities {
362362
gap: 1rem;
363363
display: flex;
364+
min-height: 0;
364365
flex-direction: column;
365366
grid-area: popular-cities;
366367
/* TODO: [-] Extract to variable */
367368
background: rgba(255, 255, 255, 0.075);
368369
}
369370

370371
.popular-cities .cities {
371-
justify-content: space-around;
372372
flex: 1;
373+
gap: 2rem;
373374
display: flex;
375+
overflow: auto;
374376
flex-direction: column;
375377
}
376378

client/src/components/dashboard.tsx

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Map from './map';
2+
import PopularLocations from './popular-locations';
23
import { PiWind, PiEyeLight, PiSunLight, PiDropLight, PiWindLight, PiThermometerSimple } from 'react-icons/pi';
34

45
const Dashboard = () => {
@@ -47,36 +48,7 @@ const Dashboard = () => {
4748

4849
<Map coordinates={{ lat: 51.5, lng: 0.12 }} />
4950

50-
<div className='popular-cities'>
51-
<h1>Popular Cities</h1>
52-
53-
<div className='cities'>
54-
<div>
55-
<h2>London</h2>
56-
<span>25°C</span>
57-
</div>
58-
59-
<div>
60-
<h2>Paris</h2>
61-
<span>30°C</span>
62-
</div>
63-
64-
<div>
65-
<h2>New York</h2>
66-
<span>20°C</span>
67-
</div>
68-
69-
<div>
70-
<h2>Tokyo</h2>
71-
<span>35°C</span>
72-
</div>
73-
74-
<div>
75-
<h2>Rome</h2>
76-
<span>28°C</span>
77-
</div>
78-
</div>
79-
</div>
51+
<PopularLocations />
8052

8153
<div className='weekly-forecast'>
8254
<h1>Forecast</h1>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { trpc } from '@utils/trpc';
2+
3+
const PopularLocations = () => {
4+
const [data] = trpc.getPopularLocations.useSuspenseQuery();
5+
6+
return (
7+
<div className='popular-cities'>
8+
<h1>Popular Cities</h1>
9+
10+
<div className='cities'>
11+
{
12+
data.map((city) => (
13+
<div key={ city.name }>
14+
<h2>{ city.name }</h2>
15+
<span>{ city.temp_c }°C</span>
16+
</div>
17+
))
18+
}
19+
</div>
20+
</div>
21+
);
22+
};
23+
24+
export default PopularLocations;

server/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { z } from 'zod';
22
import cors from 'cors';
33
import { router, publicProcedure } from './utils/trpc';
4-
import { fetchSearch } from './utils/fetchers/fetchers';
54
import { createHTTPServer } from '@trpc/server/adapters/standalone';
5+
import { fetchSearch, fetchPopularLocations } from './utils/fetchers/fetchers';
66

77
export type AppRouter = typeof appRouter;
88

@@ -12,6 +12,12 @@ const appRouter = router({
1212
.query(async ({ input }) => {
1313
const results = await fetchSearch(input);
1414

15+
return results;
16+
}),
17+
getPopularLocations: publicProcedure
18+
.query(async () => {
19+
const results = await fetchPopularLocations();
20+
1521
return results;
1622
}),
1723
});

server/utils/fetchers/fetchers.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fetchExternalData from './fetch-external-data';
2+
import CurrentWeatherResponse from './types/current-forecast';
23
import LocationSearchResponse from './types/search-locations';
34

45
export const fetchSearch = async (query: string) => {
@@ -14,3 +15,35 @@ export const fetchSearch = async (query: string) => {
1415
name: `${ location.name }, ${ location.region }, ${ location.country }`,
1516
}));
1617
}
18+
19+
export const fetchCurrentWeather = async (query: string) => {
20+
const searchParams = new URLSearchParams({ q: query });
21+
22+
const data = await fetchExternalData<CurrentWeatherResponse>({
23+
endpoint: '/current.json',
24+
searchParams: searchParams,
25+
});
26+
27+
return data;
28+
}
29+
30+
export const fetchPopularLocations = async () => {
31+
const cities = [
32+
'London',
33+
'New York',
34+
'Paris',
35+
'Tokyo',
36+
'Beijing',
37+
'Madrid',
38+
'Rome',
39+
];
40+
41+
return Promise.all(cities.map(async (city) => {
42+
const data = await fetchCurrentWeather(city);
43+
44+
return {
45+
name: data.location.name,
46+
temp_c: data.current.temp_c,
47+
};
48+
}));
49+
};

0 commit comments

Comments
 (0)