Newer
Older
import { ActivatedRoute } from '@angular/router';
import { Component, Input, ChangeDetectorRef, OnInit } from '@angular/core';
import { CdkDragDrop, transferArrayItem } from '@angular/cdk/drag-drop';
import { DataService } from '../../core/data.service';
import { DegreePlannerDataService } from './../../core/service/degree-planner-data.service';
import { SidenavService } from './../../core/service/sidenav.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';
pnogal
committed
@Component({
templateUrl: './term-container.component.html',
styleUrls: ['./term-container.component.scss']
pnogal
committed
})
export class TermContainerComponent implements OnInit {
// @Input() term: Term;
// @Input() courses: Course[];
// @Input() termCodes: String[];
// @Input() termsByAcademicYear: Object;
// @Input() course: CourseDetails;
@Input() favoriteCourses;
pnogal
committed
termsWithNotes: any[];
noteLookup: Object;
constructor(
private dataService: DataService,
private degreePlannerDataSvc: DegreePlannerDataService,
private route: ActivatedRoute,
private sidenavService: SidenavService,
this.notes = route.snapshot.data.requiredData.notes;
}
this.termsWithNotes = this.notes.map(note => note.termCode);
this.degreePlannerDataSvc.getAllTerms().subscribe(data => {
this.dropZones = data.map(termCode => {
return `term-${termCode}`;
});
this.dropZones.push('favoriteCourse-dropZone');
});
openAddSidenav() {
this.sidenavService.open();
}
openNotesDialog(note, termCode) {
let dialogRef;
if (!note) {
this.myNote = this.notes.filter(singleNote => (singleNote.termCode === termCode))[0];
dialogRef = this.dialog.open(NotesDialogComponent, {
data: { note: this.myNote.note, termCode: this.myNote.termCode, id: this.myNote.id }
} else {
dialogRef = this.dialog.open(NotesDialogComponent, { data: { termCode: termCode } });
}
dialogRef = this.dialog.open(NotesDialogComponent, { data: note });
dialogRef.afterClosed().subscribe(result => {
if (result) {
if (result.event === 'save') {
this.termsWithNotes.push(termCode);
this.degreePlannerDataSvc.getAllNotes().subscribe(notes => this.notes = notes);
} else if (result.event === 'remove') {
this.termsWithNotes = this.termsWithNotes.filter(n => n !== termCode);
this.notes = this.notes.filter(n => n.termCode !== termCode);
}
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;
}
const { container, currentIndex, previousContainer, previousIndex } = event;
// Get the course JSON
const item = event.item.data;
console.log(event);
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, this.degreePlannerDataSvc.getPlanId())
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 => {});
} else if (event.previousContainer.id !== event.container.id) {
container.data.push(item);
previousContainer.data.splice(previousIndex, 1);
// Tell the API this course updated
this.dataService.updateCourseTerm(this.degreePlannerDataSvc.getPlanId(), event.item.data.id, newTermCode)
this.degreePlannerDataSvc.getDegreePlanDataById(this.degreePlannerDataSvc.getPlanId());
// Do nothing on drop to same term