Zipcar React
by Matthew Vasallo
July 11, 2019
HTML
<div id="app"></div>
<!--
Tickets:
-Display the current weather in fahrenheit
-Include humidity
-Hook it up to live data using OpenWeatherMap
-Docs: https://openweathermap.org/api
-API Key: df8f9d34fbbc4d4a81fc7469e821616b
>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
height: 100vh;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
React
const WeatherCard = ({tempMin, tempMax, description, humidity}) => {
return (
<div className="weather-card">
<div>
Low of {tempMin}, high of {tempMax}, humidity of {humidity}%
</div>
<div>
{description}
</div>
</div>
)
}
class WeatherApp extends React.Component {
constructor(props) {
super(props)
this.state = {
isLoading: true
}
}
componentDidMount(){
this.setState({
isLoading:true
});
setTimeout(()=>{this.fetchData()}, 3000);
}
fetchData(){
const url = "https://api.openweathermap.org/data/2.5/weather?zip=33157&appid=df8f9d34fbbc4d4a81fc7469e821616b&units=imperial";
return fetch(url).then(res =>res.json()).then(json=>{
this.setState({
isLoading:false,
weather:{
tempMin: json.main.temp_min,
tempMax: json.main.temp_max,
humidity: json.main.humidity,
description: json.weather[0].description
}
});
});
}
render() {
return (
<div className="weather-app-body">
<div className="weather-card">
<h2>Weather:</h2>
{this.state.isLoading ? <div>Loading...</div> : <WeatherCard {...this.state.weather}/>}
</div>
</div>
)
}
}
ReactDOM.render(<WeatherApp />, document.querySelector("#app"))