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:
11
app/components/About.jsx
Normal file
11
app/components/About.jsx
Normal file
@@ -0,0 +1,11 @@
|
||||
var React = require('react');
|
||||
|
||||
var About = React.createClass({
|
||||
render: function (){
|
||||
return (
|
||||
<h3>About Component</h3>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = About;
|
||||
11
app/components/Examples.jsx
Normal file
11
app/components/Examples.jsx
Normal file
@@ -0,0 +1,11 @@
|
||||
var React = require('react');
|
||||
|
||||
var Examples = React.createClass({
|
||||
render: function () {
|
||||
return (
|
||||
<h3>Examples Component</h3>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Examples;
|
||||
17
app/components/Main.jsx
Normal file
17
app/components/Main.jsx
Normal file
@@ -0,0 +1,17 @@
|
||||
var React = require('react');
|
||||
var Nav = require('Nav');
|
||||
|
||||
var Main = React.createClass({
|
||||
render: function () {
|
||||
return (
|
||||
<div>
|
||||
<Nav/>
|
||||
<h2>Main Component</h2>
|
||||
{this.props.children}
|
||||
</div>
|
||||
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Main;
|
||||
17
app/components/Nav.jsx
Normal file
17
app/components/Nav.jsx
Normal file
@@ -0,0 +1,17 @@
|
||||
var React = require('react');
|
||||
var {Link, IndexLink} = require('react-router');
|
||||
|
||||
var Nav = React.createClass({
|
||||
render: function (){
|
||||
return (
|
||||
<div>
|
||||
<h2>Nav Component</h2>
|
||||
<IndexLink to="/" activeClassName="active" activeStyle={{fontWeight: 'bold'}}> Get Weather</IndexLink>
|
||||
<Link to="/about" activeClassName="active" activeStyle={{fontWeight: 'bold'}}> About</Link>
|
||||
<Link to="/examples" activeClassName="active" activeStyle={{fontWeight: 'bold'}}> Examples</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = Nav;
|
||||
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;
|
||||
26
app/components/WeatherForm.jsx
Normal file
26
app/components/WeatherForm.jsx
Normal file
@@ -0,0 +1,26 @@
|
||||
var React = require('react');
|
||||
|
||||
var WeatherForm = React.createClass({
|
||||
onFormSubmit: function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
var location = this.refs.location.value;
|
||||
|
||||
if (location.length > 0) {
|
||||
this.refs.location.value = '';
|
||||
this.props.onSearch(location);
|
||||
}
|
||||
},
|
||||
render: function () {
|
||||
return (
|
||||
<div>
|
||||
<form onSubmit={this.onFormSubmit}>
|
||||
<input type="text" ref="location"/>
|
||||
<button>Get Weather</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = WeatherForm;
|
||||
13
app/components/WeatherMessage.jsx
Normal file
13
app/components/WeatherMessage.jsx
Normal file
@@ -0,0 +1,13 @@
|
||||
var React = require('react');
|
||||
|
||||
var WeatherMessage = React.createClass({
|
||||
render: function () {
|
||||
var {temp, location} = this.props;
|
||||
|
||||
return (
|
||||
<h3>It's it {temp} in {location}.</h3>
|
||||
)
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = WeatherMessage;
|
||||
Reference in New Issue
Block a user