Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Link } from 'react-router-dom';
  4. import Utils from '../Utils'
  5.  
  6. const yrno = require('yr.no-forecast')({
  7.   version: '1.9', // this is the default if not provided,
  8.   request: {
  9.     // make calls to locationforecast timeout after 15 seconds
  10.     timeout: 15000
  11.   }
  12. });
  13.  
  14. export default class WeatherWidget extends Component {
  15.   constructor(props) {
  16.     super(props)
  17.     this.state = {
  18.       loading: true,
  19.       location: null,
  20.       weather: null
  21.     }
  22.   }
  23.  
  24.   componentWillMount() {
  25.     var component = this
  26.     Utils.getCurrentLocation()
  27.       .then((loc) => {
  28.         yrno.getWeather(loc)
  29.           .then((weather) => {
  30.             // Get general weather for next five days (Array with five objects)
  31.             weather.getFiveDaySummary()
  32.               .then((data) => component.setState(
  33.                 {
  34.                   weather: data[0],
  35.                   loading: false
  36.                 }))
  37.           })
  38.           .catch((e) => {
  39.             console.log('an error occurred!', e);
  40.           });
  41.       })
  42.   }
  43.  
  44.   render() {
  45.     if (this.state.loading) {
  46.       return <p> Loading... </p>
  47.     } else {
  48.     return (
  49.       <div>
  50.         <img src=`"./components/widgets/img/${this.state.weather.icon}.png"` alt="Current Weather Icon" width="195" height="168"/>
  51.         <p> Icon: {this.state.weather.icon} </p>
  52.         <p> Current Temp: {this.state.weather.temperature.value} Celcius </p>
  53.         <p> Rain: {this.state.weather.rain}</p>
  54.         <p> Wind: {this.state.weather.windSpeed.name}, {this.state.weather.windSpeed.mps}mps {this.state.weather.windDirection.name} </p>
  55.       </div>
  56.     )};
  57.   }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement