Skip to content

Commit

Permalink
added react-router-dom, spun out Home, LocationInput, Forecast compon…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
bdhsu committed Jan 13, 2019
1 parent b2c5262 commit 0888403
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 71 deletions.
87 changes: 87 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"normalize.css": "^8.0.1",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.3"
},
"scripts": {
Expand Down
81 changes: 12 additions & 69 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,10 @@ import 'normalize.css';
import './App.css';
import api from './utils/api';
import PropTypes from 'prop-types';

class LocationInput extends Component {
constructor(props) {
super(props);

this.state = {
location: '',
};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
var value = event.target.value;

this.setState(_ => {
return {
location: value,
}
});
}
handleSubmit(event) {
event.preventDefault();

this.props.onSubmit(
this.state.location,
);
}
render() {
return (
<div className="loc-container">
<form className="getweather" onSubmit={this.handleSubmit} style={this.props.style}>
<input
id="location"
placeholder="San Francisco, CA"
type="text"
className="locinput"
autoComplete="off"
value={this.state.location}
onChange={this.handleChange}
/>
<button
className="btn"
type="submit"
disabled={!this.state.location}>
Get Weather
</button>
</form>
</div>
)
}
}

// LocationInput.propTypes = {
// location: PropTypes.string.isRequired,
// }


import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
import Home from './Home';
import Forecast from './Forecast';
import LocationInput from './LocationInput';

class App extends Component {
constructor(props) {
Expand All @@ -75,10 +21,8 @@ class App extends Component {
handleSubmit(location) {
console.log('location: ' + location);

api.fetchCurrentWeather(location);
// this.setState(_ => {
//
// });
// api.fetchCurrentWeather(location);
api.fetchFiveDayForecast(location);
}

render() {
Expand All @@ -90,13 +34,12 @@ class App extends Component {
</div>
<LocationInput style={{flexDirection: "row"}} />
</header>
<div className="container">
<h1 className="text-center">Enter a City and State</h1>
<LocationInput
style={{flexDirection: "column"}}
onSubmit={this.handleSubmit}
/>
</div>
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/forecast" component={Forecast} />
</Switch>
</Router>
</div>
);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Forecast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { Component } from 'react';
import { Link } from 'react-router-dom';

class Forecast extends React.Component {
render() {
return (
<div className="container">
Forecast Component
</div>
)
}
}

export default Forecast;
20 changes: 20 additions & 0 deletions src/Home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';
import api from './utils/api';

class Home extends Component {
render() {
return (
<div className="container">
<h1 className="text-center">Enter a City and State</h1>
<LocationInput
style={{flexDirection: "column"}}
onSubmit={this.handleSubmit}
/>
</div>
)
}
}

export default Home;
61 changes: 61 additions & 0 deletions src/LocationInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, { Component } from 'react';
import api from './utils/api';

class LocationInput extends Component {
constructor(props) {
super(props);

this.state = {
location: '',
};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
var value = event.target.value;

this.setState(_ => {
return {
location: value,
}
});
}
handleSubmit(event) {
event.preventDefault();

this.props.onSubmit(
this.state.location,
);
}
render() {
return (
<div className="loc-container">
<form className="getweather" onSubmit={this.handleSubmit} style={this.props.style}>
<input
id="location"
placeholder="San Francisco, CA"
type="text"
className="locinput"
autoComplete="off"
value={this.state.location}
onChange={this.handleChange}
/>
<button
className="btn"
type="submit"
disabled={!this.state.location}>
Get Weather
</button>
</form>

</div>
)
}
}

// LocationInput.propTypes = {
// location: PropTypes.string.isRequired,
// }

export default LocationInput;
13 changes: 11 additions & 2 deletions src/utils/api.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
var axios = require('axios');

var baseURL = "http://api.openweathermap.org/data/2.5/";
var apikey = "8fad34166684286360dc6d77073bd5e5";
var params = "&APPID=" + apikey;

module.exports = {
fetchCurrentWeather: function(location) {
var link = window.encodeURI("http://api.openweathermap.org/data/2.5/weather?q=" + location + params);
var link = window.encodeURI(baseURL + "weather?q=" + location + params);
return axios.get(link)
.then(function(results) {
console.log('results: ' + results);
console.log('current weather results: ' + results);
console.log(JSON.stringify(results["data"]["weather"]));
});
},
fetchFiveDayForecast: function(location) {
var link = window.encodeURI(baseURL + "forecast?q=" + location + ",us" + params);
return axios.get(link)
.then(function(results) {
console.log('forecast results: ' + results);
console.log(JSON.stringify(results));
});
}
Expand Down

0 comments on commit 0888403

Please sign in to comment.