-
Notifications
You must be signed in to change notification settings - Fork 295
/
CountdownWidget.tsx
192 lines (171 loc) · 5.15 KB
/
CountdownWidget.tsx
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
// @flow
import React, { Component, Fragment } from 'react';
import { observer } from 'mobx-react';
import { intlShape } from 'react-intl';
import moment from 'moment';
import SVGInline from 'react-svg-inline';
import styles from './CountdownWidget.scss';
import delimeterIcon from '../../assets/images/delimeter.inline.svg';
import spinnerIcon from '../../assets/images/spinner.inline.svg';
import globalMessages from '../../i18n/global-messages';
const TIME_LEFT_INTERVAL = 1 * 1000; // 1 second | unit: milliseconds;
const COLUMNS = {
YYYY: 'years',
MM: 'months',
DD: 'days',
HH: 'hours',
mm: 'minutes',
ss: 'seconds',
};
type Format =
| 'YYYY-MM-DD-HH-mm-ss'
| 'MM-DD-HH-mm-ss'
| 'DD-HH-mm-ss'
| 'HH-mm-ss'
| 'mm-ss'
| 'YYYY'
| 'MM'
| 'DD'
| 'HH'
| 'mm'
| 'ss';
type Props = {
startDateTime: ?string,
redirectOnEnd?: Function,
format?: Format, // If no format is specified only non-empty columns will be shown
};
type State = { timeLeft: ?number };
@observer
export default class CountdownWidget extends Component<Props, State> {
intervalHandler: ?IntervalID = null;
state = { timeLeft: null };
static contextTypes = {
intl: intlShape.isRequired,
};
componentDidMount() {
this.startTimer();
}
componentWillUnmount() {
this.stopTimer();
}
startTimer = () => {
this.updateTimeLeft();
this.intervalHandler = setInterval(
() => this.updateTimeLeft(),
TIME_LEFT_INTERVAL
);
};
stopTimer = () => {
if (this.intervalHandler) {
clearInterval(this.intervalHandler);
this.intervalHandler = null;
}
};
updateTimeLeft = () => {
const { redirectOnEnd, startDateTime } = this.props;
if (startDateTime) {
const timeLeft = Math.max(
0,
new Date(startDateTime).getTime() - new Date().getTime()
);
this.setState({ timeLeft });
if (timeLeft === 0 && redirectOnEnd) {
redirectOnEnd();
}
}
};
generateFieldPanel = (labels: any, values: any, index: number) => {
const value = values[index];
const includeDelimeter = index !== values.length - 1;
const labelStr = labels[index];
const { format } = this.props;
const shouldBeHidden =
values.slice(0, index).reduce((acc, val) => acc + val, 0) === 0 &&
value === 0 &&
!format;
if (shouldBeHidden) {
return null;
}
const valueStr = value.toString();
const zeroValue = valueStr.length === 1 ? '0' : '';
const isZeroValue =
valueStr === '0' &&
values.slice(0, index).reduce((acc, val) => acc + val, 0) === 0;
return (
<div className={styles.fieldPanel}>
<div className={styles.left}>
<div className={styles.fieldLabel}>{labelStr}</div>
<div className={styles.fieldValue}>
{(isZeroValue || zeroValue) && <span>{zeroValue}</span>}
{valueStr}
</div>
</div>
{includeDelimeter && (
<div className={styles.right}>
<SVGInline svg={delimeterIcon} className={styles.delimeterIcon} />
</div>
)}
</div>
);
};
generateCountdownPanels = () => {
const { intl } = this.context;
const { timeLeft } = this.state;
const { format } = this.props;
const duration = moment.duration(timeLeft, 'milliseconds');
const yearsLabel = intl.formatMessage(globalMessages.years);
const monthsLabel = intl.formatMessage(globalMessages.months);
const daysLabel = intl.formatMessage(globalMessages.days);
const hoursLabel = intl.formatMessage(globalMessages.hours);
const minutesLabel = intl.formatMessage(globalMessages.minutes);
const secondsLabel = intl.formatMessage(globalMessages.seconds);
const labels: Array<string> = format
? format
.split('-')
.map((item) => intl.formatMessage(globalMessages[COLUMNS[item]]))
: [
yearsLabel,
monthsLabel,
daysLabel,
hoursLabel,
minutesLabel,
secondsLabel,
];
const years = duration.years();
const months = duration.months();
const days = duration.days();
const hours = duration.hours();
const minutes = duration.minutes();
const seconds = duration.seconds();
const values = format
? format.split('-').map((item) => duration[COLUMNS[item]]())
: [years, months, days, hours, minutes, seconds];
const keys = format
? format.split('-').map((item) => COLUMNS[item])
: ['years', 'months', 'days', 'hours', 'minutes', 'seconds'];
return labels.map<any>((
label: string, // eslint-disable-line
index: number
) => (
<Fragment key={keys[index]}>
{this.generateFieldPanel(labels, values, index)}
</Fragment>
));
};
render() {
const { startDateTime } = this.props;
const fieldPanels = this.generateCountdownPanels();
const showSpinner = startDateTime === null;
return (
<div className={styles.timeLeftContainer}>
<div className={styles.timeLeft}>
{showSpinner ? (
<SVGInline svg={spinnerIcon} className={styles.spinnerIcon} />
) : (
fieldPanels
)}
</div>
</div>
);
}
}