Skip to content
Snippets Groups Projects
course-item.component.ts 4.05 KiB
Newer Older
import { DegreePlannerDataService } from './../../../core/service/degree-planner-data.service';
import { ActivatedRoute } from '@angular/router';
import { Component, Input, ChangeDetectorRef, OnInit } from '@angular/core';
import { Course } from '../../../core/models/course';
import { Subject } from '@app/core/models/course-details';
import { Term } from '../../../core/models/term';
import { FavoriteCourse } from '../../../core/models/favorite-course';
import { DataService } from '../../../core/data.service';
import { CourseDetailsDialogComponent } from '../../dialogs/course-details-dialog/course-details-dialog.component';
// tslint:disable-next-line:max-line-length
import { RemoveCourseConfirmDialogComponent } from '../../dialogs/remove-course-confirm-dialog/remove-course-confirm-dialog.component';
import { MatDialog } from '@angular/material';

@Component({
Joe Van Boxtel's avatar
Joe Van Boxtel committed
	selector: 'cse-course-item',
	templateUrl: './course-item.component.html',
	styleUrls: ['./course-item.component.scss']

export class CourseItemComponent implements OnInit {
	termsInAcademicYear: [];
	favoriteCourse: FavoriteCourse;
	degreePlanCourses: any[];
	@Input() course: Course;
	@Input() status: string;
	@Input() disabled: string;
	@Input() term: Term;
	@Input() termsByAcademicYear: Object;
	@Input() refreshContent;
	@Input() savedForLater: Boolean;
	@Input() favoriteCourses: FavoriteCourse[];
	subjects: Object;

	constructor(
		private dataService: DataService,
		public dialog: MatDialog,
		private route: ActivatedRoute,
		private degreePlannerDataSvc: DegreePlannerDataService,
		private cdRef: ChangeDetectorRef) {
			this.subjects = route.snapshot.data.requiredData.subjects;
		}

	ngOnInit() {
	}

	removeCourseFromUI(courseItem) {
		const index = courseItem.indexOf(this.course, 0);
		if (index > -1) {
			courseItem = courseItem.splice(index, 1);
		}
	}

	openCourseDetailsDialog(course) {
		this.dataService.getCourseDetails(course.termCode, course.subjectCode, course.courseId)
		.subscribe(courseDetails => {
			const dialogRef = this.dialog.open(CourseDetailsDialogComponent, {
				data: { courseDetails: courseDetails }
			});
		});
	}

	// Get term codes to display in the course item menu
	getAllTermCodes() {
		const termYears: any = Object.values(this.termsByAcademicYear);
		this.termsInAcademicYear = termYears.map(year => year.terms).flat();
	}

	// Add this coruse to the favorites list
	moveToFavorites(course) {
		this.dataService.saveFavoriteCourse(course.subjectCode, course.courseId)
		.subscribe(favoriteCourse => {
			this.favoriteCourses.push(this.course);
		});

		// Remove this course from term
		this.dataService.removeCourse(this.degreePlannerDataSvc.getPlanId(), course.id)
		.subscribe(data => {
			this.removeCourseFromUI(this.courses);
		});
	}

	// Add Favorite course to term
	addToTerm(newTermCode) {
		this.dataService.addCourse( this.course.subjectCode, this.course.courseId, newTermCode, this.degreePlannerDataSvc.getPlanId)
		.subscribe( data => {
			const yearCode = newTermCode.substring(0, 3);
			const termCode = newTermCode.substring(3);
			this.termsByAcademicYear[yearCode].terms[termCode].courses.push(this.course);
		});

		// Remove course from favorites list
		this.dataService.removeFavoriteCourse(this.course.subjectCode, this.course.courseId)
		.subscribe(data => {
			this.removeCourseFromUI(this.favoriteCourses);
		});
	}
	// Move course to another term
	switchTerm(newTermCode) {
		this.dataService.updateCourseTerm(this.degreePlannerDataSvc.getPlanId, this.course.id, newTermCode)
		.subscribe( data => {
			const index = this.courses.indexOf(this.course, 0);
			if (index > -1) {
				this.courses = this.courses.splice(index, 1);
				const yearCode = newTermCode.substring(0, 3);
				const termCode = newTermCode.substring(3);
				this.termsByAcademicYear[yearCode].terms[termCode].courses.push(this.course);
			}
		});
	}
	openRemoveConfirmationDialog(savedForLater) {
		const dialogRef = this.dialog.open(RemoveCourseConfirmDialogComponent, {
			data: { course: this.course, courses: this.courses, favoriteCourses: this.favoriteCourses, savedForLater }
		});
	}