Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4.  
  5. const PLACES = [
  6. { name: "Moscow", zip: "119311" },
  7. { name: "St. Petersburg", zip: "188506" },
  8. { name: "Obninsk", zip: "249032" },
  9. { name: "Missoula", zip: "59847" }
  10. ]
  11.  
  12. function toCelsius(fahrenheit) {
  13. return Math.round((fahrenheit - 32) * 5 / 9);
  14. }
  15.  
  16. class Weather extends React.Component{
  17.  
  18. constructor(){
  19. super();
  20. this.state = {
  21. weatherData: null
  22. }
  23. }
  24.  
  25. componentDidMount(){
  26. const zip = this.props.zip;
  27. const URL = "http://api.openweathermap.org/data/2.5/weather?q=" + zip + "&appid=b1b35bba8b434a28a0be2a3e1071ae5b&units=imperial";
  28. fetch(URL).then(res => res.json()).then(json => this.setState({weatherData: json}))
  29. };
  30.  
  31. render(){
  32. const weatherData = this.state.weatherData;
  33. if (!weatherData) return <div>Loading...</div>
  34. const weather = weatherData.weather[0];
  35. const iconURL = "http://openweathermap.org/img/w/" + weather.icon + ".png";
  36. return(
  37. <div>
  38. <h1>
  39. {weather.main} in {weatherData.name}
  40. <img src = {iconURL} alt = {weatherData.description} />
  41. </h1>
  42. <p>Сейчас: {toCelsius(weatherData.main.temp)}&#8451;</p>
  43. <p>Максимум: {toCelsius(weatherData.main.temp_max)}&#8451;</p>
  44. <p>Минимум: {toCelsius(weatherData.main.temp_min)}&#8451;</p>
  45. <p>Ветер: {weatherData.wind.speed} м/с</p>
  46. </div>
  47. );
  48.  
  49. }
  50. }
  51.  
  52. class App extends React.Component {
  53. constructor(){
  54. super();
  55. this.state = {
  56. activePlace: 0,
  57. };
  58. }
  59. render() {
  60. const activePlace = this.state.activePlace;
  61. return (
  62. <div className="App">
  63. {
  64. PLACES.map((place, index) =>
  65. <button
  66. key = {index}
  67. onClick = {() =>
  68. this.setState({activePlace: index})
  69. }
  70. >
  71. {place.name}
  72. </button>)
  73. }
  74. <Weather
  75. key = {activePlace}
  76. zip = {PLACES[activePlace].zip} />
  77. </div>
  78. );
  79. }
  80. }
  81.  
  82. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement