From 51d0b305fc65bff2976cb5ab28d4043b1cfa9ede Mon Sep 17 00:00:00 2001 From: jpntc Date: Thu, 10 Oct 2024 11:57:01 -0400 Subject: [PATCH] finished assignment --- src/App.css | 17 ++++++++ src/App.jsx | 113 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 124 insertions(+), 6 deletions(-) diff --git a/src/App.css b/src/App.css index c7f4da8..f6f9a41 100644 --- a/src/App.css +++ b/src/App.css @@ -4,3 +4,20 @@ color: white; text-align: center; } +.label{ + margin-top: 10px; + font-weight: bold; +} + +.zipSearchField{ + margin-top: 20px; +} +.city{ + background-color: rgb(204 205 203); + padding-left: 10px; + font-weight: 500; +} +.fields{ + padding: 10px 60px 10px 60px; + font-weight:500 ; +} \ No newline at end of file diff --git a/src/App.jsx b/src/App.jsx index 6478b09..787f935 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,25 +1,126 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import "./App.css"; function City(props) { - return
This is the City component
; + const[formattedCityName, setFormattedCityName] = useState("") + + + function toTitleCase(str) { + let formatted_name = str.toLowerCase().split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' '); + setFormattedCityName(formatted_name); + } + + useEffect(()=>{ + toTitleCase(props.city); + }, [props]) + + return ( +
+
{`${formattedCityName}, ${props.state}`}
+ +
+ ); } function ZipSearchField(props) { - return
This is the ZipSearchField component
; + + const [input, setInput] = useState(""); + + function handleChange(newInput){ + const text = newInput.target.value + setInput(text) + } + + + // async function fetchCities(zips){ + // const endpoint = `https://ctp-zip-code-api.onrender.com/zip/${input}`; + // fetch(endpoint) + // .then((response) => response.json()) + // .then((data) => { + // console.log("data", data); + // }) + // .catch((err) => { + // alert("An error occurred when getting the information."), + // console.log("Error fetching from endpoint: ", err); + // }); + // } + function handleSubmit(e){ + e.preventDefault(); + console.log(input) + if(input === ""){ + return + } + if(parseInt(input)){ + const endpoint = `https://ctp-zip-code-api.onrender.com/zip/${input}`; + fetch(endpoint) + .then((response) =>response.json()) + .then( data => {console.log("data", data); props.copyResults(data)}) + .catch(err=>{alert("An error occurred when getting the information."), console.log("Error fetching from endpoint: ",err)}) + // }else{ + // const upper_input = input.toUpperCase(); + // const endpoint = `https://ctp-zip-code-api.onrender.com/city/${upper_input}`; + // fetch(endpoint) + // .then((response) => + // response.json() + // ) + // .then((data) => fetchCity(data)) + // .catch((err) => { + // alert("An error occurred when getting the information."), + // console.log("Error fetching from endpoint: ", err); + // }); + } +} + return ( +
+
+ + handleChange(e)} + /> +
+
+ ); } function App() { + const [results, setResults] = useState([]); + + function copyResults(data){ + setResults(data); + } return (

Zip Code Search

- +
- - + { + + results.map((result, index)=>( + + )) + }