mirror of
https://github.com/Raghu-Ch/ReactWeather.git
synced 2026-02-10 11:03:02 -05:00
Initial commit React Weather App
This commit is contained in:
49
app/components/Weather.jsx
Normal file
49
app/components/Weather.jsx
Normal file
@@ -0,0 +1,49 @@
|
||||
var React = require('react');
|
||||
var WeatherForm = require('WeatherForm');
|
||||
var WeatherMessage = require('WeatherMessage');
|
||||
var openWeatherMap = require('openWeatherMap');
|
||||
|
||||
var Weather = React.createClass({
|
||||
getInitialState: function () {
|
||||
return {
|
||||
isLoading: false
|
||||
}
|
||||
},
|
||||
handleSearch: function (location) {
|
||||
var that = this;
|
||||
|
||||
this.setState({isLoading: true});
|
||||
|
||||
openWeatherMap.getTemp(location).then(function (temp) {
|
||||
that.setState({
|
||||
location: location,
|
||||
temp: temp,
|
||||
isLoading: false
|
||||
});
|
||||
}, function (errorMessage) {
|
||||
that.setState({isLoading: false});
|
||||
alert(errorMessage);
|
||||
});
|
||||
},
|
||||
render: function () {
|
||||
var {isLoading, temp, location} = this.state;
|
||||
|
||||
function renderMessage () {
|
||||
if (isLoading) {
|
||||
return <h3>Fetching weather...</h3>;
|
||||
} else if (temp && location) {
|
||||
return <WeatherMessage temp={temp} location={location}/>;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h3>Weather Component</h3>
|
||||
<WeatherForm onSearch={this.handleSearch}/>
|
||||
{renderMessage()}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Weather;
|
||||
Reference in New Issue
Block a user