-
Notifications
You must be signed in to change notification settings - Fork 99
Marina's weather app #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mannushka
wants to merge
9
commits into
rocketacademy:main
Choose a base branch
from
Mannushka:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b789249
axios installed
Mannushka 1b66f2c
axios installed
Mannushka fed60e9
background and font colors changed
Mannushka 4bbd20b
input added, axios get request added
Mannushka 4371eb4
input field and button size changed
Mannushka 353e0ab
input classname added
Mannushka 4521126
dotenv installed
Mannushka 23e722b
dotenv installed
Mannushka 14e2953
API key hidden
Mannushka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,84 @@ | ||
| import React from "react"; | ||
| import logo from "./logo.png"; | ||
| import "./App.css"; | ||
| import axios from "axios"; | ||
|
|
||
| class App extends React.Component { | ||
| constructor() { | ||
| super(); | ||
| this.state = { | ||
| input: " ", | ||
| weatherData: [], | ||
| }; | ||
| } | ||
|
|
||
| handleChange = (e) => { | ||
| const inputValue = e.target.value; | ||
| this.setState({ input: inputValue }); | ||
| }; | ||
|
|
||
| handleSubmit = (e) => { | ||
| e.preventDefault(); | ||
| axios | ||
| .get( | ||
| `https://api.openweathermap.org/geo/1.0/direct?q=${this.state.input}&limit=1&appid=${process.env.REACT_APP_API_KEY}` | ||
| ) | ||
| // City geo data is in response.data[0] | ||
| // Arrow functions with no curly braces return value after arrow | ||
| .then((response) => response.data[0]) | ||
| .then((cityGeoData) => | ||
| axios.get( | ||
| `https://api.openweathermap.org/data/2.5/weather?lat=${cityGeoData.lat}&lon=${cityGeoData.lon}&appid=${process.env.REACT_APP_API_KEY}&units=metric` | ||
| ) | ||
| ) | ||
| .then((response) => { | ||
| const { data: weatherData } = response; | ||
| this.setState({ | ||
| weatherData: [...this.state.weatherData, weatherData], | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| render() { | ||
| return ( | ||
| <div className="App"> | ||
| <header className="App-header"> | ||
| <img src={logo} className="App-logo" alt="logo" /> | ||
| <p> | ||
| Edit <code>src/App.js</code> and save to reload. | ||
| </p> | ||
| </header> | ||
| <h1>Welcome to my weather app 🤠</h1> | ||
| <p> | ||
| Please type in any city's name into the input field below and click | ||
| "submit" to get the weather info 😉 | ||
| </p> | ||
|
|
||
| <form> | ||
| <input | ||
| className="input-field" | ||
| type="text" | ||
| placeholder="enter the city name" | ||
| value={this.state.input} | ||
| onChange={this.handleChange} | ||
| ></input> | ||
| <input | ||
| className="button" | ||
| type="submit" | ||
| value="submit" | ||
| onClick={this.handleSubmit} | ||
| ></input> | ||
| </form> | ||
|
|
||
| {this.state.weatherData && | ||
| this.state.weatherData.length > 0 && | ||
| this.state.weatherData.map((location) => ( | ||
| <div key={location.name}> | ||
| <h2>Current weather in {location.name}:</h2> | ||
| <img | ||
| src={`https://openweathermap.org/img/wn/${location.weather[0].icon}@2x.png`} | ||
| alt="icon" | ||
| /> | ||
| <p>{location.weather[0].description}</p> | ||
|
|
||
| <p>Temperature: {Math.floor(location.main.temp)} °C</p> | ||
| <p>Feels like: {Math.floor(location.main.feels_like)} °C</p> | ||
| <p>Wind speed: {Math.floor(location.wind.speed)} m/sec</p> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| ); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. textbook implementation, perfect! |
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be sufficient. An array is always truthy, thus
this.state.weatherDatais always true.When running
.map()on an empty array, we do not run the callback function, so just running map directly is totally fine.