Newer
Older
import { Observable, Subscription } from 'rxjs';
import { Component, OnInit, OnDestroy, Inject } from '@angular/core';
import { CourseDetails } from '../../../core/models/course-details';
import { MAT_DIALOG_DATA } from '@angular/material';
import * as selectors from '@app/degree-planner/store/selectors';
import * as utils from '@app/degree-planner/shared/utils';
import { GlobalState } from '@app/core/state';
import { Store, select } from '@ngrx/store';
import { distinctUntilChanged } from 'rxjs/operators';
import { FormBuilder, FormGroup } from '@angular/forms';
import {
AddCourse,
RemoveSaveForLater,
} from '@app/degree-planner/store/actions/course.actions';
selector: 'cse-course-details',
templateUrl: './course-details.component.html',
styleUrls: ['./course-details.component.scss'],
export class CourseDetailsComponent implements OnInit, OnDestroy {
public courseDetails: CourseDetails;
public type: 'course' | 'search' | 'saved';
public selectedSearchTerm: string;
public termSelector: FormGroup;
public selectedSearchTerm$: Observable<string>;
public droppableTermCodes$: Observable<string[]>;
public searchTermSubscription: Subscription;
constructor(
@Inject(MAT_DIALOG_DATA) public data: any,
private store: Store<GlobalState>,
private fb: FormBuilder,
) {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
ngOnInit() {
this.selectedSearchTerm$ = this.store.pipe(
select(selectors.getSelectedSearchTerm),
);
this.droppableTermCodes$ = this.store.pipe(
select(selectors.selectAllVisibleYears),
utils.yearsToDroppableTermCodes(),
distinctUntilChanged(utils.compareStringArrays),
);
this.searchTermSubscription = this.selectedSearchTerm$.subscribe(
termCode => {
this.selectedSearchTerm = termCode;
},
);
this.termSelector = this.fb.group({
term: this.selectedSearchTerm,
});
}
ngOnDestroy() {
this.searchTermSubscription.unsubscribe();
}
addCourseToPlan($event) {
$event.preventDefault();
const termCode = this.termSelector.value.term;
const subjectCode = this.courseDetails.subject.subjectCode;
const courseId = this.courseDetails.courseId;
switch (this.type) {
case 'search':
this.store.dispatch(new AddCourse({ subjectCode, courseId, termCode }));
break;
case 'saved':
this.store.dispatch(new AddCourse({ subjectCode, courseId, termCode }));
this.store.dispatch(new RemoveSaveForLater({ subjectCode, courseId }));
break;
}
}