diff --git a/zip-search/src/App.js b/zip-search/src/App.js index a505dbef..d52caefe 100644 --- a/zip-search/src/App.js +++ b/zip-search/src/App.js @@ -1,28 +1,122 @@ import React, { Component } from 'react'; -import logo from './logo.svg'; import './App.css'; function City(props) { - return (
); + return ( +
+
+
+
+

{props.data.LocationText}

+
+
+
    +
  • State: {props.data.State}
  • +
  • Location: ({props.data.Lat}, {props.data.Long})
  • +
  • Population (estimated): {props.data.EstimatedPopulation}
  • +
  • Total Wages: {props.data.TotalWages}
  • + {/* You can add any other data points you want here */} +
+
+
+
+
+ ); } function ZipSearchField(props) { - return (
); + return ( +
+
+ + +
+
+ ); } + class App extends Component { + constructor() { + super(); + this.state = { + zipValue: "", + cities: [], + } + + // Don't forget to bind the event handler + this.zipValueChanged = this.zipValueChanged.bind(this); + } + + zipValueChanged(event) { + const zip = event.target.value; + + this.setState({ + zipValue: zip, + }) + + if(zip.length === 5) { + fetch('http://ctp-zip-api.herokuapp.com/zip/'+zip) + .then((response) => { + if(response.ok) { + return response.json(); + } else { + return []; + } + /* + if we were to just return response.json() here + then an exception will be thrown if there is an + error, and the catch() function below would execute. + The exception occurs because the API does not return + a proper json body when a 404 occurs. + */ + }) + .then((jsonResponse) => { + const cities = jsonResponse.map((city) => { + return ; + }); + + this.setState({ + cities: cities, + }); + }) + .catch((e) => { + this.setState({ + cities: [], + }); + console.log("In catch: " + e); + }); + } else { + this.setState({ + cities: [], + }); + } + } + render() { return (

Zip Code Search

- -
- - +
+
+ {/* the following classes centers the 6 columns */} +
+ + {this.state.cities.length > 0 ? this.state.cities :
No Results
} +
+
);