Skip to content

Commit e3b0e8b

Browse files
committed
Merge branch 'issue-303' into develop
2 parents fd30cf1 + 449771d commit e3b0e8b

File tree

2 files changed

+29
-32
lines changed
  • __tests__/shared/components/challenge-listing/ChallengeCard/__snapshots__
  • src/shared/components/challenge-listing/ChallengeCard/Status

2 files changed

+29
-32
lines changed

__tests__/shared/components/challenge-listing/ChallengeCard/__snapshots__/Status.jsx.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ exports[`Matches shallow shapshot shapshot 1 1`] = `
112112
<div
113113
className="src-shared-components-challenge-listing-ChallengeCard-Status-___style__time-left___1RyL7"
114114
>
115-
897d 19:18h to go
115+
168d 20h to go
116116
</div>
117117
</div>
118118
</ProgressBarTooltip>
@@ -123,7 +123,7 @@ exports[`Matches shallow shapshot shapshot 1 1`] = `
123123
target={undefined}
124124
>
125125
<span>
126-
897d 19:18h
126+
168d 20h
127127
</span>
128128
<span
129129
className="src-shared-components-challenge-listing-ChallengeCard-Status-___style__to-register___17rpT"
@@ -739,7 +739,7 @@ exports[`Matches shallow shapshot shapshot 1 6`] = `
739739
<div
740740
className="src-shared-components-challenge-listing-ChallengeCard-Status-___style__time-left___1RyL7"
741741
>
742-
min to go
742+
0min 0s to go
743743
</div>
744744
</div>
745745
</ProgressBarTooltip>

src/shared/components/challenge-listing/ChallengeCard/Status/index.jsx

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const DRAFT_MSG = 'In Draft';
2020
const STALLED_TIME_LEFT_MSG = 'Challenge is currently on hold';
2121
const FF_TIME_LEFT_MSG = 'Winner is working on fixes';
2222

23+
const HOUR_MS = 60 * 60 * 1000;
24+
const DAY_MS = 24 * HOUR_MS;
25+
2326
const getTimeLeft = (date, currentPhase) => {
2427
if (!currentPhase || currentPhase === 'Stalled') {
2528
return {
@@ -33,27 +36,18 @@ const getTimeLeft = (date, currentPhase) => {
3336
};
3437
}
3538

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 };
5751
};
5852

5953
function numRegistrantsTipText(number) {
@@ -313,10 +307,18 @@ class ChallengeStatus extends Component {
313307
detailLink,
314308
openChallengesInNewTabs,
315309
} = this.props;
316-
const lng = getTimeLeft(
310+
const timeDiff = getTimeLeft(
317311
challenge.registrationEndDate || challenge.submissionEndDate,
318312
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+
}
320322
return (
321323
<a
322324
href={detailLink}
@@ -325,12 +327,7 @@ class ChallengeStatus extends Component {
325327
target={openChallengesInNewTabs ? '_blank' : undefined}
326328
>
327329
<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 }
334331
</span>
335332
<span styleName="to-register">to register</span>
336333
</a>

0 commit comments

Comments
 (0)