Skip to content
Snippets Groups Projects
favorites-container.component.ts 1.66 KiB
Newer Older
Scott Berg's avatar
Scott Berg committed
import { Component, Input } from '@angular/core';
import { DataService } from '../../core/data.service';
import { FavoriteCourse } from '../../core/models/favorite-course';
import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
import { DegreePlannerDataService } from '@app/core/service/degree-planner-data.service';
import { ActivatedRoute } from '@angular/router';
pnogal's avatar
pnogal committed

@Component({
Joe Van Boxtel's avatar
Joe Van Boxtel committed
	selector: 'cse-favorites-container',
	templateUrl: './favorites-container.component.html',
	styleUrls: ['./favorites-container.component.scss']
pnogal's avatar
pnogal committed
})
export class FavoritesContainerComponent {
	favoriteCourse: FavoriteCourse;
	@Input() favoriteDropZone: [];
Scott Berg's avatar
Scott Berg committed
	dropZones: any[];
	favoriteCourses: FavoriteCourse[];
	constructor(
		private route: ActivatedRoute,
Scott Berg's avatar
Scott Berg committed
		public degreePlannerDataSvc: DegreePlannerDataService,
		private dataService: DataService) {
			this.favoriteCourses = route.snapshot.data.requiredData.favorites;
			this.termCodes$ = route.snapshot.data.requiredData.termCodes$;
pnogal's avatar
pnogal committed

	drop(event: CdkDragDrop<string[]>) {
Scott Berg's avatar
Scott Berg committed
		const { container, previousContainer, previousIndex } = event;
		// Get the course JSON
		const item = event.item.data;

		if (event.previousContainer.id !== event.container.id) {
Scott Berg's avatar
Scott Berg committed

			container.data.push(item);
			previousContainer.data.splice(previousIndex, 1);
			// Add this coruse to the favorites list
			this.dataService.saveFavoriteCourse(event.item.data.subjectCode, event.item.data.courseId)
			.subscribe(favoriteCourse => {
				this.favoriteCourse = favoriteCourse;
			});

			// Remove this course from the degree plan
Scott Berg's avatar
Scott Berg committed
			this.dataService.removeCourse(this.degreePlannerDataSvc.getPlanId(), event.item.data.id);
pnogal's avatar
pnogal committed
}