From 407dae50cca3c08ba0c7db2613185447f2efb482 Mon Sep 17 00:00:00 2001 From: zntb Date: Wed, 31 Jan 2024 15:47:40 +0200 Subject: [PATCH] Building city search component --- package-lock.json | 23 +++++++++++++++--- package.json | 3 +++ src/App.css | 40 +++--------------------------- src/App.js | 23 ++++++------------ src/api.js | 11 +++++++++ src/components/search/Search.js | 43 +++++++++++++++++++++++++++++++++ src/index.css | 5 ++-- 7 files changed, 88 insertions(+), 60 deletions(-) create mode 100644 src/api.js create mode 100644 src/components/search/Search.js diff --git a/package-lock.json b/package-lock.json index 3060cb6..048d8e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,9 @@ "react-scripts": "5.0.1", "react-select-async-paginate": "^0.7.3", "web-vitals": "^2.1.4" + }, + "devDependencies": { + "dotenv": "^16.4.1" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -7331,11 +7334,15 @@ } }, "node_modules/dotenv": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", - "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "version": "16.4.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.1.tgz", + "integrity": "sha512-CjA3y+Dr3FyFDOAMnxZEGtnW9KBR2M0JvvUtXNW+dYJL5ROWxP9DUHCwgFqpMk0OXCc0ljhaNTr2w/kutYIcHQ==", + "dev": true, "engines": { - "node": ">=10" + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" } }, "node_modules/dotenv-expand": { @@ -15225,6 +15232,14 @@ } } }, + "node_modules/react-scripts/node_modules/dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "engines": { + "node": ">=10" + } + }, "node_modules/react-select": { "version": "5.8.0", "resolved": "https://registry.npmjs.org/react-select/-/react-select-5.8.0.tgz", diff --git a/package.json b/package.json index a70f3ec..451ae55 100644 --- a/package.json +++ b/package.json @@ -36,5 +36,8 @@ "last 1 firefox version", "last 1 safari version" ] + }, + "devDependencies": { + "dotenv": "^16.4.1" } } diff --git a/src/App.css b/src/App.css index 74b5e05..e173d92 100644 --- a/src/App.css +++ b/src/App.css @@ -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; } diff --git a/src/App.js b/src/App.js index 3784575..43e2310 100644 --- a/src/App.js +++ b/src/App.js @@ -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 ( -
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
+
+
); } diff --git a/src/api.js b/src/api.js new file mode 100644 index 0000000..5b30936 --- /dev/null +++ b/src/api.js @@ -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'; diff --git a/src/components/search/Search.js b/src/components/search/Search.js new file mode 100644 index 0000000..e2d4e69 --- /dev/null +++ b/src/components/search/Search.js @@ -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 ( + + ); +}; +export default Search; diff --git a/src/index.css b/src/index.css index ec2585e..a79df11 100644 --- a/src/index.css +++ b/src/index.css @@ -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 {