4
4
5
5
import moment from 'moment' ;
6
6
import { find , sumBy } from 'lodash' ;
7
+ import { phaseStartDate , phaseEndDate } from './helper' ;
7
8
8
9
export const SORTS = {
9
10
CURRENT_PHASE : 'current-phase' ,
@@ -52,13 +53,21 @@ export default {
52
53
} ,
53
54
[ SORTS . MOST_RECENT ] : {
54
55
func : ( a , b ) => {
55
- const getRegistrationStartDate = ( challenge ) => {
56
+ const getChallengeStartDate = ( challenge ) => {
57
+ // extract the phases from `challenge.phases`,
58
+ // as `challenge.registrationStartDate` returned from API is not reliable
56
59
const registrationPhase = find ( challenge . phases , p => p . name === 'Registration' ) ;
57
- return registrationPhase . actualStartDate || registrationPhase . scheduledStartDate ;
60
+ const submissionPhase = find ( challenge . phases , p => p . name === 'Submission' ) ;
61
+ // registration phase exists
62
+ if ( registrationPhase ) {
63
+ return moment ( phaseStartDate ( registrationPhase ) ) ;
64
+ }
65
+ // registration phase doesnt exist, This is possibly a F2F or TSK. Take submission phase
66
+ return moment ( phaseStartDate ( submissionPhase ) ) ;
58
67
} ;
59
- const aRegistrationStartDate = getRegistrationStartDate ( a ) ;
60
- const bRegistrationStartDate = getRegistrationStartDate ( b ) ;
61
- return moment ( bRegistrationStartDate ) . diff ( aRegistrationStartDate ) ;
68
+ const aChallengeStartDate = getChallengeStartDate ( a ) ;
69
+ const bChallengeStartDate = getChallengeStartDate ( b ) ;
70
+ return bChallengeStartDate . diff ( aChallengeStartDate ) ;
62
71
} ,
63
72
name : 'Most recent' ,
64
73
} ,
@@ -77,12 +86,20 @@ export default {
77
86
[ SORTS . TIME_TO_REGISTER ] : {
78
87
func : ( a , b ) => {
79
88
const getRegistrationEndDate = ( challenge ) => {
89
+ // extract the registration phase from `challenge.phases`,
90
+ // as `challenge.registrationEndDate` returned from API is not reliable
80
91
const registrationPhase = find ( challenge . phases , p => p . name === 'Registration' ) ;
81
- return registrationPhase . actualEndDate || registrationPhase . scheduledEndDate ;
92
+ const submissionPhase = find ( challenge . phases , p => p . name === 'Submission' ) ;
93
+ // case 1: registration phase exists
94
+ if ( registrationPhase ) {
95
+ return moment ( phaseEndDate ( registrationPhase ) ) ;
96
+ }
97
+ // case 2: registration phase doesn't exist. Take submission phase instead.
98
+ return moment ( phaseEndDate ( submissionPhase ) ) ;
82
99
} ;
83
100
84
- const aDate = moment ( getRegistrationEndDate ( a ) || a . submissionEndTimestamp ) ;
85
- const bDate = moment ( getRegistrationEndDate ( b ) || b . submissionEndTimestamp ) ;
101
+ const aDate = getRegistrationEndDate ( a ) ;
102
+ const bDate = getRegistrationEndDate ( b ) ;
86
103
87
104
if ( aDate . isBefore ( ) && bDate . isAfter ( ) ) return 1 ;
88
105
if ( aDate . isAfter ( ) && bDate . isBefore ( ) ) return - 1 ;
@@ -94,11 +111,23 @@ export default {
94
111
} ,
95
112
[ SORTS . TIME_TO_SUBMIT ] : {
96
113
func : ( a , b ) => {
97
- function nextSubEndDate ( o ) {
98
- if ( o . checkpointSubmissionEndDate && moment ( o . checkpointSubmissionEndDate ) . isAfter ( ) ) {
99
- return moment ( o . checkpointSubmissionEndDate ) ;
114
+ function nextSubEndDate ( challenge ) {
115
+ // extract the submission and checkpoint (if any) phases from `challenge.phases`,
116
+ // as `challenge.submissionEndDate` returned from API is not reliable
117
+ const checkpointPhase = find ( challenge . phases , p => p . name === 'Checkpoint Submission' ) ;
118
+ const submissionPhase = find ( challenge . phases , p => p . name === 'Submission' ) ;
119
+ // Case 1: challenge has checkpoint submission phase
120
+ if ( ! ! checkpointPhase === true ) {
121
+ // Case 1.1: checkpoint submission phase is still open.
122
+ // then take the `scheduledEndDate` of this phase.
123
+ // Case 1.2: checkpoint submission phase is closed
124
+ // but its `scheduledStartDate` is a future date.
125
+ // This means this phase is not yet started. Take the `scheduledEndDate` of this phase.
126
+ if ( checkpointPhase . isOpen || moment ( checkpointPhase . scheduledStartDate ) . isAfter ( ) ) {
127
+ return moment ( checkpointPhase . scheduledEndDate ) ;
128
+ }
100
129
}
101
- return moment ( o . submissionEndTimestamp ) ;
130
+ return moment ( phaseEndDate ( submissionPhase ) ) ;
102
131
}
103
132
104
133
const aDate = nextSubEndDate ( a ) ;
0 commit comments