From 709a3bae00804463fe4d47e5a2686063989d89e9 Mon Sep 17 00:00:00 2001 From: Kazi Sadman Date: Wed, 18 Sep 2024 16:48:18 -0400 Subject: [PATCH] fixed API calls --- src/App.css | 25 +++++++++++++++++++ src/App.jsx | 72 ++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 88 insertions(+), 9 deletions(-) diff --git a/src/App.css b/src/App.css index c7f4da8..b7efc2f 100644 --- a/src/App.css +++ b/src/App.css @@ -4,3 +4,28 @@ color: white; text-align: center; } + +.App { + text-align: center; + font-family: Arial, sans-serif; +} + +.city { + border: 1px solid #ccc; + padding: 10px; + margin: 10px 0; + border-radius: 5px; +} + +.error-message { + color: red; +} + +input { + width: 100%; + padding: 8px; + margin: 10px 0; + border-radius: 4px; + border: 1px solid #ccc; +} + diff --git a/src/App.jsx b/src/App.jsx index 6478b09..1f22de9 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,26 +1,80 @@ import { useState } from "react"; import "./App.css"; -function City(props) { - return
This is the City component
; +function City({ city }) { + return ( +
+

{city.City}

+

State: {city.State}

+

Location: ({city.Lat}, {city.Long})

+

Population (estimated): {city.EstimatedPopulation}

+

Total Wages: ${city.TotalWages}

+
+ ); } -function ZipSearchField(props) { - return
This is the ZipSearchField component
; +function ZipSearchField({ onSearch }) { + const [zip, setZip] = useState(""); + + const handleInputChange = (e) => { + setZip(e.target.value); + }; + + const handleKeyDown = (e) => { + if (e.key === "Enter") { + onSearch(zip); + } + }; + + return ( +
+ + +
+ ); } function App() { + const [cities, setCities] = useState([]); + const [error, setError] = useState(null); + + const fetchCityData = async (zip) => { + setError(null); + try { + const response = await fetch(`https://ctp-zip-code-api.onrender.com/zip/${zip}`); + if (!response.ok) { + throw new Error("No results found"); + } + const data = await response.json(); + setCities(data); + } catch (err) { + setCities([]); + setError(err.message); + } + }; + return (

Zip Code Search

- -
- - -
+ + {error ? ( +
{error}
+ ) : ( +
+ {cities.map((city) => ( + + ))} +
+ )}
);