|
| 1 | +import React from "react"; |
| 2 | +import Clock from "react-live-clock"; |
| 3 | + |
| 4 | +import AnimatedWeatherIcon from "./components/AnimatedWeather"; |
| 5 | +import Forcast from "./Forcast"; |
| 6 | +import loader from "./images/WeatherIcons.gif"; |
| 7 | +import { dateBuilder, getPosition, getWeatherByCoordinate } from "./utils"; |
| 8 | + |
| 9 | +const ONE_MINUTE_IN_MS = 60 * 1000; |
| 10 | + |
| 11 | +export class Weather extends React.Component { |
| 12 | + state = { |
| 13 | + coords: { |
| 14 | + latitude: undefined, |
| 15 | + longitude: undefined, |
| 16 | + }, |
| 17 | + errorMessage: undefined, |
| 18 | + temperatureC: undefined, |
| 19 | + temperatureF: undefined, |
| 20 | + city: undefined, |
| 21 | + country: undefined, |
| 22 | + humidity: undefined, |
| 23 | + description: undefined, |
| 24 | + icon: "CLEAR_DAY", |
| 25 | + sunrise: undefined, |
| 26 | + sunset: undefined, |
| 27 | + errorMsg: undefined, |
| 28 | + timerID: undefined, |
| 29 | + }; |
| 30 | + |
| 31 | + async updateWeather() { |
| 32 | + const weather = await getWeatherByCoordinate(this.state.coords); |
| 33 | + const timerID = setTimeout(this.updateWeather, 10 * ONE_MINUTE_IN_MS); |
| 34 | + return this.setState({ ...this.state, timerID, ...weather }); |
| 35 | + } |
| 36 | + |
| 37 | + async componentDidMount() { |
| 38 | + if (!navigator.geolocation) { |
| 39 | + return alert("Geolocation not available"); |
| 40 | + } |
| 41 | + |
| 42 | + let position; |
| 43 | + |
| 44 | + try { |
| 45 | + position = await getPosition(); |
| 46 | + } catch (err) { |
| 47 | + position = { coords: { latitude: 28.67, longitude: 77.22 } }; |
| 48 | + alert( |
| 49 | + "You have disabled location service. Allow 'This APP' to access your location. " + |
| 50 | + "Your current location will be used for calculating Real time weather." |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + return this.setState((prevState) => ({ ...prevState, coords: position.coords }), this.updateWeather); |
| 55 | + } |
| 56 | + |
| 57 | + componentWillUnmount() { |
| 58 | + return clearTimeout(this.state.timerID); |
| 59 | + } |
| 60 | + |
| 61 | + render() { |
| 62 | + if (!this.state.temperatureC) { |
| 63 | + return ( |
| 64 | + <React.Fragment> |
| 65 | + <img alt="loader" src={loader} style={{ width: "50%", WebkitUserDrag: "none" }} /> |
| 66 | + <h3 style={{ color: "white", fontSize: "22px", fontWeight: "600" }}>Detecting your location</h3> |
| 67 | + <h3 style={{ color: "white", marginTop: "10px" }}> |
| 68 | + Your current location wil be displayed on the App <br></br> & used for calculating Real time weather. |
| 69 | + </h3> |
| 70 | + </React.Fragment> |
| 71 | + ); |
| 72 | + } |
| 73 | + return ( |
| 74 | + <React.Fragment> |
| 75 | + <div className="city"> |
| 76 | + <div className="title"> |
| 77 | + <h2>{this.state.city}</h2> |
| 78 | + <h3>{this.state.country}</h3> |
| 79 | + </div> |
| 80 | + <AnimatedWeatherIcon className="mb-icon" icon={this.state.icon} title={this.state.main} /> |
| 81 | + <div className="date-time"> |
| 82 | + <div className="dmy"> |
| 83 | + <div id="txt"></div> |
| 84 | + <div className="current-time"> |
| 85 | + <Clock format="HH:mm:ss" interval={1000} ticking={true} /> |
| 86 | + </div> |
| 87 | + <div className="current-date">{dateBuilder(new Date())}</div> |
| 88 | + </div> |
| 89 | + <div className="temperature"> |
| 90 | + <p> |
| 91 | + {this.state.temperatureC}°<span>C</span> |
| 92 | + </p> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + <Forcast icon={this.state.icon} weather={this.state.main} /> |
| 97 | + </React.Fragment> |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export default Weather; |
0 commit comments