/
JoinEventFormCountdownProvider.js
223 lines (186 loc) · 5.54 KB
/
JoinEventFormCountdownProvider.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
212
213
214
215
216
217
218
219
220
221
222
223
// @flow
import React, { Component, type Node, type ComponentType } from 'react';
import moment from 'moment-timezone';
import type { Dateish, Event, EventRegistration } from 'app/models';
import { registrationIsClosed } from '../utils';
type Action =
| 'REGISTRATION_AVAILABLE'
| '90_SECONDS_LEFT'
| '10_MINUTE_LEFT'
| 'STILL_WAITING'
| 'REGISTERED_OR_REGISTRATION_ALREADY_OPENED'
| 'REGISTRATION_NOT_AVAILABLE'
| 'RESET';
export type Props = {
event: Event,
registration: ?EventRegistration,
render?: (state: State) => Node
};
const COUNTDOWN_STARTS_WHEN_MINUTES_LEFT = 10;
// How often to check when to start the real countdown
const CHECK_COUNTDOWN_START_INTERVAL = 10000;
// How often to run the countdown, should be <= 1000
const COUNTDOWN_INTERVAL = 1000;
// Must be sorted lo->hi
const TICK_ACTIONS: Array<[number, Action]> = [
[0, 'REGISTRATION_AVAILABLE'],
[90 * 1000, '90_SECONDS_LEFT'],
[60 * 10000, '10_MINUTE_LEFT'],
[Infinity, 'STILL_WAITING']
];
type State = {
formOpen: boolean,
captchaOpen: boolean,
buttonOpen: boolean,
registrationOpensIn: ?string
};
const countdownReducer = (
state: State,
action: Action,
registrationOpensIn: ?string
): State => {
switch (action) {
case 'REGISTRATION_AVAILABLE':
return {
buttonOpen: true,
formOpen: true,
captchaOpen: true,
registrationOpensIn: null
};
case '90_SECONDS_LEFT':
return {
captchaOpen: true,
formOpen: true,
buttonOpen: false,
registrationOpensIn
};
case '10_MINUTE_LEFT':
return {
captchaOpen: false,
formOpen: true,
buttonOpen: false,
registrationOpensIn
};
case 'REGISTERED_OR_REGISTRATION_ALREADY_OPENED':
return {
formOpen: true,
captchaOpen: true,
buttonOpen: true,
registrationOpensIn: null
};
case 'REGISTRATION_NOT_AVAILABLE':
case 'RESET':
return {
formOpen: false,
captchaOpen: false,
buttonOpen: false,
registrationOpensIn: null
};
default:
return state;
}
};
function withCountdown(WrappedComponent: ComponentType<Props>) {
return class JoinEventFormCountdownProvider extends Component<Props, State> {
state = {
formOpen: false,
captchaOpen: false,
buttonOpen: false,
registrationOpensIn: null
};
countdownProbeTimer: IntervalID;
countdownTimer: IntervalID;
componentDidMount() {
this.setupEventCountdown(this.props.event, this.props.registration);
}
// eslint-disable-next-line
componentWillReceiveProps(nextProps: Props) {
if (
(nextProps.event.activationTime && !this.props.event.activationTime) ||
nextProps.registration !== this.props.registration
) {
this.dispatch('RESET');
this.setupEventCountdown(nextProps.event, nextProps.registration);
}
}
componentWillUnmount() {
clearInterval(this.countdownProbeTimer);
clearInterval(this.countdownTimer);
}
dispatch(action: Action, registrationOpensIn: ?string) {
this.setState(state =>
countdownReducer(state, action, registrationOpensIn)
);
}
setupEventCountdown = (event: Event, registration: ?EventRegistration) => {
const { activationTime } = event;
const poolActivationTime = moment(activationTime);
if ((!registration && !activationTime) || registrationIsClosed(event)) {
this.dispatch('REGISTRATION_NOT_AVAILABLE');
return;
}
if (registration || poolActivationTime.isBefore(moment())) {
this.dispatch('REGISTERED_OR_REGISTRATION_ALREADY_OPENED');
return;
}
const timeUntilRegistrationOpens = getTimeUntil(poolActivationTime);
if (
timeUntilRegistrationOpens.asMinutes() <=
COUNTDOWN_STARTS_WHEN_MINUTES_LEFT
) {
this.initiateCountdown(poolActivationTime);
return;
}
const poll = () => {
const timeUntilRegistrationOpens = getTimeUntil(poolActivationTime);
if (
timeUntilRegistrationOpens.asMinutes() <=
COUNTDOWN_STARTS_WHEN_MINUTES_LEFT
) {
clearInterval(this.countdownProbeTimer);
this.initiateCountdown(poolActivationTime);
}
};
poll();
this.countdownProbeTimer = setInterval(
poll,
CHECK_COUNTDOWN_START_INTERVAL
);
};
initiateCountdown(finishTime: Dateish) {
const poll = () => {
const timeUntilRegistrationOpens = getTimeUntil(
finishTime
).asMilliseconds();
if (timeUntilRegistrationOpens <= 0) {
clearInterval(this.countdownTimer);
}
const [, action] =
TICK_ACTIONS.find(
([time, action]) => timeUntilRegistrationOpens <= time
) || [];
const registrationOpensIn = moment(
timeUntilRegistrationOpens + 1000
).format('mm:ss');
if (action) {
this.dispatch(action, registrationOpensIn);
}
};
poll();
this.countdownTimer = setInterval(poll, COUNTDOWN_INTERVAL);
}
render() {
return <WrappedComponent {...this.props} {...this.state} />;
}
};
}
function getTimeDifference(first: moment, second: moment): number {
return moment(first).diff(moment(second));
}
export function getTimeUntil(
time: Dateish,
currentTime?: Dateish = moment()
): moment$MomentDuration {
return moment.duration(getTimeDifference(time, currentTime), 'milliseconds');
}
export default withCountdown;