Skip to content
Snippets Groups Projects
term-container.component.ts 2.42 KiB
Newer Older
// Libraries
import { Component, Input, OnInit } 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';
import { DegreePlannerState } from '@app/degree-planner/state';
import {
	ChangeCourseTermRequest
} from '@app/degree-planner/actions/plan.actions';

// Selectors
import {
	getDropZones,
	isActiveTerm
} from '@app/degree-planner/selectors';

// Services
import { DataService } from '../../core/data.service';
import { DegreePlannerDataService } from './../../core/service/degree-planner-data.service';
import { SidenavService } from './../../core/service/sidenav.service';

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

	constructor(
		private dataService: DataService,
Scott Berg's avatar
Scott Berg committed
		public degreePlannerDataSvc: DegreePlannerDataService,
		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 to = event.container.id.substr(5);
		const { termCode: from, id } = event.item.data;

		if (from === to) {
			return;
		// console.log(to);
		// console.log(from);
		// console.log(id);
		this.store.dispatch(new ChangeCourseTermRequest({ to, from, id }));