4
4
5
5
import moment from 'moment' ;
6
6
import { find , sumBy } from 'lodash' ;
7
+ import { phaseStartDate , phaseEndDate } from '../challenge-detail/helper' ;
7
8
8
9
export const SORTS = {
9
10
CURRENT_PHASE : 'current-phase' ,
@@ -27,12 +28,14 @@ export default {
27
28
[ SORTS . MOST_RECENT ] : {
28
29
func : ( a , b ) => {
29
30
const getRegistrationStartDate = ( challenge ) => {
31
+ // extract the registration phase from `challenge.phases`,
32
+ // as `challenge.registrationStartDate` returned from API is not reliable
30
33
const registrationPhase = find ( challenge . phases , p => p . name === 'Registration' ) ;
31
- return registrationPhase . actualStartDate || registrationPhase . scheduledStartDate ;
34
+ return moment ( phaseStartDate ( registrationPhase ) ) ;
32
35
} ;
33
36
const aRegistrationStartDate = getRegistrationStartDate ( a ) ;
34
37
const bRegistrationStartDate = getRegistrationStartDate ( b ) ;
35
- return moment ( bRegistrationStartDate ) . diff ( aRegistrationStartDate ) ;
38
+ return bRegistrationStartDate . diff ( aRegistrationStartDate ) ;
36
39
} ,
37
40
name : 'Most recent' ,
38
41
} ,
@@ -51,12 +54,20 @@ export default {
51
54
[ SORTS . TIME_TO_REGISTER ] : {
52
55
func : ( a , b ) => {
53
56
const getRegistrationEndDate = ( challenge ) => {
57
+ // extract the registration phase from `challenge.phases`,
58
+ // as `challenge.registrationEndDate` returned from API is not reliable
54
59
const registrationPhase = find ( challenge . phases , p => p . name === 'Registration' ) ;
55
- return registrationPhase . actualEndDate || registrationPhase . scheduledEndDate ;
60
+ const submissionPhase = find ( challenge . phases , p => p . name === 'Submission' ) ;
61
+ // case 1: registration phase exists
62
+ if ( registrationPhase ) {
63
+ return moment ( phaseEndDate ( registrationPhase ) ) ;
64
+ }
65
+ // case 2: registration phase doesn't exist. Take submission phase instead.
66
+ return moment ( phaseEndDate ( submissionPhase ) ) ;
56
67
} ;
57
68
58
- const aDate = moment ( getRegistrationEndDate ( a ) || a . submissionEndTimestamp ) ;
59
- const bDate = moment ( getRegistrationEndDate ( b ) || b . submissionEndTimestamp ) ;
69
+ const aDate = getRegistrationEndDate ( a ) ;
70
+ const bDate = getRegistrationEndDate ( b ) ;
60
71
61
72
if ( aDate . isBefore ( ) && bDate . isAfter ( ) ) return 1 ;
62
73
if ( aDate . isAfter ( ) && bDate . isBefore ( ) ) return - 1 ;
@@ -68,11 +79,23 @@ export default {
68
79
} ,
69
80
[ SORTS . TIME_TO_SUBMIT ] : {
70
81
func : ( a , b ) => {
71
- function nextSubEndDate ( o ) {
72
- if ( o . checkpointSubmissionEndDate && moment ( o . checkpointSubmissionEndDate ) . isAfter ( ) ) {
73
- return moment ( o . checkpointSubmissionEndDate ) ;
82
+ function nextSubEndDate ( challenge ) {
83
+ // extract the submission and checkpoint (if any) phases from `challenge.phases`,
84
+ // as `challenge.submissionEndDate` returned from API is not reliable
85
+ const checkpointPhase = find ( challenge . phases , p => p . name === 'Checkpoint Submission' ) ;
86
+ const submissionPhase = find ( challenge . phases , p => p . name === 'Submission' ) ;
87
+ // Case 1: challenge has checkpoint submission phase
88
+ if ( ! ! checkpointPhase === true ) {
89
+ // Case 1.1: checkpoint submission phase is still open.
90
+ // then take the `scheduledEndDate` of this phase.
91
+ // Case 1.2: checkpoint submission phase is closed
92
+ // but its `scheduledStartDate` is a future date.
93
+ // This means this phase is not yet started. Take the `scheduledEndDate` of this phase.
94
+ if ( checkpointPhase . isOpen || moment ( checkpointPhase . scheduledStartDate ) . isAfter ( ) ) {
95
+ return moment ( checkpointPhase . scheduledEndDate ) ;
96
+ }
74
97
}
75
- return moment ( o . submissionEndTimestamp ) ;
98
+ return moment ( phaseEndDate ( submissionPhase ) ) ;
76
99
}
77
100
78
101
const aDate = nextSubEndDate ( a ) ;
0 commit comments