From 0ecc3dfd7cf36291d899bb370ad8fbececf1fe32 Mon Sep 17 00:00:00 2001 From: jascharan-singh Date: Wed, 25 Sep 2024 20:40:32 -0400 Subject: [PATCH] done --- src/App.jsx | 45 +++++++++++++++++++++++++++++++----------- src/City.jsx | 29 +++++++++++++++++++++++++++ src/ZipSearchField.jsx | 25 +++++++++++++++++++++++ 3 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 src/City.jsx create mode 100644 src/ZipSearchField.jsx diff --git a/src/App.jsx b/src/App.jsx index 6478b09..d693f6b 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,29 +1,52 @@ import { useState } from "react"; import "./App.css"; +import City from "./City"; +import ZipSearchField from "./ZipSearchField"; -function City(props) { - return
This is the City component
; -} - -function ZipSearchField(props) { - return
This is the ZipSearchField component
; -} function App() { + const [cities, setCities] = useState([]); + const [error, setError] = useState(null); + + + const fetchCitiesByZip = (zip) => { + if (zip.length === 5) { + fetch(`https://ctp-zip-code-api.onrender.com/zip/${zip}`) + .then((response) => { + if (!response.ok) { + throw new Error("No results found for this ZIP code."); + } + return response.json(); + }) + .then((data) => { + setCities(data); + setError(null); + }) + .catch((err) => { + setError(err.message); + setCities([]); + }); + } else { + setCities([]); + } + }; + return (

Zip Code Search

- +
- - + {error &&

{error}

} + {cities.length > 0 + ? cities.map((cityData, index) => ) + : !error &&

No city data available. Please enter a valid ZIP code.

}
); } -export default App; +export default App; \ No newline at end of file diff --git a/src/City.jsx b/src/City.jsx new file mode 100644 index 0000000..b88342a --- /dev/null +++ b/src/City.jsx @@ -0,0 +1,29 @@ +function City({ cityData }) { + return ( +
+
+

{cityData.City}

+
+
+
+
+ State: {cityData.State} +
+
+ Coordinates: ({cityData.Lat}, {cityData.Long}) +
+
+
+
+ Total Wages: ${cityData.TotalWages.toLocaleString()} +
+
+ Estimated Population: {cityData.EstimatedPopulation.toLocaleString()} +
+
+
+
+ ); +} + +export default City; diff --git a/src/ZipSearchField.jsx b/src/ZipSearchField.jsx new file mode 100644 index 0000000..2c293ec --- /dev/null +++ b/src/ZipSearchField.jsx @@ -0,0 +1,25 @@ +import { useState } from "react"; +function ZipSearchField({ onZipChange }) { + const [zip, setZip] = useState(""); + + + const handleInputChange = (e) => { + const newZip = e.target.value; + setZip(newZip); + onZipChange(newZip); + }; + + return ( +
+ + +
+ ); + } + export default ZipSearchField; \ No newline at end of file