diff --git a/src/App.css b/src/App.css index c7f4da8..00a65fd 100644 --- a/src/App.css +++ b/src/App.css @@ -3,4 +3,31 @@ padding: 20px; color: white; text-align: center; -} + } + + .zip-search-field { + margin-bottom: 20px; + margin-top: 10px; + text-align: center; + } + + .zip-search-field input { + padding: 8px; + font-size: 16px; + } + + .city-result { + margin: 25px 0; + padding: 10px; + } + + + .city-result h3 { + font-size: 18px; + } + + .error { + color: red; + text-align: center; + } + \ No newline at end of file diff --git a/src/App.jsx b/src/App.jsx index 6478b09..1763f74 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,25 +1,82 @@ import { useState } from "react"; import "./App.css"; -function City(props) { - return
This is the City component
; +function City( props ) { + return ( +
+
+

{props.City}, {props.State}

+
+ +
+ ); } -function ZipSearchField(props) { - return
This is the ZipSearchField component
; +function ZipSearchField({ handleZipSearch }) { + const [zipCode, setZipCode] = useState(""); + + const handleInputChange = (event) => { + const newZip = event.target.value; + setZipCode(newZip); + handleZipSearch(newZip); + }; + + return ( +
+ +
+ ); } function App() { + const [cities, setCities] = useState([]); + const [error, setError] = useState(""); + + const fetchData = async (zipCode) => { + if (!zipCode) { + setCities([]); + setError(""); + return; + } + + try { + const response = await fetch(`https://ctp-zip-code-api.onrender.com/zip/${zipCode}`); + if (response.ok) { + const data = await response.json(); + setCities(data); + setError(""); // clear error on success + } else { + setCities([]); + setError("No results found"); // Show error if no data returned + } + } catch (error) { + setCities([]); + setError("No results found"); // General error message + } + }; + return (

Zip Code Search

- +
- - + {error &&

{error}

} + {cities.map((city) => ( + + ))}