import { SidenavService } from './../../core/service/sidenav.service'; import { Component, Input, ChangeDetectorRef, ViewChild } from '@angular/core'; import { Term } from '../../core/models/term'; import { Course } from '../../core/models/course'; import { DataService } from '../../core/data.service'; import { CdkDragDrop, transferArrayItem } from '@angular/cdk/drag-drop'; import { MatDialog } from '@angular/material'; import { CourseDetailsDialogComponent } from '../dialogs/course-details-dialog/course-details-dialog.component'; import { CourseDetails } from '../../core/models/course-details'; import { NotesDialogComponent } from '../dialogs/notes-dialog/notes-dialog.component'; import { Note } from '../../core/models/note'; import { Observable } from 'rxjs'; import { map, share } from 'rxjs/operators'; @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; @Input() course: CourseDetails; @Input() subjectsMap: Object; @Input() notes: Observable<Array<Note>>; @Input() favoriteCourses; terms: any[]; note: Object; myNote: any; noteLookup: Object; constructor( private dataService: DataService, public dialog: MatDialog, private sidenavService: SidenavService, private cdRef: ChangeDetectorRef) { } openAddSidenav() { this.sidenavService.open(); } openNotesDialog(note, termCode) { let dialogRef; if (!note) { this.myNote = this.notes.pipe( map(notes => notes.filter(theNote => { return theNote.termCode === termCode; })) ); this.myNote.subscribe(x => { this.noteLookup = x[0]; dialogRef = this.dialog.open(NotesDialogComponent, { data: this.noteLookup || { termCode: this.term.termCode, note: ''} }); dialogRef.afterClosed().subscribe(() => { this.notes = this.dataService.getAllNotes().pipe(share()); }); }); } else { dialogRef = this.dialog.open(NotesDialogComponent, { data: note || { termCode: this.term.termCode, note: ''} }); dialogRef.afterClosed().subscribe(() => { this.notes = this.dataService.getAllNotes().pipe(share()); }); } } 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 } }