import React, { Component } from 'react'; import classnames from 'classnames'; import moment from 'moment'; export default class WeatherTile extends Component { constructor(props) { super(props); this.state = { latlng: '', manualEntry: false, error: false }; let api = window.api; } // geolocation and manual input functions locationSubmit() { navigator.geolocation.getCurrentPosition((res) => { let latlng = `${res.coords.latitude},${res.coords.longitude}`; this.setState({ latlng }, (err) => { console.log(err); }, { maximumAge: Infinity, timeout: 10000 }); api.action("clock", "json", latlng); api.action('weather', 'json', latlng); }); } manualLocationSubmit() { event.preventDefault(); let gpsInput = document.getElementById('gps'); let latlngNoSpace = gpsInput.value.replace(/\s+/g, ''); let latlngParse = /-?[0-9]+(?:\.[0-9]*)?,-?[0-9]+(?:\.[0-9]*)?/g; if (latlngParse.test(latlngNoSpace)) { let latlng = latlngNoSpace; this.setState({latlng}, (err) => {console.log(err)}, {maximumAge: Infinity, timeout: 10000}); api.action("clock", "json", latlng); api.action('weather', 'json', latlng); this.setState({manualEntry: !this.state.manualEntry}); } else { this.setState({error: true}); return false; } } // set appearance based on weather setColors(data) { let weatherStyle = { gradient1: "", gradient2: "", text: "" }; switch (data.daily.icon) { case "clear-day": weatherStyle = { gradient1: "#A5CEF0", gradient2: "#FEF4E0", text: "black" } break; case "clear-night": weatherStyle = { gradient1: "#56668e", gradient2: "#000080", text: "white" } break; case "rain": weatherStyle = { gradient1: "#b1b2b3", gradient2: "#b0c7ff", text: "black" }; break; case "snow": weatherStyle = { gradient1: "#eee", gradient2: "#f9f9f9", text: "black" }; break; case "sleet": weatherStyle = { gradient1: "#eee", gradient2: "#f9f9f9", text: "black" }; break; case "wind": weatherStyle = { gradient1: "#eee", gradient2: "#fff", text: "black" }; break; case "fog": weatherStyle = { gradient1: "#eee", gradient2: "#fff", text: "black" }; break; case "cloudy": weatherStyle = { gradient1: "#eee", gradient2: "#b1b2b3", text: "black" }; break; case "partly-cloudy-day": weatherStyle = { gradient1: "#fcc440", gradient2: "#b1b2b3", text: "black" }; break; case "partly-cloudy-night": weatherStyle = { gradient1: "#7f7f7f", gradient2: "#56668e", text: "white" }; break; default: weatherStyle = { gradient1: "white", gradient2: "white", text: "black" }; } return weatherStyle; } // all tile views renderWrapper(child, weatherStyle = { gradient1: "white", gradient2: "white", text: "black" } ) { return (
{child}
); } renderManualEntry() { let secureCheck; let error; if (this.state.error === true) { error =

Please try again.

} if (location.protocol === "https:") { secureCheck = this.locationSubmit()}>Detect -> } return this.renderWrapper(
this.setState({ manualEntry: !this.state.manualEntry }) }> <- {secureCheck}

Please enter your{" "} latitude and longitude .

{error}
{ if (e.key === "Enter") { e.preventDefault(); this.manualLocationSubmit(e.target.value); }} }/> this.manualLocationSubmit()} value="->"/>
); } renderNoData() { return this.renderWrapper((
this.setState({manualEntry: !this.state.manualEntry})}>

Weather

-> Set location

)); } renderWithData(data, weatherStyle) { let c = data.currently; let d = data.daily.data[0]; let da = moment.unix(d.sunsetTime).format('h:mm a') || ''; return this.renderWrapper(

Weather

this.setState({ manualEntry: !this.state.manualEntry }) }> ->

{c.summary}

{Math.round(c.temperature)}°

Sunset at {da}

, weatherStyle); } render() { let data = !!this.props.data ? this.props.data : {}; if (this.state.manualEntry === true) { return this.renderManualEntry(); } if ('currently' in data && 'daily' in data) { let weatherStyle = this.setColors(data); return this.renderWithData(data, weatherStyle); } return this.renderNoData(); } } window.weatherTile = WeatherTile;