Skip to content
Snippets Groups Projects
term-container.component.ts 4.71 KiB
Newer Older
import { ActivatedRoute } from '@angular/router';
import { Component, Input, ChangeDetectorRef, OnInit } from '@angular/core';
import { CdkDragDrop, transferArrayItem } from '@angular/cdk/drag-drop';
pnogal's avatar
pnogal committed
import { MatDialog } from '@angular/material';

import { DataService } from '../../core/data.service';
import { DegreePlannerDataService } from './../../core/service/degree-planner-data.service';
import { SidenavService } from './../../core/service/sidenav.service';
pnogal's avatar
pnogal committed
import { CourseDetailsDialogComponent } from '../dialogs/course-details-dialog/course-details-dialog.component';
Paulina Nogal's avatar
Paulina Nogal committed
import { NotesDialogComponent } from '../dialogs/notes-dialog/notes-dialog.component';
import { Note } from '../../core/models/note';
Joe Van Boxtel's avatar
Joe Van Boxtel committed
	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() termCode: string;
	@Input() courses: any = [];
pnogal's avatar
pnogal committed
	terms: any[];
Scott Berg's avatar
Scott Berg committed
	dropZones: any[];
	notes: Note[];
	myNote: any;
	termsWithNotes: any[];
	noteLookup: Object;

	constructor(
		private dataService: DataService,
		private degreePlannerDataSvc: DegreePlannerDataService,
		private route: ActivatedRoute,
		public dialog: MatDialog,
		private sidenavService: SidenavService,
		private cdRef: ChangeDetectorRef) {
			this.notes = route.snapshot.data.requiredData.notes;
	}
		this.termsWithNotes = this.notes.map(note => note.termCode);
Scott Berg's avatar
Scott Berg committed

		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];
			if (this.myNote) {
				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);
				}
pnogal's avatar
pnogal committed
	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;
			}
Scott Berg's avatar
Scott Berg committed
	getDropZones() {
		return [];
	}

	drop(event: CdkDragDrop<string[]>) {
		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())
			.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 => {});

		} 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)
			.subscribe( data => {
Scott Berg's avatar
Scott Berg committed
				this.degreePlannerDataSvc.getDegreePlanDataById(this.degreePlannerDataSvc.getPlanId());
			// Do nothing on drop to same term