/
component.js
102 lines (82 loc) · 2.47 KB
/
component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React, { Component, PropTypes, createClass } from 'react';
import { findDOMNode } from 'react-dom';
import { get } from 'axios';
import { camelizeKeys } from 'humps';
import moment from 'moment';
/**
* @method propTypes
* @type {Object}
*/
const propTypes = {
unit: PropTypes.string.isRequired
};
/**
* @method getInitialState
* @return {Object}
*/
const getInitialState = function getInitialState() {
return { weather: {} };
};
/**
* @method getDefaultProps
* @return {Object}
*/
const getDefaultProps = function getDefaultProps() {
return { unit: 'F' };
};
/**
* @method componentWillMount
* @return {void}
*/
const componentWillMount = function componentWillMount() {
get('/weather').then(response => {
this.setState({ weather: camelizeKeys(response.data).report });
});
};
/**
* @method render
* @return {XML}
*/
const render = function render() {
const { weather } = this.state;
const unitProps = this.props.unit === 'F' ? ['minTempFahrenheit', 'maxTempFahrenheit'] : ['minTemp', 'maxTemp'];
const temperature = (weather[unitProps[0]] + weather[unitProps[1]]) / 2;
/**
* @method sendChangeEvent
* @param {String} unit
* @return {void}
*/
const sendChangeEvent = unit => {
findDOMNode(this).dispatchEvent(new CustomEvent('unitChanged', {
bubbles: true,
detail: { unit }
}));
};
return (
<section>
<ul className="units">
<li>Temperature:</li>
<li>
<a onClick={() => sendChangeEvent('C')}>
Celsius
</a>
</li>
<li>
<a onClick={() => sendChangeEvent('F')}>
Fahrenheit
</a>
</li>
</ul>
<var>{temperature ? Number(temperature).toFixed(1) : String.fromCharCode(8212)}°{this.props.unit}</var>
<label>is the current weather on Mars</label>
{weather.atmoOpacity && (
<ul className="other">
<li>Weather: {weather.atmoOpacity}</li>
<li>Sunrise: {moment(weather.sunrise).format('LT')}</li>
<li>Sunset: {moment(weather.sunset).format('LT')}</li>
</ul>
)}
</section>
);
};
export default createClass({ propTypes, getInitialState, getDefaultProps, componentWillMount, render });