2019-05-28 23:32:52 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
class IconWithData extends Component {
|
|
|
|
render() {
|
|
|
|
const { props } = this;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='mt2'>
|
2020-02-06 02:16:56 +03:00
|
|
|
<p className="f9 white">{props.text}</p>
|
2019-05-28 23:32:52 +03:00
|
|
|
</div>
|
2020-01-04 00:06:42 +03:00
|
|
|
);
|
2019-05-28 23:32:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class WeatherTile extends Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
let ship = window.ship;
|
|
|
|
let api = window.api;
|
|
|
|
|
|
|
|
this.state = {
|
2019-08-13 02:42:41 +03:00
|
|
|
latlng: '',
|
2019-08-13 02:55:28 +03:00
|
|
|
manualEntry: false,
|
|
|
|
error: false
|
2019-05-28 23:32:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
locationSubmit() {
|
2019-06-05 01:12:13 +03:00
|
|
|
navigator.geolocation.getCurrentPosition((res) => {
|
|
|
|
let latlng = `${res.coords.latitude},${res.coords.longitude}`;
|
|
|
|
this.setState({
|
|
|
|
latlng
|
2019-06-28 21:38:14 +03:00
|
|
|
}, (err) => {
|
|
|
|
console.log(err);
|
|
|
|
}, { maximumAge: Infinity, timeout: 10000 });
|
2020-02-06 05:08:22 +03:00
|
|
|
api.action("clock", "json", latlng);
|
2019-06-05 01:12:13 +03:00
|
|
|
api.action('weather', 'json', latlng);
|
|
|
|
});
|
2019-05-28 23:32:52 +03:00
|
|
|
}
|
2019-08-13 02:42:41 +03:00
|
|
|
|
|
|
|
manualLocationSubmit() {
|
2019-08-14 03:17:40 +03:00
|
|
|
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;
|
2019-08-13 02:42:41 +03:00
|
|
|
if (latlngParse.test(latlngNoSpace)) {
|
2019-08-14 03:17:40 +03:00
|
|
|
let latlng = latlngNoSpace;
|
|
|
|
this.setState({latlng}, (err) => {console.log(err)}, {maximumAge: Infinity, timeout: 10000});
|
2020-02-06 05:08:22 +03:00
|
|
|
api.action("clock", "json", latlng);
|
2019-08-14 03:17:40 +03:00
|
|
|
api.action('weather', 'json', latlng);
|
|
|
|
this.setState({manualEntry: !this.state.manualEntry});
|
2019-08-13 02:42:41 +03:00
|
|
|
}
|
2019-08-13 02:55:28 +03:00
|
|
|
else {
|
2019-08-14 03:17:40 +03:00
|
|
|
this.setState({error: true});
|
|
|
|
return false;
|
2019-08-13 02:55:28 +03:00
|
|
|
}
|
2019-08-13 02:42:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
keyPress(e) {
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.manualLocationSubmit(e.target.value);
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 23:32:52 +03:00
|
|
|
|
|
|
|
renderWrapper(child) {
|
|
|
|
return (
|
2020-02-06 02:16:56 +03:00
|
|
|
<div
|
|
|
|
className="pa2 relative bg-white b--black ba"
|
|
|
|
style={{
|
|
|
|
width: 126,
|
|
|
|
height: 126,
|
|
|
|
}}>
|
2019-05-28 23:32:52 +03:00
|
|
|
{child}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-13 02:42:41 +03:00
|
|
|
renderManualEntry() {
|
2019-08-14 03:37:02 +03:00
|
|
|
let secureCheck;
|
2019-08-13 02:55:28 +03:00
|
|
|
let error;
|
|
|
|
if (this.state.error === true) {
|
2020-01-04 00:06:42 +03:00
|
|
|
error = <p
|
2020-02-06 02:16:56 +03:00
|
|
|
className="f9 red2 pt1">
|
2019-08-14 03:37:02 +03:00
|
|
|
Incorrect latitude/longitude formatting. Please try again. <br/>
|
|
|
|
(eg. "29.558107, -95.089023")
|
2019-08-14 03:17:40 +03:00
|
|
|
</p>
|
2019-08-13 02:55:28 +03:00
|
|
|
}
|
2019-08-14 03:37:02 +03:00
|
|
|
if (location.protocol === "https:") {
|
|
|
|
secureCheck = <a
|
2020-02-06 02:16:56 +03:00
|
|
|
className="black f9 absolute pointer"
|
|
|
|
style={{right: 8, top: 8}}
|
|
|
|
onClick={() => this.locationSubmit()}>Detect -></a>
|
2019-08-14 03:37:02 +03:00
|
|
|
}
|
2020-02-06 02:16:56 +03:00
|
|
|
return this.renderWrapper(
|
2019-08-13 02:42:41 +03:00
|
|
|
<div>
|
2020-02-06 02:16:56 +03:00
|
|
|
<a
|
|
|
|
className="f9 black pointer"
|
|
|
|
onClick={() =>
|
|
|
|
this.setState({ manualEntry: !this.state.manualEntry })
|
|
|
|
}>
|
|
|
|
<-
|
2019-08-14 03:17:40 +03:00
|
|
|
</a>
|
2019-08-14 03:37:02 +03:00
|
|
|
{secureCheck}
|
2020-02-06 02:16:56 +03:00
|
|
|
<p className="f9 black pt2">
|
|
|
|
Please enter your{" "}
|
|
|
|
<a
|
|
|
|
className="white"
|
|
|
|
href="https://latitudeandlongitude.org/"
|
|
|
|
target="_blank">
|
|
|
|
latitude and longitude
|
|
|
|
</a>
|
|
|
|
.
|
|
|
|
</p>
|
2019-08-13 02:55:28 +03:00
|
|
|
{error}
|
2020-02-06 02:16:56 +03:00
|
|
|
<div className="flex">
|
|
|
|
<form className="flex absolute" style={{ bottom: 0, left: 8 }}>
|
|
|
|
<input
|
|
|
|
id="gps"
|
|
|
|
className="w-100 black pa1 bg-transparent outline-0 bn bb-ns b--black f9"
|
|
|
|
type="text"
|
|
|
|
placeholder="29.558107, -95.089023"
|
|
|
|
onKeyDown={this.keyPress.bind(this)}></input>
|
|
|
|
<input
|
|
|
|
className="bg-transparent black outliner-0 bn pointer f9 flex-shrink-0"
|
|
|
|
type="submit"
|
|
|
|
onClick={() => this.manualLocationSubmit()}
|
|
|
|
value="->"></input>
|
|
|
|
</form>
|
|
|
|
</div>
|
2019-08-13 02:42:41 +03:00
|
|
|
</div>
|
2020-02-06 02:16:56 +03:00
|
|
|
);
|
2019-08-13 02:42:41 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 23:32:52 +03:00
|
|
|
renderNoData() {
|
|
|
|
return this.renderWrapper((
|
2019-08-14 22:14:28 +03:00
|
|
|
<div onClick={() => this.setState({manualEntry: !this.state.manualEntry})}>
|
2020-02-06 02:16:56 +03:00
|
|
|
<p className="black f9 absolute"
|
|
|
|
style={{left: 8, top: 8}}>
|
2019-08-01 20:31:47 +03:00
|
|
|
Weather
|
|
|
|
</p>
|
2020-02-06 02:16:56 +03:00
|
|
|
<p className="absolute w-100 flex-col f9 black" style={{verticalAlign: "bottom", bottom: 8, left: 8, cursor: "pointer"}}>-> Set location</p>
|
2019-05-28 23:32:52 +03:00
|
|
|
</div>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
renderWithData(data) {
|
|
|
|
let c = data.currently;
|
|
|
|
let d = data.daily.data[0];
|
|
|
|
|
|
|
|
let da = moment.unix(d.sunsetTime).format('h:mm a') || '';
|
|
|
|
|
2020-02-06 02:16:56 +03:00
|
|
|
return this.renderWrapper(
|
2019-05-28 23:32:52 +03:00
|
|
|
<div>
|
2020-02-06 02:16:56 +03:00
|
|
|
<p className="black f9 absolute" style={{ left: 8, top: 8 }}>
|
|
|
|
Weather
|
|
|
|
</p>
|
|
|
|
<a
|
|
|
|
className="black f9 absolute pointer"
|
|
|
|
style={{ right: 8, top: 8 }}
|
|
|
|
onClick={() =>
|
|
|
|
this.setState({ manualEntry: !this.state.manualEntry })
|
|
|
|
}>
|
|
|
|
->
|
|
|
|
</a>
|
|
|
|
<div className="w-100 absolute" style={{ left: 8, bottom: 8 }}>
|
|
|
|
<p className="f9 black">{c.summary}</p>
|
|
|
|
<p className="f9 pt1 black">{Math.round(c.temperature)}°</p>
|
|
|
|
<p className="f9 pt1 black">Sunset at {da}</p>
|
2019-05-28 23:32:52 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-02-06 02:16:56 +03:00
|
|
|
);
|
2019-05-28 23:32:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let data = !!this.props.data ? this.props.data : {};
|
|
|
|
|
2019-08-13 02:42:41 +03:00
|
|
|
if (this.state.manualEntry === true) {
|
|
|
|
return this.renderManualEntry();
|
|
|
|
}
|
|
|
|
|
2019-05-28 23:32:52 +03:00
|
|
|
if ('currently' in data && 'daily' in data) {
|
|
|
|
return this.renderWithData(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.renderNoData();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
window.weatherTile = WeatherTile;
|