Skip to content
Snippets Groups Projects
Forked from an inaccessible project.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
favorites-container.component.ts 1.65 KiB
import { Component, OnInit, 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';

@Component({
	selector: 'cse-favorites-container',
	templateUrl: './favorites-container.component.html',
	styleUrls: ['./favorites-container.component.scss']
})

export class FavoritesContainerComponent {
	favoriteCourse: FavoriteCourse;
	termCodes$: any;
	@Input() favoriteDropZone: [];
	favoriteCourses: FavoriteCourse[];

	constructor(
		private route: ActivatedRoute,
		private degreePlannerDataSvc: DegreePlannerDataService,
		private dataService: DataService) {
			this.favoriteCourses = route.snapshot.data.requiredData.favorites;
			this.termCodes$ = route.snapshot.data.requiredData.termCodes$;
	}

	drop(event: CdkDragDrop<string[]>) {
		if (event.previousContainer.id !== event.container.id) {
			transferArrayItem(event.previousContainer.data,
				event.container.data,
				event.previousIndex,
				event.currentIndex);

			// 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
			this.dataService.removeCourse(this.degreePlannerDataSvc.getPlanId(), event.item.data.id)
			.subscribe(data => {
				console.log(data);
			});

			console.log(event);
		}
	}
}