@@ -20,6 +20,9 @@ const DRAFT_MSG = 'In Draft';
20
20
const STALLED_TIME_LEFT_MSG = 'Challenge is currently on hold' ;
21
21
const FF_TIME_LEFT_MSG = 'Winner is working on fixes' ;
22
22
23
+ const HOUR_MS = 60 * 60 * 1000 ;
24
+ const DAY_MS = 24 * HOUR_MS ;
25
+
23
26
const getTimeLeft = ( date , currentPhase ) => {
24
27
if ( ! currentPhase || currentPhase === 'Stalled' ) {
25
28
return {
@@ -33,27 +36,18 @@ const getTimeLeft = (date, currentPhase) => {
33
36
} ;
34
37
}
35
38
36
- const duration = moment . duration ( moment ( date ) . diff ( moment ( ) ) ) ;
37
- const h = duration . hours ( ) ;
38
- const d = duration . asDays ( ) ;
39
- const m = duration . minutes ( ) ;
40
- const late = ( d < 0 || h < 0 || m < 0 ) ;
41
- const suffix = h !== 0 ? 'h' : 'min' ;
42
- let text = '' ;
43
- if ( d >= 1 ) text += `${ Math . abs ( parseInt ( d , 10 ) ) } d ` ;
44
- if ( h !== 0 ) text += `${ Math . abs ( h ) } ` ;
45
- if ( h !== 0 && m !== 0 ) text += ':' ;
46
- if ( m !== 0 ) text += `${ Math . abs ( m ) } ` ;
47
- text += suffix ;
48
- if ( late ) {
49
- text = `Late by ${ text } ` ;
50
- } else {
51
- text = `${ text } to go` ;
52
- }
53
- return {
54
- late,
55
- text,
56
- } ;
39
+ let time = moment ( date ) . diff ( ) ;
40
+ const late = time < 0 ;
41
+ if ( late ) time = - time ;
42
+
43
+ let format ;
44
+ if ( time > DAY_MS ) format = 'DDD[d] H[h]' ;
45
+ else if ( time > HOUR_MS ) format = 'H[h] m[min]' ;
46
+ else format = 'm[min] s[s]' ;
47
+
48
+ time = moment ( time ) . format ( format ) ;
49
+ time = late ? `Late by ${ time } ` : `${ time } to go` ;
50
+ return { late, text : time } ;
57
51
} ;
58
52
59
53
function numRegistrantsTipText ( number ) {
@@ -313,10 +307,18 @@ class ChallengeStatus extends Component {
313
307
detailLink,
314
308
openChallengesInNewTabs,
315
309
} = this . props ;
316
- const lng = getTimeLeft (
310
+ const timeDiff = getTimeLeft (
317
311
challenge . registrationEndDate || challenge . submissionEndDate ,
318
312
challenge . currentPhases [ 0 ] ? challenge . currentPhases [ 0 ] . phaseType : '' ,
319
- ) . text . length ;
313
+ ) ;
314
+ let timeNote = timeDiff . text ;
315
+ /* TODO: This is goofy, makes the trick, but should be improved. The idea
316
+ * here is that the standard "getTimeLeft" method, for positive times,
317
+ * generates a string like "H MM to go"; here we want to render just
318
+ * H MM part, so we cut the last 6 symbols. Not a good code. */
319
+ if ( ! timeDiff . late ) {
320
+ timeNote = timeNote . substring ( 0 , timeNote . length - 6 ) ;
321
+ }
320
322
return (
321
323
< a
322
324
href = { detailLink }
@@ -325,12 +327,7 @@ class ChallengeStatus extends Component {
325
327
target = { openChallengesInNewTabs ? '_blank' : undefined }
326
328
>
327
329
< span >
328
- {
329
- getTimeLeft (
330
- challenge . registrationEndDate || challenge . submissionEndDate ,
331
- challenge . currentPhases [ 0 ] ? challenge . currentPhases [ 0 ] . phaseType : '' ,
332
- ) . text . substring ( 0 , lng - 6 )
333
- }
330
+ { timeNote }
334
331
</ span >
335
332
< span styleName = "to-register" > to register</ span >
336
333
</ a >
0 commit comments