import { ActivatedRoute } from '@angular/router'; import { DegreePlannerDataService } from './../../core/service/degree-planner-data.service'; import { Component, Input, ChangeDetectorRef, OnInit } from '@angular/core'; import { CdkDragDrop, transferArrayItem } from '@angular/cdk/drag-drop'; import { MatDialog } from '@angular/material'; import { Observable } from 'rxjs'; import { DataService } from '../../core/data.service'; import { CourseDetailsDialogComponent } from '../dialogs/course-details-dialog/course-details-dialog.component'; import { NotesDialogComponent } from '../dialogs/notes-dialog/notes-dialog.component'; import { Note } from '../../core/models/note'; import { SidenavService } from './../../core/service/sidenav.service'; @Component({ selector: 'cse-term-container', templateUrl: './term-container.component.html', styleUrls: ['./term-container.component.scss'] }) export class TermContainerComponent implements OnInit { // @Input() term: Term; // @Input() courses: Course[]; // @Input() termCodes: String[]; // @Input() termsByAcademicYear: Object; // @Input() course: CourseDetails; @Input() favoriteCourses; @Input() courses: any = []; terms: any[]; notes: Note[]; notes$: Observable<Note[]>; myNote: any; noteLookup: Object; constructor( private dataService: DataService, private degreePlannerDataSvc: DegreePlannerDataService, private route: ActivatedRoute, public dialog: MatDialog, private sidenavService: SidenavService, private cdRef: ChangeDetectorRef) { } ngOnInit() { this.notes = this.route.snapshot.data.requiredData.notes; } openAddSidenav() { this.sidenavService.open(); } openNotesDialog(note, termCode) { let dialogRef; if (!note) { this.myNote = this.notes.filter(singleNote => (singleNote.termCode === termCode))[0]; if (this.myNote) { dialogRef = this.dialog.open(NotesDialogComponent, { data: { note: this.myNote.note, termCode: this.myNote.termCode } }); } else { dialogRef = this.dialog.open(NotesDialogComponent, { data: { termCode: termCode } }); } } else { dialogRef = this.dialog.open(NotesDialogComponent, { data: note }); } dialogRef.afterClosed().subscribe(result => { if (result.event === 'save') { this.dataService.getAllNotes().subscribe(notes => this.notes = notes); } else if (result.event === 'remove') { this.notes.forEach((singleNote, i) => { if (singleNote.termCode === termCode) { this.notes.splice(i, 1); } }); } }); } openCourseDetailsDialog(course) { this.dataService.getCourseDetails(course.termCode, course.subjectCode, course.courseId) .subscribe(courseDetails => { const dialogRef = this.dialog.open(CourseDetailsDialogComponent, { data: { courseDetails: courseDetails } }); }); } 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[]>) { const newTermCode = event.container.id.substr(5, 4); 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, newTermCode) .subscribe(data => { const yearCode = newTermCode.substring(0, 3); const termCode = newTermCode.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, newTermCode) .subscribe( data => { // TODO Handle errors }); } // Do nothing on drop to same term } }