weather: colors change based on weather

This commit is contained in:
Matilde Park 2020-02-18 21:22:19 -05:00
parent 5b1c3f3cca
commit 069df98edd
2 changed files with 119 additions and 39 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,31 +2,15 @@ import React, { Component } from 'react';
import classnames from 'classnames'; import classnames from 'classnames';
import moment from 'moment'; import moment from 'moment';
class IconWithData extends Component {
render() {
const { props } = this;
return (
<div className='mt2'>
<p className="f9 white">{props.text}</p>
</div>
);
}
}
export default class WeatherTile extends Component { export default class WeatherTile extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
let ship = window.ship;
let api = window.api;
this.state = { this.state = {
latlng: '', latlng: '',
manualEntry: false, manualEntry: false,
error: false error: false
}; };
let api = window.api;
} }
locationSubmit() { locationSubmit() {
@ -62,18 +46,22 @@ export default class WeatherTile extends Component {
keyPress(e) { keyPress(e) {
if (e.keyCode === 13) { if (e.keyCode === 13) {
e.preventDefault();
this.manualLocationSubmit(e.target.value);
} }
} }
renderWrapper(child) { renderWrapper(child,
weatherStyle = { gradient1: "white", gradient2: "white", text: "black" }
) {
return ( return (
<div <div
className="pa2 relative bg-white b--black ba" className={"relative b--black ba " + weatherStyle.text}
style={{ style={{
width: 126, width: 126,
height: 126, height: 126,
background: `linear-gradient(135deg, ${weatherStyle.gradient1} 0%,` +
`${weatherStyle.gradient2} 45%, ${weatherStyle.gradient2} 65%,` +
`${weatherStyle.gradient1} 100%)`
}}> }}>
{child} {child}
</div> </div>
@ -95,7 +83,7 @@ export default class WeatherTile extends Component {
onClick={() => this.locationSubmit()}>Detect -></a> onClick={() => this.locationSubmit()}>Detect -></a>
} }
return this.renderWrapper( return this.renderWrapper(
<div> <div className="pa2 w-100 h-100 bg-white black">
<a <a
className="f9 black pointer" className="f9 black pointer"
onClick={() => onClick={() =>
@ -104,10 +92,10 @@ export default class WeatherTile extends Component {
&lt;&#45; &lt;&#45;
</a> </a>
{secureCheck} {secureCheck}
<p className="f9 black pt2"> <p className="f9 pt2">
Please enter your{" "} Please enter your{" "}
<a <a
className="white" className="black"
href="https://latitudeandlongitude.org/" href="https://latitudeandlongitude.org/"
target="_blank"> target="_blank">
latitude and longitude latitude and longitude
@ -122,12 +110,17 @@ export default class WeatherTile extends Component {
className="w-100 black bg-transparent bn f9" className="w-100 black bg-transparent bn f9"
type="text" type="text"
placeholder="29.558107, -95.089023" placeholder="29.558107, -95.089023"
onKeyDown={this.keyPress.bind(this)}></input> onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
this.manualLocationSubmit(e.target.value);
}}
}/>
<input <input
className="bg-transparent black bn pointer f9 flex-shrink-0" className="bg-transparent black bn pointer f9 flex-shrink-0"
type="submit" type="submit"
onClick={() => this.manualLocationSubmit()} onClick={() => this.manualLocationSubmit()}
value="->"></input> value="->"/>
</form> </form>
</div> </div>
</div> </div>
@ -136,29 +129,32 @@ export default class WeatherTile extends Component {
renderNoData() { renderNoData() {
return this.renderWrapper(( return this.renderWrapper((
<div onClick={() => this.setState({manualEntry: !this.state.manualEntry})}> <div
<p className="black f9 absolute" className="pa2 bg-white black"
onClick={() => this.setState({manualEntry: !this.state.manualEntry})}>
<p className="f9 absolute"
style={{left: 8, top: 8}}> style={{left: 8, top: 8}}>
Weather Weather
</p> </p>
<p className="absolute w-100 flex-col f9 black" style={{verticalAlign: "bottom", bottom: 8, left: 8, cursor: "pointer"}}>-> Set location</p> <p className="absolute w-100 flex-col f9" style={{verticalAlign: "bottom", bottom: 8, left: 8, cursor: "pointer"}}>-> Set location</p>
</div> </div>
)); ));
} }
renderWithData(data) { renderWithData(data, weatherClasses) {
let c = data.currently; let c = data.currently;
let d = data.daily.data[0]; let d = data.daily.data[0];
let da = moment.unix(d.sunsetTime).format('h:mm a') || ''; let da = moment.unix(d.sunsetTime).format('h:mm a') || '';
return this.renderWrapper( return this.renderWrapper(
<div> <div className="w-100 h-100"
<p className="black f9 absolute" style={{ left: 8, top: 8 }}> style={{backdropFilter: "blur(35px)"}}>
<p className="f9 absolute" style={{ left: 8, top: 8 }}>
Weather Weather
</p> </p>
<a <a
className="black f9 absolute pointer" className="f9 absolute pointer"
style={{ right: 8, top: 8 }} style={{ right: 8, top: 8 }}
onClick={() => onClick={() =>
this.setState({ manualEntry: !this.state.manualEntry }) this.setState({ manualEntry: !this.state.manualEntry })
@ -166,12 +162,12 @@ export default class WeatherTile extends Component {
-> ->
</a> </a>
<div className="w-100 absolute" style={{ left: 8, bottom: 8 }}> <div className="w-100 absolute" style={{ left: 8, bottom: 8 }}>
<p className="f9 black">{c.summary}</p> <p className="f9">{c.summary}</p>
<p className="f9 pt1 black">{Math.round(c.temperature)}°</p> <p className="f9 pt1">{Math.round(c.temperature)}°</p>
<p className="f9 pt1 black">Sunset at {da}</p> <p className="f9 pt1">Sunset at {da}</p>
</div> </div>
</div> </div>
); , weatherClasses);
} }
render() { render() {
@ -182,7 +178,91 @@ export default class WeatherTile extends Component {
} }
if ('currently' in data && 'daily' in data) { if ('currently' in data && 'daily' in data) {
return this.renderWithData(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 this.renderWithData(data, weatherStyle);
} }
return this.renderNoData(); return this.renderNoData();