-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCountdown.jsx
More file actions
executable file
·177 lines (150 loc) · 5.76 KB
/
Countdown.jsx
File metadata and controls
executable file
·177 lines (150 loc) · 5.76 KB
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
import React, { Component } from 'react';
import SatelliteIcon from '@material-ui/icons/Satellite';
function fetchPassovers(next, most_recent) {
var url = '/api/passovers';
if (next === true) {
url = url + '?next=true&limit=1';
}
if (most_recent === true) {
url = url + '&most-recent=true';
}
let passovers = new Promise((resolve, reject) => {
fetch(url)
.then(results => {
return results.json();
}).then(data => {
if(data.status == 'success'){
let nextPassover;
let mostRecentPassover;
if (data.data.next_passovers === undefined || data.data.next_passovers.length == 0) {
nextPassover = null;
} else {
let newNextDate_aos = data.data.next_passovers[0].aos_timestamp.replace(' ', 'T');
let newNextDate_los = data.data.next_passovers[0].los_timestamp.replace(' ', 'T');
nextPassover = {'aos': Date.parse(newNextDate_aos), 'los': Date.parse(newNextDate_los)};
}
if (data.data.most_recent_passover === undefined || data.data.most_recent_passover === null) {
mostRecentPassover = null;
} else {
let newMostRecentDate_aos = data.data.most_recent_passover.aos_timestamp.replace(' ', 'T');
let newMostRecentDate_los = data.data.most_recent_passover.los_timestamp.replace(' ', 'T');
mostRecentPassover = {'aos': Date.parse(newMostRecentDate_aos), 'los': Date.parse(newMostRecentDate_los)};
}
resolve({
nextPassover: nextPassover,
mostRecentPassover: mostRecentPassover
});
}
})
});
return passovers;
}
// inspired by https://www.cssscript.com/minimal-digital-clock-javascript-css/
class Countdown extends Component{
constructor(){
super();
this.state = {
hour: '00',
minute: '00',
second: '00',
displayCountdown:false,
nextPassover: null,
untilPassover: null,
operationIsAdd:false,
operatorChar:'-',
color:'#ffe21f'
}
this.updateCountdown = this.updateCountdown.bind(this);
this.drawCountdownAfterStateChange = this.drawCountdownAfterStateChange.bind(this);
}
componentDidMount() {
fetchPassovers(true, true).then(data => {
this.setState({
nextPassover: data.nextPassover,
mostRecentPassover: data.mostRecentPassover,
displayCountdown: true
}, this.updateCountdown);
});
this.timer = setInterval(
() => this.updateCountdown(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timer);
}
drawCountdownAfterStateChange(){
var utcToday;
let today = new Date(Date.now());
utcToday = new Date(today.getTime() + (today.getTimezoneOffset() * 60000));
let new_value = this.state.operationIsAdd ? utcToday - this.state.mostRecentPassover.aos : this.state.nextPassover.aos - utcToday;
let hour = Math.floor((new_value / (1000 * 60 * 60)) % 24);
let minute = Math.floor((new_value / (1000 * 60)) % 60);
let second = Math.floor((new_value / 1000) % 60)
hour = (hour < 10)? "0" + hour : hour;
minute = (minute < 10)? "0" + minute : minute;
second = (second < 10)? "0" + second: second;
this.setState({
hour: hour,
minute: minute,
second: second,
})
}
updateCountdown(calls_remaining){
var utcToday;
let today = new Date(Date.now());
utcToday = new Date(today.getTime() + (today.getTimezoneOffset() * 60000));
var breakout = true;
var mostRecentPassover = this.state.mostRecentPassover;
var nextPassover = this.state.nextPassover;
if (this.state.mostRecentPassover === null || this.state.nextPassover === null) {
this.setState({displayCountdown:false});
clearInterval(this.timer);
return
}
var timeDifferenceWithMostRecent = utcToday - this.state.mostRecentPassover.los;
var timeDifferenceWithNext = this.state.nextPassover.aos - utcToday;
if (timeDifferenceWithMostRecent <= 0) {
this.setState(
{operationIsAdd:true, operatorChar:'+', color:"#2ecc40"},
this.drawCountdownAfterStateChange
)
} else if (timeDifferenceWithNext > 0) {
this.setState(
{operationIsAdd:false, operatorChar:'-', color:"#ffe21f"},
this.drawCountdownAfterStateChange,
)
} else if (timeDifferenceWithNext <= 0) {
this.setState(prevState => ({
mostRecentPassover: prevState.nextPassover,
nextPassover:null,
operationIsAdd:true,
operatorChar:'+',
color:"#2ecc40"
}), this.drawCountdownAfterStateChange);
fetchPassovers(true, false).then(data => {
this.setState({
nextPassover: data.nextPassover
});
});
}
}
render(){
if (!this.state.displayCountdown) {
return (
<div></div>
)
}
return (
<span style={{marginLeft: '1.5em', display: 'inherit'}}>
<span style={{marginRight: '0.5em', color:this.state.color}}>
<SatelliteIcon style={{fontSize: '1.85em'}}/>
</span>
<span style={{color:this.state.color, fontWeight: 'bold', fontSize: '1.25em'}}>
T{this.state.operatorChar} {this.state.hour}:{this.state.minute}:{this.state.second}
</span>
</span>
)
}
}
export default Countdown;