From 507fd73d302e10b3f7af424c2112263faf37abfb Mon Sep 17 00:00:00 2001 From: AllenL8921 Date: Wed, 25 Sep 2024 01:21:55 -0400 Subject: [PATCH] Solution --- src/App.jsx | 77 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 6478b09..390d6a2 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,25 +1,86 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import "./App.css"; -function City(props) { - return
This is the City component
; +function City({ City, State, Lat, Long, EstimatedPopulation, TotalWages }) { + return ( +
+
+ {City}, {State} +
+ +
+
    +
  • State: {State}
  • +
  • Location: ({Lat}, {Long})
  • +
  • Population (estimated): {EstimatedPopulation}
  • +
  • Total Wages: {TotalWages}
  • +
+
+ +
+ ); } -function ZipSearchField(props) { - return
This is the ZipSearchField component
; +function ZipSearchField({ handleSearch }) { + return ( +
+ + +
+ ); } + function App() { + const [zipCode, setZipCode] = useState(''); + const [data, setData] = useState([]); + + const handleSearch = (event) => { + const value = event.target.value; + + console.log(value); + + // Check for valid zip code + if (value.length === 5 && /^[0-9]+$/.test(value)) { + setZipCode(value); + } + }; + + + const fetchData = async () => { + if (!zipCode) return; // Avoid fetching if zipCode is empty + + try { + const response = await fetch(`https://ctp-zip-code-api.onrender.com/zip/${zipCode}`); + const result = await response.json(); + + setData(result); + + console.log(result); + } catch (error) { + console.error(error); + } + + }; + + useEffect(()=>{ + fetchData(); + }, [zipCode]); + return (

Zip Code Search

- +
- - + {(data.length > 0) ? ( + data.map((city) => ( + + )) + ) : (

No results found

) + }