diff --git a/src/App.jsx b/src/App.jsx index 6478b09..84b532c 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,29 +1,110 @@ import { useState } from "react"; import "./App.css"; -function City(props) { - return
This is the City component
; +function LocationCard({ + region, + latitude, + longitude, + population, + totalIncome, + locationName, +}) { + return ( +
+
{locationName}
+
+ +
+
+ ); } -function ZipSearchField(props) { - return
This is the ZipSearchField component
; +function ZipCodeInput({ onLookup }) { + const [inputValue, setInputValue] = useState(""); + + const handleInputChange = (event) => { + const formattedZip = event.target.value.replace(/\D+/g, "").slice(0, 5); + setInputValue(formattedZip); + console.log("Zip Code changed to:", formattedZip); + + if (formattedZip.length === 5 && !isNaN(parseInt(formattedZip))) { + onLookup(formattedZip); + } + }; + + return ( + + ); } -function App() { +async function fetchLocationData(zipCode, onSuccess) { + try { + const response = await fetch( + `https://ctp-zip-code-api.onrender.com/zip/${zipCode}` + ); + + if (!response.ok) { + console.error( + `Error fetching data for ZIP: ${zipCode}, Status: ${response.status}` + ); + onSuccess([]); + return; + } + + const contentType = response.headers.get("content-type"); + if (contentType && contentType.indexOf("application/json") !== -1) { + const data = await response.json(); + console.log("Data fetched successfully:", data); + onSuccess(data); + } else { + console.error("Unexpected content type, expected JSON."); + onSuccess([]); + } + } catch (error) { + console.error("Fetch error:", error); + onSuccess([]); + } +} + +function ZipApp() { + const [locationList, setLocationList] = useState([]); + + const searchLocation = (zipCode) => { + console.log("Fetching data for zip code:", zipCode); + fetchLocationData(zipCode, setLocationList); + }; + return (
-

Zip Code Search

+

Find Location by Zip Code

-
- +
+
- - + {locationList.length > 0 + ? locationList.map((location) => ) + : "No locations found"}
); } -export default App; +export default ZipApp;