From 15a5023a483981882e3bfb73b79cb3b4998738d0 Mon Sep 17 00:00:00 2001 From: Cagdas U Date: Tue, 18 Aug 2020 06:45:27 +0300 Subject: [PATCH] fix(challenge-listing): `CURRENT_PHASE` sorting Addresses topcoder-platform/community-app#4129 --- src/shared/utils/challenge-listing/sort.js | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/shared/utils/challenge-listing/sort.js b/src/shared/utils/challenge-listing/sort.js index 4ef586d1f8..27a97696c8 100644 --- a/src/shared/utils/challenge-listing/sort.js +++ b/src/shared/utils/challenge-listing/sort.js @@ -21,7 +21,33 @@ export const SORTS = { export default { [SORTS.CURRENT_PHASE]: { - func: (a, b) => a.status.localeCompare(b.status), + func: (a, b) => { + const aPhases = a.phases || []; + const bPhases = b.phases || []; + const aPhase = aPhases + .filter(p => p.name !== 'Registration' && p.isOpen) + .sort((p1, p2) => moment(p1.scheduledEndDate).diff(p2.scheduledEndDate))[0]; + const bPhase = bPhases + .filter(p => p.name !== 'Registration' && p.isOpen) + .sort((p1, p2) => moment(p1.scheduledEndDate).diff(p2.scheduledEndDate))[0]; + + let aPhaseName = 'Stalled'; + let bPhaseName = 'Stalled'; + if (!aPhase && a.type === 'First2Finish' && aPhases.length) { + aPhaseName = 'Submission'; + } + if (!bPhase && b.type === 'First2Finish' && bPhases.length) { + bPhaseName = 'Submission'; + } + + if (aPhase) aPhaseName = aPhase.name; + else if (a.status === 'Draft') aPhaseName = 'Draft'; + + if (bPhase) bPhaseName = bPhase.name; + else if (b.status === 'Draft') bPhaseName = 'Draft'; + + return aPhaseName.localeCompare(bPhaseName); + }, name: 'Current phase', }, [SORTS.MOST_RECENT]: {