import { Component } from '@angular/core'; import { DataService } from '../core/data.service'; import { DegreePlan } from '../core/models/degree-plan'; import { Term } from '../core/models/term'; @Component({ selector: 'cse-degree-planner', templateUrl: './degree-planner.component.html', styleUrls: ['./degree-planner.component.scss'] }) export class DegreePlannerComponent { degreePlans: DegreePlan[]; degreePlanCourses: any[]; selectedDegreePlan: number; terms: Term[]; termsByAcademicYear: Object; constructor(private dataService: DataService) { this.dataService.getDegreePlans() .subscribe(data => { this.degreePlans = data; this.selectedDegreePlan = this.degreePlans[0].roadmapId; }); this.dataService.getDegreePlannerCourseData() .subscribe(degreePlanCourses => { this.degreePlanCourses = degreePlanCourses; }); this.dataService.getTerms() .subscribe(terms => { this.terms = terms; // Create an empty object to store term data in this.termsByAcademicYear = {}; // Loop through each term this.terms.forEach(term => { // Get the 3 digit year code and 1 digit term code const year = term.termCode.substring(0, 3); const termCode = term.termCode.substring(3); // If this year does not exsist in the "termsByAcademicYear" create a new year // This year has placeholder data for each term in the year if (!this.termsByAcademicYear[year]) { this.termsByAcademicYear[year] = { year: year, terms: { 2: { termCode: `${year}2`}, 4: { termCode: `${year}4`}, 6: { termCode: `${year}6`} } }; } // Overwrite the default term with the current term this.termsByAcademicYear[year].terms[termCode] = term; }); }); } getCoursesByTerm(termCode) { if (!this.degreePlanCourses) { return false; } for ( const term of this.degreePlanCourses ) { if (term.termCode === termCode) { return term.courses; } } } }