From 2efd083ebf4732eb7e29d49128ec3935d0adb6c1 Mon Sep 17 00:00:00 2001 From: Nevio Di Gennaro Date: Tue, 24 Dec 2024 10:31:17 +0100 Subject: [PATCH] bring back all not null assertions and create some switch statements in scoring component for readability --- frontend/cypress/support/commands.ts | 2 +- .../cypress/support/helper/scoringSupport.ts | 8 +- .../action-plan/action-plan.component.ts | 2 +- .../custom/scoring/scoring.component.spec.ts | 4 +- .../custom/scoring/scoring.component.ts | 86 +++++++++---------- 5 files changed, 48 insertions(+), 54 deletions(-) diff --git a/frontend/cypress/support/commands.ts b/frontend/cypress/support/commands.ts index e5fa2511a5..825531899a 100644 --- a/frontend/cypress/support/commands.ts +++ b/frontend/cypress/support/commands.ts @@ -81,7 +81,7 @@ function loginWithCredentials(username: string, password: string) { cy.url() .then((url) => { const currentUrl = new URL(url); - const baseURL = new URL(Cypress.config().baseUrl ?? ''); + const baseURL = new URL(Cypress.config().baseUrl!); expect(currentUrl.pathname) .equal(baseURL.pathname); }); diff --git a/frontend/cypress/support/helper/scoringSupport.ts b/frontend/cypress/support/helper/scoringSupport.ts index c02502316b..cfa2c75aca 100644 --- a/frontend/cypress/support/helper/scoringSupport.ts +++ b/frontend/cypress/support/helper/scoringSupport.ts @@ -56,11 +56,9 @@ function validateScoringWidth(zone: string, percent: number, isOverview: boolean .invoke('width') .then((parentWidth) => { expect(parentWidth).not.to.equal(undefined); - if (parentWidth) { - cy.getZone(zone, isOverview) - .invoke('width') - .should('be.within', parentWidth * (percent / 100) - 3, parentWidth * (percent / 100) + 3); - } + cy.getZone(zone, isOverview) + .invoke('width') + .should('be.within', parentWidth! * (percent / 100) - 3, parentWidth! * (percent / 100) + 3); }); } diff --git a/frontend/src/app/components/action-plan/action-plan.component.ts b/frontend/src/app/components/action-plan/action-plan.component.ts index 7eeea1b059..674ddcf58d 100644 --- a/frontend/src/app/components/action-plan/action-plan.component.ts +++ b/frontend/src/app/components/action-plan/action-plan.component.ts @@ -108,7 +108,7 @@ export class ActionPlanComponent { .subscribe((result) => { if (result) { if (actions[index].id) { - this.actionService.deleteAction(actions[index].id) + this.actionService.deleteAction(actions[index].id!) .subscribe(); } actions.splice(index, 1); diff --git a/frontend/src/app/shared/custom/scoring/scoring.component.spec.ts b/frontend/src/app/shared/custom/scoring/scoring.component.spec.ts index 2e227d1125..ff4c6523b6 100644 --- a/frontend/src/app/shared/custom/scoring/scoring.component.spec.ts +++ b/frontend/src/app/shared/custom/scoring/scoring.component.spec.ts @@ -162,8 +162,8 @@ describe('ScoringComponent', () => { component.commitPercent = 0; component.failPercent = 0; - // Set zone - (component.keyResult.lastCheckIn as CheckInOrdinalMin)!.zone = object.zoneValue; + // Set zone parenthesis needed to make lint happy + ((component.keyResult.lastCheckIn as CheckInOrdinalMin)!.zone!) = object.zoneValue; component.calculatePercentageOrdinal(); // Verify if percentage was set correctly diff --git a/frontend/src/app/shared/custom/scoring/scoring.component.ts b/frontend/src/app/shared/custom/scoring/scoring.component.ts index d5ae44b1d1..8855a7850d 100644 --- a/frontend/src/app/shared/custom/scoring/scoring.component.ts +++ b/frontend/src/app/shared/custom/scoring/scoring.component.ts @@ -74,15 +74,12 @@ export class ScoringComponent implements OnInit, AfterViewInit, OnChanges { * Define width of scoring elements * All checked individually because that if one is undefined, the others can still be set */ - if (this.failElement) { - this.failElement.nativeElement.style.width = this.failPercent + '%'; - } - if (this.commitElement) { - this.commitElement.nativeElement.style.width = this.commitPercent + '%'; - } - if (this.targetElement) { - this.targetElement.nativeElement.style.width = this.targetPercent + '%'; - } + this.failElement!.nativeElement.style.width = this.failPercent + '%'; + + this.commitElement!.nativeElement.style.width = this.commitPercent + '%'; + + this.targetElement!.nativeElement.style.width = this.targetPercent + '%'; + if (this.keyResult.keyResultType === 'metric') { this.labelPercentage.subscribe((value) => { @@ -96,16 +93,9 @@ export class ScoringComponent implements OnInit, AfterViewInit, OnChanges { // Set color of scoring component const scoringClass = this.getScoringColorClassAndSetBorder(); if (scoringClass !== null) { - // All checked individually because that if one is undefined, the others can still be set - if (this.targetElement) { - this.targetElement.nativeElement.classList.add(scoringClass); - } - if (this.commitElement) { - this.commitElement.nativeElement.classList.add(scoringClass); - } - if (this.failElement) { - this.failElement.nativeElement.classList.add(scoringClass); - } + this.targetElement!.nativeElement.classList.add(scoringClass); + this.commitElement!.nativeElement.classList.add(scoringClass); + this.failElement!.nativeElement.classList.add(scoringClass); } // Fill out icon if target percent has reached 100 percent or more @@ -116,7 +106,7 @@ export class ScoringComponent implements OnInit, AfterViewInit, OnChanges { } calculatePercentageOrdinal() { - switch ((this.keyResult.lastCheckIn as CheckInOrdinalMin).zone) { + switch ((this.keyResult.lastCheckIn as CheckInOrdinalMin)!.zone!) { case Zone.STRETCH: this.stretched = true; break; @@ -143,36 +133,42 @@ export class ScoringComponent implements OnInit, AfterViewInit, OnChanges { const keyResultMetric: KeyResultMetricMin = this.castToMetric(); const percentage = calculateCurrentPercentage(keyResultMetric); this.labelPercentage = of(percentage); - if (percentage < 30) { - this.stretched = false; - this.failPercent = 100 / 30 * percentage; - } else if (percentage < 70) { - this.stretched = false; - this.failPercent = 100; - this.commitPercent = 100 / 40 * (percentage - 30); - } else if (percentage < 100) { - this.stretched = false; - this.failPercent = 100; - this.commitPercent = 100; - this.targetPercent = 100 / 30 * (percentage - 70); - } else if (percentage >= 100) { - this.stretched = true; + switch (true) { + case percentage < 30: + this.stretched = false; + this.failPercent = 100 / 30 * percentage; + break; + case percentage < 70: + this.stretched = false; + this.failPercent = 100; + this.commitPercent = 100 / 40 * (percentage - 30); + break; + case percentage < 100: + this.stretched = false; + this.failPercent = 100; + this.commitPercent = 100; + this.targetPercent = 100 / 30 * (percentage - 70); + break; + case percentage >= 100: + this.stretched = true; + break; } } } getScoringColorClassAndSetBorder(): string | null { - if (this.targetPercent > 100) { - return 'score-stretch'; - } else if (this.targetPercent > 0 || this.commitPercent === 100 && this.keyResult.keyResultType === 'metric') { - return 'score-green'; - } else if (this.commitPercent > 0 || this.failPercent === 100 && this.keyResult.keyResultType === 'metric') { - return 'score-yellow'; - } else if (this.failPercent >= 3.3333) { - // 3.3333% because if lower fail is not visible in overview and we display ! - return 'score-red'; - } else { - return null; + switch (true) { + case this.targetPercent > 100: + return 'score-stretch'; + case this.targetPercent > 0 || this.commitPercent === 100 && this.keyResult.keyResultType === 'metric': + return 'score-green'; + case this.commitPercent > 0 || this.failPercent === 100 && this.keyResult.keyResultType === 'metric': + return 'score-yellow'; + case this.failPercent >= 3.3333: + // 3.3333% because if lower fail is not visible in overview and we display ! + return 'score-red'; + default: + return null; } }