Skip to content
Snippets Groups Projects
term-container.component.ts 3.24 KiB
Newer Older
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import {
  Component,
  Input,
  OnInit,
  ChangeDetectionStrategy,
} from '@angular/core';
import { CdkDragDrop } from '@angular/cdk/drag-drop';
pnogal's avatar
pnogal committed
import { MatDialog } from '@angular/material';
// rsjx / ngrx
import { Observable } from 'rxjs';
import { Store, select } from '@ngrx/store';
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import { DegreePlannerState } from '@app/degree-planner/store/state';
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
  ChangeCourseTermRequest,
  AddCourseRequest,
  RemoveSavedForLaterRequest,
} from '@app/degree-planner/store/actions/course.actions';
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import {
  getDropZones,
  isActiveTerm,
} from '@app/degree-planner/store/selectors';
import { SidenavService } from './../../core/service/sidenav.service';

// Models
import { PlannedTerm } from '@app/core/models/planned-term';
import {
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
  NotesDialogComponent,
  NotesDialogData,
} from '../dialogs/notes-dialog/notes-dialog.component';
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
  selector: 'cse-term-container',
  templateUrl: './term-container.component.html',
  styleUrls: ['./term-container.component.scss'],
export class TermContainerComponent implements OnInit {
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
  @Input() term: PlannedTerm;
  public dropZones$: Observable<String[]>;
  public isActiveTerm$: Observable<Boolean>;

  constructor(
    public dialog: MatDialog,
    private sidenavService: SidenavService,
    private store: Store<{ degreePlanner: DegreePlannerState }>,
  ) {}

  public ngOnInit() {
    this.dropZones$ = this.store.pipe(select(getDropZones));
    this.isActiveTerm$ = this.store.pipe(
      select(isActiveTerm(this.term.termCode)),
    );
  }

  public openAddSidenav(): void {
    this.sidenavService.open();
  }

  public openNotesDialog(): void {
    const termCode = this.term.termCode;
    const data: NotesDialogData = this.term.note
      ? { termCode, hasExistingNote: true, initialText: this.term.note.note }
      : { termCode, hasExistingNote: false };
    this.dialog.open<any, NotesDialogData>(NotesDialogComponent, { data });
  }

  public getTotalCredits(): number {
    return this.term.courses.reduce((sum, course) => {
      return sum + course.credits;
    }, 0);
  }

  drop(event: CdkDragDrop<string[]>) {
    const newContainer = event.container.id;
    const previousContainer = event.previousContainer.id;

    if (newContainer === previousContainer) {
      // If the user dropped a course into the same container do nothing
      return;
    } else if (previousContainer.indexOf('term-') === 0) {
      // If moving from term to term

      // Get the pervious and new term code, and the record ID
      const to = newContainer.substr(5);
      const { termCode: from, id } = event.item.data;

      // Dispatch a new change request
      this.store.dispatch(new ChangeCourseTermRequest({ to, from, id }));
    } else if (previousContainer === 'saved-courses') {
      // If moving from saved courses to term

      // Get the term code from the new term dropzone's ID
      const termCode = newContainer.substr(5);

      // Pull the course data from the moved item
      const { subjectCode, courseId } = event.item.data;

      // Dispatch the add event
      this.store.dispatch(
        new AddCourseRequest({ subjectCode, courseId, termCode }),
      );
      this.store.dispatch(
        new RemoveSavedForLaterRequest({ subjectCode, courseId }),
      );
    }
  }