From 3d342b76676e3bd238cbca1c227e1e0d377924d7 Mon Sep 17 00:00:00 2001 From: verena <9377970+vpchung@users.noreply.github.com> Date: Thu, 2 Nov 2023 23:59:05 +0000 Subject: [PATCH 1/5] add new arg --- .../ui/src/lib/challenge-card/challenge-card.component.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts index 0fcb870101..f79cad99bc 100644 --- a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts +++ b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts @@ -58,7 +58,7 @@ export class ChallengeCardComponent implements OnInit { objectKey: 'banner-default.svg', }); if (this.challenge.endDate && this.status === 'completed') { - const timeSince = this.calcTimeDiff(this.challenge.endDate); + const timeSince = this.calcTimeDiff(this.challenge.endDate, true); if (timeSince) { this.time_info = `Ended ${timeSince} ago`; } @@ -72,7 +72,7 @@ export class ChallengeCardComponent implements OnInit { } } - calcTimeDiff(date: string) { + calcTimeDiff(date: string, hideFarDates = false) { const pattern = /\d{4}-\d{2}-\d{2}/; if (!pattern.test(date)) { return ''; @@ -92,7 +92,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' : ''); From 1c59c0fbdc0a9f84f454cf57a1e16c44364affd3 Mon Sep 17 00:00:00 2001 From: verena <9377970+vpchung@users.noreply.github.com> Date: Fri, 3 Nov 2023 00:14:08 +0000 Subject: [PATCH 2/5] add type-hints; throw error instead of empty string return --- .../ui/src/lib/challenge-card/challenge-card.component.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts index f79cad99bc..755b40b294 100644 --- a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts +++ b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts @@ -72,10 +72,10 @@ export class ChallengeCardComponent implements OnInit { } } - calcTimeDiff(date: string, hideFarDates = false) { + 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(); From 918c04af7ac7f736142e834dd66b0c7a538553d0 Mon Sep 17 00:00:00 2001 From: verena <9377970+vpchung@users.noreply.github.com> Date: Fri, 3 Nov 2023 00:14:25 +0000 Subject: [PATCH 3/5] add try-catch --- .../challenge-card.component.ts | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts index 755b40b294..afcdb697ac 100644 --- a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts +++ b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts @@ -57,17 +57,23 @@ 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, true); - 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 (e: unknown) { + console.log(e); } } } From 79a3a92f9c024547c8545a9914aabda2517783e8 Mon Sep 17 00:00:00 2001 From: Verena Chung <9377970+vpchung@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:03:45 -0700 Subject: [PATCH 4/5] Update libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts Co-authored-by: Thomas Schaffter --- .../ui/src/lib/challenge-card/challenge-card.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts index afcdb697ac..b77f12fd17 100644 --- a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts +++ b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts @@ -72,7 +72,7 @@ export class ChallengeCardComponent implements OnInit { this.challenge.startDate )}`; } - } catch (e: unknown) { + } catch (error: unknown) { console.log(e); } } From 71bf2c62aae5ae4a307bc85737b1fac45da2206e Mon Sep 17 00:00:00 2001 From: Verena Chung <9377970+vpchung@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:04:07 -0700 Subject: [PATCH 5/5] Update libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts Co-authored-by: Thomas Schaffter --- .../ui/src/lib/challenge-card/challenge-card.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts index b77f12fd17..4d9a674cb9 100644 --- a/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts +++ b/libs/openchallenges/ui/src/lib/challenge-card/challenge-card.component.ts @@ -73,7 +73,7 @@ export class ChallengeCardComponent implements OnInit { )}`; } } catch (error: unknown) { - console.log(e); + console.log(error); } } }