Skip to content

Commit 407dae5

Browse files
committed
Building city search component
1 parent 2e94ee2 commit 407dae5

File tree

7 files changed

+88
-60
lines changed

7 files changed

+88
-60
lines changed

package-lock.json

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@
3636
"last 1 firefox version",
3737
"last 1 safari version"
3838
]
39+
},
40+
"devDependencies": {
41+
"dotenv": "^16.4.1"
3942
}
4043
}

src/App.css

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,4 @@
1-
.App {
2-
text-align: center;
3-
}
4-
5-
.App-logo {
6-
height: 40vmin;
7-
pointer-events: none;
8-
}
9-
10-
@media (prefers-reduced-motion: no-preference) {
11-
.App-logo {
12-
animation: App-logo-spin infinite 20s linear;
13-
}
14-
}
15-
16-
.App-header {
17-
background-color: #282c34;
18-
min-height: 100vh;
19-
display: flex;
20-
flex-direction: column;
21-
align-items: center;
22-
justify-content: center;
23-
font-size: calc(10px + 2vmin);
24-
color: white;
25-
}
26-
27-
.App-link {
28-
color: #61dafb;
29-
}
30-
31-
@keyframes App-logo-spin {
32-
from {
33-
transform: rotate(0deg);
34-
}
35-
to {
36-
transform: rotate(360deg);
37-
}
1+
.container {
2+
max-width: 1080px;
3+
margin: 20px auto;
384
}

src/App.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
import logo from './logo.svg';
21
import './App.css';
2+
import Search from './components/search/Search';
33

44
function App() {
5+
const handleOnSearchChange = (searchData) => {
6+
console.log(searchData);
7+
};
8+
59
return (
6-
<div className="App">
7-
<header className="App-header">
8-
<img src={logo} className="App-logo" alt="logo" />
9-
<p>
10-
Edit <code>src/App.js</code> and save to reload.
11-
</p>
12-
<a
13-
className="App-link"
14-
href="https://reactjs.org"
15-
target="_blank"
16-
rel="noopener noreferrer"
17-
>
18-
Learn React
19-
</a>
20-
</header>
10+
<div className="container">
11+
<Search onSearchChange={handleOnSearchChange} />
2112
</div>
2213
);
2314
}

src/api.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const rapidApiKey = process.env.REACT_APP_X_RAPIDAPI_KEY;
2+
3+
export const geoApiOptions = {
4+
method: 'GET',
5+
headers: {
6+
'X-RapidAPI-Key': rapidApiKey,
7+
'X-RapidAPI-Host': 'wft-geo-db.p.rapidapi.com',
8+
},
9+
};
10+
11+
export const GEO_API_URL = 'https://wft-geo-db.p.rapidapi.com/v1/geo';

src/components/search/Search.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useState } from 'react';
2+
import { AsyncPaginate } from 'react-select-async-paginate';
3+
4+
import { geoApiOptions, GEO_API_URL } from '../../api';
5+
6+
const Search = ({ onSearchChange }) => {
7+
const [search, setSearch] = useState(null);
8+
9+
const loadOptions = (inputValue) => {
10+
return fetch(
11+
`${GEO_API_URL}/cities?minPopulation=1000000&namePrefix=${inputValue}`,
12+
geoApiOptions
13+
)
14+
.then((response) => response.json())
15+
.then((response) => {
16+
return {
17+
options: response.data.map((city) => {
18+
return {
19+
value: `${city.latitude} ${city.longitude}`,
20+
label: `${city.name} ${city.countryCode}`,
21+
};
22+
}),
23+
};
24+
})
25+
.catch((err) => console.log(err));
26+
};
27+
28+
const handleOnChange = (searchData) => {
29+
setSearch(searchData);
30+
onSearchChange(searchData);
31+
};
32+
33+
return (
34+
<AsyncPaginate
35+
placeholder="Search for city"
36+
debounceTimeout={600}
37+
value={search}
38+
onChange={handleOnChange}
39+
loadOptions={loadOptions}
40+
/>
41+
);
42+
};
43+
export default Search;

src/index.css

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
body {
22
margin: 0;
3-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4-
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5-
sans-serif;
3+
font-family: 'Roboto', Arial !important;
64
-webkit-font-smoothing: antialiased;
75
-moz-osx-font-smoothing: grayscale;
6+
background-color: #d5d4d4;
87
}
98

109
code {

0 commit comments

Comments
 (0)