Skip to content
Snippets Groups Projects
favorites-container.component.ts 1.91 KiB
Newer Older
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { Store, select } from '@ngrx/store';
import { SavedForLaterCourse } from '@app/core/models/saved-for-later-course';
import { GlobalState } from '@app/core/state';
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import { getSavedForLaterCourses } from '@app/degree-planner/store/selectors';
import { Course } from '@app/core/models/course';
pnogal's avatar
pnogal committed

jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import { DegreePlannerState } from '@app/degree-planner/store/state';
Isaac Evavold's avatar
Isaac Evavold committed
  AddSaveForLater,
  RemoveCourse,
} from '@app/degree-planner/store/actions/course.actions';
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import { getDropZones } from '@app/degree-planner/store/selectors';
pnogal's avatar
pnogal committed
@Component({
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
  selector: 'cse-favorites-container',
  templateUrl: './favorites-container.component.html',
  styleUrls: ['./favorites-container.component.scss'],
pnogal's avatar
pnogal committed
})
export class SavedForLaterContainerComponent implements OnInit {
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
  public courses$: Observable<SavedForLaterCourse[]>;
  public dropZones$: Observable<String[]>;

  constructor(private store: Store<{ degreePlanner: DegreePlannerState }>) {}

  public ngOnInit() {
    this.dropZones$ = this.store.pipe(select(getDropZones));
    this.courses$ = this.store.pipe(select(getSavedForLaterCourses));
  }

  drop(event: any) {
    const newContainerId = event.container.id;
    const previousContainerId = event.previousContainer.id;
    const fromTerm = previousContainerId.indexOf('term-') === 0;
    const fromDifferentContainer = newContainerId !== previousContainerId;
    if (fromDifferentContainer && fromTerm) {
      const course = event.item.data as Course;
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed

      this.store.dispatch(
Isaac Evavold's avatar
Isaac Evavold committed
        new AddSaveForLater({
          courseId: course.courseId,
          subjectCode: course.subjectCode,
          title: course.title,
          catalogNumber: course.catalogNumber,
        }),
      );

      this.store.dispatch(
Isaac Evavold's avatar
Isaac Evavold committed
        new RemoveCourse({
          recordId: course.id as number,
        }),
pnogal's avatar
pnogal committed
}