From e7c2b2f9555d2ae4339f6973e8dd6feebc2351e7 Mon Sep 17 00:00:00 2001 From: "jvanboxtel@wisc.edu" <jvanboxtel@wisc.edu> Date: Wed, 9 Jan 2019 15:40:16 -0600 Subject: [PATCH] Update academic year pipe to work new data --- .../degree-planner.component.html | 2 +- .../shared/pipes/academic-year-state.pipe.ts | 41 +++++++++---------- src/app/shared/shared.module.ts | 7 +++- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/app/degree-planner/degree-planner.component.html b/src/app/degree-planner/degree-planner.component.html index 8d60695..9b48358 100644 --- a/src/app/degree-planner/degree-planner.component.html +++ b/src/app/degree-planner/degree-planner.component.html @@ -26,7 +26,7 @@ <mat-expansion-panel class="year-container"[expanded]="true"> <mat-expansion-panel-header> <mat-panel-title> - {{ term[0].year }} + {{ term[0].year | academicYearState }} </mat-panel-title> </mat-expansion-panel-header> <div fxLayout="row" fxLayout.lt-md="column" fxLayoutGap="20px" fxLayoutAlign="start stretch" class="term-container-wrapper"> diff --git a/src/app/shared/pipes/academic-year-state.pipe.ts b/src/app/shared/pipes/academic-year-state.pipe.ts index 047db3a..8fbd1fe 100644 --- a/src/app/shared/pipes/academic-year-state.pipe.ts +++ b/src/app/shared/pipes/academic-year-state.pipe.ts @@ -1,3 +1,4 @@ +import { ActivatedRoute } from '@angular/router'; import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ @@ -5,29 +6,25 @@ import { Pipe, PipeTransform } from '@angular/core'; }) export class AcademicYearStatePipe implements PipeTransform { + terms = []; + constructor(private route: ActivatedRoute) { + this.terms = route.snapshot.data.requiredData.terms.map(t => t.termCode); + } - transform(yearCode: string, args?: any): any { - // Get the century digit - const century = parseInt(yearCode.substring(0, 1), 0) + 19; - - // Get the year - const academicYear = parseInt(yearCode.substring(1), 0); - - const endYear = (century * 100) + academicYear; - const startYear = endYear - 1; - - const yearString = `${startYear} - ${endYear}`; - - const currentYear = new Date().getFullYear(); - - if (currentYear === (startYear)) { - return `Current: ${yearString}`; - } else if (startYear > currentYear) { - return `Future: ${yearString}`; - } else if (startYear < currentYear) { - return `Past: ${yearString}`; + transform(year: string): string { + const termCode = this.terms[0]; + let century = 2000; + if (termCode.substr(0, 1) === '0') { + century = 1900; + } + const YYYY = century + Number(year); + + if (year < termCode.substr(1, 2)) { + return 'Past: ' + (YYYY - 1) + '-' + YYYY; + } else if (year > termCode.substr(1, 2)) { + return 'Future: ' + (YYYY - 1) + '-' + YYYY; + } else { + return 'Current: ' + (YYYY - 1) + '-' + YYYY; } - return yearString; } - } diff --git a/src/app/shared/shared.module.ts b/src/app/shared/shared.module.ts index 69442a8..8d954b1 100644 --- a/src/app/shared/shared.module.ts +++ b/src/app/shared/shared.module.ts @@ -39,11 +39,14 @@ const modules = [ MatInputModule, MatTooltipModule ]; +const pipes = [ + GetTermDescriptionPipe, AcademicYearStatePipe, AcademicYearRangePipe +]; @NgModule({ imports: [ modules ], - exports: [ modules, GetTermDescriptionPipe, AcademicYearStatePipe, CourseDetailsComponent, AcademicYearRangePipe ], - declarations: [ GetTermDescriptionPipe, AcademicYearStatePipe, CourseDetailsComponent, AcademicYearRangePipe ] + exports: [ modules, pipes, CourseDetailsComponent ], + declarations: [ pipes, CourseDetailsComponent ] }) export class SharedModule { } -- GitLab