-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
211 lines (179 loc) · 4.66 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React, { Component, PropTypes } from 'react';
import classnames from 'classnames';
import TickTock from 'tick-tock';
/**
* Hide contents until the count down has passed.
*
* @constructor
* @public
*/
export default class Countdown extends Component {
constructor(props) {
super(...arguments);
this.timers = new TickTock();
this.tick = this.tick.bind(this);
this.state = this.diff(props.date);
}
/**
* Start the interval.
*
* @private
*/
componentDidMount() {
this.timers.setInterval('update', this.tick, 1000);
}
/**
* Stop the interval.
*
* @private
*/
componentWillUnmount() {
this.timers.clear('update');
}
/**
* Update the component when we receive a new date.
*
* @param {Object} next The new props.
* @private
*/
componentWillReceiveProps(next) {
if (this.props.date === next.date) return;
this.timers.clear('update');
this.timers.setInterval('update', this.tick, 1000);
this.setState(this.diff(next.date));
}
/**
* Update the state.
*
* @private
*/
tick() {
const next = this.diff(this.props.date);
//
// Stop updating if the timer has passed and we don't want to show negative
// countdowns.
//
if (next.passed) {
this.timers.clear('update');
//
// This is the first time that we've set the state as passed so we can
// call the completion callback if specified.
//
if (!this.state.passed && this.props.passed) {
this.props.passed(next);
}
}
this.setState(next);
}
/**
* Calculate the difference between now and the supplied date.
*
* @param {String|Date} date The new date.
* @returns {Object} Formatted difference between dates.
* @private
*/
diff(date) {
date = new Date(date);
const now = Date.now();
const diff = date - now;
const days = parseInt(diff / (24 * 3600 * 1000));
const hours = parseInt(diff / (3600 * 1000) - (days * 24));
const mins = parseInt(diff / (60 * 1000) - (days * 24 * 60) - (hours * 60));
const secs = parseInt(diff / (1000) - (mins * 60) - (days * 24 * 60 * 60) - (hours * 60 * 60));
const finalcountdown = days === 0 && hours === 0 && mins === 0 && secs <= 10;
return {
passed: now > +date,
final: finalcountdown,
days: days,
hours: hours,
mins: mins,
secs: secs
};
}
/**
* Apply leading zero to the supplied value.
*
* @param {Number|String} value Thing that needs to have a 0 prepended.
* @returns {String} The new value
* @private
*/
zero(value) {
value = value.toString();
if (value.length >= 2) return value;
return '0' + value;
}
/**
* Render the countdown timer.
*
* @private
*/
render() {
const { passed, days, hours, mins, secs, final } = this.state;
//
// We haven't received a valid date yet, we're going to assume that we are
// waiting to receive a working state from an async update. Until then we're
// not going to display anything.
//
if (isNaN(days)) return null;
//
// Time has passed, show the children.
//
if (passed) return (
<div className='countdown inactive'>
{ this.props.children }
</div>
);
const finalcountdown = classnames('secs', {
finalcountdown: final
});
return (
<div className='countdown active'>
{ this.props.prefix }
<Conditional value={ days }>
<div className='days' key='days'>
{ days }
<span>{ days == 1 ? 'Day' : 'Days'}</span>
</div>
<div className='sep' key='sep'>:</div>
</Conditional>
<Conditional value={ hours }>
<div className='hours'>
{ this.zero(hours) }
<span>Hours</span>
</div>
<div className='sep'>:</div>
</Conditional>
<Conditional value={ mins }>
<div className='mins'>
{ this.zero(mins) }
<span>Minutes</span>
</div>
<div className='sep'>:</div>
</Conditional>
<Conditional value={ secs } negative>
<div className={ finalcountdown }>
{ this.zero(secs) }
<span>Seconds</span>
</div>
</Conditional>
</div>
);
}
}
/**
* Simple helper component which will make some conditional statements easier to
* read.
*
* @param {Object} props Properties of the component.
* @returns {Component}
* @private
*/
function Conditional(props) {
const allowed = props.negative ? props.value >= 0 : props.value > 0;
if (allowed) return (
<div>
{ props.children }
</div>
);
return null;
}