diff --git a/src/App.jsx b/src/App.jsx
index 6478b09..250fda2 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -1,12 +1,60 @@
-import { useState } from "react";
+import { useState, useEffect } from "react";
import "./App.css";
function City(props) {
- return
This is the City component
;
+ return (
+
+
{props.City}, {props.State}
+
State: {props.State}
+
Location: ({props.Lat}, {props.Long})
+
Population (estimated): {props.EstimatedPopulation}
+
Total Wages: {props.TotalWages}
+
+ );
}
function ZipSearchField(props) {
- return This is the ZipSearchField component
;
+ const [inputText, setInputText] = useState('');
+ const [isValidZip, setIsValidZip] = useState(false);
+ const [cityRecords, setCityRecords] = useState([]);
+
+ const validateZip = (zip) => /^\d{5}$/.test(zip);
+ const handleInputChange = (event) => {
+ const zip = event.target.value;
+ setInputText(zip);
+ setIsValidZip(validateZip(zip));
+ }
+
+ useEffect (() => {
+ if (isValidZip) {
+ const fetchCityRecords = async () => {
+ try {
+ const response = await fetch(`https://ctp-zip-code-api.onrender.com/zip/${inputText}`);
+ const cityData = await response.json();
+ if (cityData.length > 0) {
+ setCityRecords(cityData);
+ } else {
+ setCityRecords([]);
+ console.log('No city data found for this zip code.');
+ }
+ } catch (error) {
+ console.error('Error fetching city data: ', error);
+ }
+ }
+
+ fetchCityRecords();
+ } else {
+ setCityRecords([]);
+ }
+ }, [isValidZip, inputText]);
+
+ return (
+
+
Zip Code:
+
+ {cityRecords.length > 0 ? cityRecords.map((record) =>
) :
No results found
}
+
+ );
}
function App() {
@@ -17,10 +65,6 @@ function App() {
);