From eb481d98f8ab7e81e0764ae227fd5db2adef7645 Mon Sep 17 00:00:00 2001 From: Alif Rahman Date: Tue, 17 Sep 2024 23:49:27 -0400 Subject: [PATCH 1/2] might be over --- src/App.jsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/App.jsx b/src/App.jsx index 6478b09..3a61120 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -6,7 +6,23 @@ function City(props) { } function ZipSearchField(props) { - return
This is the ZipSearchField component
; + const [input, setInput] = useState(''); + + const handleChange = (event) => { + setInput(event.target.value); + } + + return <> + Zip Code: + + ; } function App() { From 460cf154831b672325c27f0aa18346f06369831a Mon Sep 17 00:00:00 2001 From: Alif Rahman Date: Wed, 18 Sep 2024 18:28:00 -0400 Subject: [PATCH 2/2] give up --- src/App.jsx | 109 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 87 insertions(+), 22 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 3a61120..84b532c 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,45 +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}
+
+
    +
  • Region: {region}
  • +
  • + Coordinates: ({latitude}, {longitude}) +
  • +
  • Estimated Population: {population}
  • +
  • Total Income: {totalIncome}
  • +
+
+
+ ); } -function ZipSearchField(props) { - const [input, setInput] = useState(''); +function ZipCodeInput({ onLookup }) { + const [inputValue, setInputValue] = useState(""); - const handleChange = (event) => { - setInput(event.target.value); - } + 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 <> - Zip Code: - + ); } -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;