Skip to content

Commit

Permalink
Building city search component
Browse files Browse the repository at this point in the history
  • Loading branch information
zntb committed Jan 31, 2024
1 parent 2e94ee2 commit 407dae5
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 60 deletions.
23 changes: 19 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"dotenv": "^16.4.1"
}
}
40 changes: 3 additions & 37 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +1,4 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
.container {
max-width: 1080px;
margin: 20px auto;
}
23 changes: 7 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import logo from './logo.svg';
import './App.css';
import Search from './components/search/Search';

function App() {
const handleOnSearchChange = (searchData) => {
console.log(searchData);
};

return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<div className="container">
<Search onSearchChange={handleOnSearchChange} />
</div>
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const rapidApiKey = process.env.REACT_APP_X_RAPIDAPI_KEY;

export const geoApiOptions = {
method: 'GET',
headers: {
'X-RapidAPI-Key': rapidApiKey,
'X-RapidAPI-Host': 'wft-geo-db.p.rapidapi.com',
},
};

export const GEO_API_URL = 'https://wft-geo-db.p.rapidapi.com/v1/geo';
43 changes: 43 additions & 0 deletions src/components/search/Search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useState } from 'react';
import { AsyncPaginate } from 'react-select-async-paginate';

import { geoApiOptions, GEO_API_URL } from '../../api';

const Search = ({ onSearchChange }) => {
const [search, setSearch] = useState(null);

const loadOptions = (inputValue) => {
return fetch(
`${GEO_API_URL}/cities?minPopulation=1000000&namePrefix=${inputValue}`,
geoApiOptions
)
.then((response) => response.json())
.then((response) => {
return {
options: response.data.map((city) => {
return {
value: `${city.latitude} ${city.longitude}`,
label: `${city.name} ${city.countryCode}`,
};
}),
};
})
.catch((err) => console.log(err));
};

const handleOnChange = (searchData) => {
setSearch(searchData);
onSearchChange(searchData);
};

return (
<AsyncPaginate
placeholder="Search for city"
debounceTimeout={600}
value={search}
onChange={handleOnChange}
loadOptions={loadOptions}
/>
);
};
export default Search;
5 changes: 2 additions & 3 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
font-family: 'Roboto', Arial !important;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background-color: #d5d4d4;
}

code {
Expand Down

0 comments on commit 407dae5

Please sign in to comment.