Newer
Older
import { Component, Input } from '@angular/core';
import { Course } from '../../core/models/course';
import { DataService } from '../../core/data.service';
import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
pnogal
committed
@Component({
templateUrl: './term-container.component.html',
styleUrls: ['./term-container.component.scss']
pnogal
committed
})
export class TermContainerComponent {
@Input() term: Term;
@Input() courses: Course[];
@Input() termsByAcademicYear: Object;
pnogal
committed
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;
}
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