import { Component } from '@angular/core'; import { DataService } from '../core/data.service'; import { DegreePlan } from '../core/models/degree-plan'; import { Term } from '../core/models/term'; import { log } from 'util'; import { Note } from '../core/models/note'; @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; subjectsMap: Object; notes: Note[]; firstCurrentTerm: null|string; constructor(private dataService: DataService) { this.firstCurrentTerm = null; this.dataService.getDegreePlans() .subscribe(data => { this.degreePlans = data; this.selectedDegreePlan = this.degreePlans[0].roadmapId; }); this.dataService.getAllPlanData() .subscribe(data => { // Seperate the data from the forked API calls this.degreePlanCourses = data[0]; this.terms = data[1]; // Loop over the current terms and determine the current / future terms for (const term of this.terms) { if (this.firstCurrentTerm === null || parseInt(term.termCode, 10) < parseInt(this.firstCurrentTerm, 10)) { this.firstCurrentTerm = term.termCode; } } // Create the global data object this.termsByAcademicYear = {}; // Loop over all the courses, creating a placeholder year if one doesn't exsist for (const course of this.degreePlanCourses) { // Get the 3 digit year code and 1 digit term code const year = course.termCode.substring(0, 3); const termCode = course.termCode.substring(3); // Create and add a placeholder year to the data object if (!this.termsByAcademicYear[year]) { this.termsByAcademicYear[year] = this.getYearObject(year); } // Add the course to the proper year > term this.termsByAcademicYear[year].terms[termCode].courses.push(course); } }); this.dataService.getSubjectsMap() .subscribe( subjectsMap => { this.subjectsMap = subjectsMap; }); this.dataService.getTerms() .subscribe(terms => {}); this.getAllNotes(); } getAllNotes() { this.dataService.getAllNotes() .subscribe(notes => { this.notes = notes; }); } // Create a new year object to be used in this.termsByAcademicYear getYearObject(yearCode) { const year = { year: yearCode, terms: {} }; for (const termCode of ['2', '4', '6']) { year.terms[termCode] = { termCode: `${yearCode}${termCode}`, courses: [], pastTerm: (this.firstCurrentTerm && parseInt(this.firstCurrentTerm, 10) > parseInt(`${yearCode}${termCode}`, 10)) ? true : false }; } return year; } getCoursesByTerm(termCode) { if (!this.degreePlanCourses) { return []; } for ( const term of this.degreePlanCourses ) { if (term.termCode === termCode) { return term.courses; } } return []; } getTermDropZone() { if (!this.degreePlanCourses) { return false; } const termCodes = [ 'favoriteCourse-dropZone' ]; for (const yearCode in this.termsByAcademicYear) { if (this.termsByAcademicYear[yearCode]) { const year = this.termsByAcademicYear[yearCode]; for (const termKey in year.terms) { if (year.terms[termKey]) { const term = year.terms[termKey]; termCodes.push('term-' + term.termCode); } } } } // console.log(termCodes); return termCodes; } }