import { Component, Input } from '@angular/core'; import { Term } from '../../core/models/term'; import { Course } from '../../core/models/course'; import { DataService } from '../../core/data.service'; import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop'; @Component({ selector: 'cse-term-container', templateUrl: './term-container.component.html', styleUrls: ['./term-container.component.scss'] }) export class TermContainerComponent { @Input() term: Term; @Input() courses: Course[]; @Input() termCodes: String[]; @Input() termsByAcademicYear: Object; constructor(private dataService: DataService) {} getTotalCredits() { if (!this.courses) { return '0'; } let total = 0; for (const course of this.courses) { switch (typeof course.credits) { case 'number': total += course.credits; break; } } return total; } drop(event: CdkDragDrop<string[]>) { if (event.previousContainer.id === 'favoriteCourse-dropZone' && event.container.id !== 'favoriteCourse-dropZone') { // If moving from favorites to term this.dataService.addCourse(event.item.data.subjectCode, event.item.data.courseId, event.container.id) .subscribe(data => { const yearCode = event.container.id.substring(0, 3); const termCode = event.container.id.substring(3); this.termsByAcademicYear[yearCode].terms[termCode].courses[event.currentIndex] = data; }); this.dataService.removeFavoriteCourse(event.item.data.subjectCode, event.item.data.courseId) .subscribe(data => {}); // Move the course into the front end data structure transferArrayItem(event.previousContainer.data, event.container.data, event.previousIndex, event.currentIndex); } else if (event.previousContainer.id !== event.container.id) { // If moving from term to term // Move the course in the front end data object transferArrayItem(event.previousContainer.data, event.container.data, event.previousIndex, event.currentIndex); // Tell the API this course updated this.dataService.updateCourseTerm(event.item.data.id, event.container.id) .subscribe( data => { // TODO Handle errors }); } // Do nothing on drop to same term } }