import React, { Component } from 'react';
import classnames from 'classnames';
import moment from 'moment';
class IconWithData extends Component {
render() {
const { props } = this;
return (
{props.text}
);
}
}
export default class WeatherTile extends Component {
constructor(props) {
super(props);
let ship = window.ship;
let api = window.api;
this.state = {
latlng: '',
manualEntry: false,
error: false
};
}
locationSubmit() {
navigator.geolocation.getCurrentPosition((res) => {
console.log(res);
let latlng = `${res.coords.latitude},${res.coords.longitude}`;
this.setState({
latlng
}, (err) => {
console.log(err);
}, { maximumAge: Infinity, timeout: 10000 });
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('weather', 'json', latlng);
this.setState({manualEntry: !this.state.manualEntry});
}
else {
this.setState({error: true});
return false;
}
}
keyPress(e) {
if (e.keyCode === 13) {
e.preventDefault();
this.manualLocationSubmit(e.target.value);
}
}
renderWrapper(child) {
return (
{child}
);
}
renderManualEntry() {
let secureCheck;
let error;
if (this.state.error === true) {
error =
Incorrect latitude/longitude formatting. Please try again.
(eg. "29.558107, -95.089023")
}
if (location.protocol === "https:") {
secureCheck = this.locationSubmit()}>Detect location ->
}
return this.renderWrapper((
))
}
renderNoData() {
return this.renderWrapper((
));
}
renderWithData(data) {
let c = data.currently;
let d = data.daily.data[0];
let da = moment.unix(d.sunsetTime).format('h:mm a') || '';
return this.renderWrapper((
));
}
render() {
let data = !!this.props.data ? this.props.data : {};
if (this.state.manualEntry === true) {
return this.renderManualEntry();
}
if ('currently' in data && 'daily' in data) {
return this.renderWithData(data);
}
return this.renderNoData();
}
}
window.weatherTile = WeatherTile;