Skip to content

Commit

Permalink
fix(openchallenges): challenge card fixes (#2315)
Browse files Browse the repository at this point in the history
* add new arg

* add type-hints; throw error instead of empty string return

* add try-catch

* Update libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts

Co-authored-by: Thomas Schaffter <[email protected]>

* Update libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts

Co-authored-by: Thomas Schaffter <[email protected]>

---------

Co-authored-by: Thomas Schaffter <[email protected]>
  • Loading branch information
vpchung and tschaffter authored Nov 3, 2023
1 parent e2bdab8 commit 2915ab6
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,31 @@ export class ChallengeCardComponent implements OnInit {
: this.imageService.getImage({
objectKey: 'banner-default.svg',
});
if (this.challenge.endDate && this.status === 'completed') {
const timeSince = this.calcTimeDiff(this.challenge.endDate);
if (timeSince) {
this.time_info = `Ended ${timeSince} ago`;
try {
if (this.challenge.endDate && this.status === 'completed') {
const timeSince = this.calcTimeDiff(this.challenge.endDate, true);
if (timeSince) {
this.time_info = `Ended ${timeSince} ago`;
}
} else if (this.challenge.endDate && this.status === 'active') {
this.time_info = `Ends in ${this.calcTimeDiff(
this.challenge.endDate
)}`;
} else if (this.challenge.startDate && this.status === 'upcoming') {
this.time_info = `Starts in ${this.calcTimeDiff(
this.challenge.startDate
)}`;
}
} else if (this.challenge.endDate && this.status === 'active') {
this.time_info = `Ends in ${this.calcTimeDiff(this.challenge.endDate)}`;
} else if (this.challenge.startDate && this.status === 'upcoming') {
this.time_info = `Starts in ${this.calcTimeDiff(
this.challenge.startDate
)}`;
} catch (error: unknown) {
console.log(error);
}
}
}

calcTimeDiff(date: string) {
calcTimeDiff(date: string, hideFarDates = false): string | never {
const pattern = /\d{4}-\d{2}-\d{2}/;
if (!pattern.test(date)) {
return '';
throw Error(`${date} does not match the schema: ${pattern}`);
}
const refDate: any = new Date(date + ' 00:00:00');
const now: any = new Date();
Expand All @@ -92,7 +98,7 @@ export class ChallengeCardComponent implements OnInit {
// Find the largest unit of time and return in human-readable format.
let timeDiffString = '';
for (const [unit, value] of Object.entries(timeDiff)) {
if (unit === 'month' && value > 3) {
if (hideFarDates && unit === 'month' && value > 3) {
break;
} else if (value > 0) {
timeDiffString = `${value} ${unit}` + (value > 1 ? 's' : '');
Expand Down

0 comments on commit 2915ab6

Please sign in to comment.