From 049290d835fcb61c4807914e3315e5d36a02e586 Mon Sep 17 00:00:00 2001 From: Edgardo Date: Sun, 17 Sep 2017 14:56:48 -0400 Subject: [PATCH 1/2] Add solution developed in class with comments. --- zip-search/src/App.js | 108 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 7 deletions(-) diff --git a/zip-search/src/App.js b/zip-search/src/App.js index a505dbef..bf445693 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 would be thrown and the + catch() function below would execute. The exception + occurs because the API does not return a 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
} +
+
); From 208057f8bfbb98050363765e0d435d8495b74b5b Mon Sep 17 00:00:00 2001 From: medgardo Date: Sun, 17 Sep 2017 22:00:29 -0400 Subject: [PATCH 2/2] Update comment in App.js --- zip-search/src/App.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/zip-search/src/App.js b/zip-search/src/App.js index bf445693..d52caefe 100644 --- a/zip-search/src/App.js +++ b/zip-search/src/App.js @@ -73,10 +73,10 @@ class App extends Component { } /* if we were to just return response.json() here - then an exception would be thrown and the - catch() function below would execute. The exception - occurs because the API does not return a json body - when a 404 occurs. + 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) => {