Newer
Older

Paulina Nogal
committed
import { Component, OnInit, OnDestroy, Inject, Input } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Observable, Subscription } from 'rxjs';

Paulina Nogal
committed
import { distinctUntilChanged, map, filter } from 'rxjs/operators';
import { MatDialog } from '@angular/material';
import { Store, select } from '@ngrx/store';
import { CourseDetails } from '@app/core/models/course-details';
import * as selectors from '@app/degree-planner/store/selectors';
import * as utils from '@app/degree-planner/shared/utils';
import { TermCode } from '@app/core/models/termcode';

Paulina Nogal
committed
import { Course } from '@app/core/models/course';
import { PlannedTerm } from '@app/core/models/planned-term';
import { ConfirmDialogComponent } from '@app/shared/dialogs/confirm-dialog/confirm-dialog.component';
import {
AddCourse,
RemoveSaveForLater,
} from '@app/degree-planner/store/actions/course.actions';

Paulina Nogal
committed
const isntUndefined = <T>(thing: T | undefined): thing is T => {
return thing !== undefined;
};
selector: 'cse-course-details',
templateUrl: './course-details.component.html',
styleUrls: ['./course-details.component.scss'],
export class CourseDetailsComponent implements OnInit, OnDestroy {

Paulina Nogal
committed
@Input() termCode: TermCode;
public courseDetails: CourseDetails;
public type: 'course' | 'search' | 'saved';
public selectedSearchTerm: string;

Paulina Nogal
committed
public term$: Observable<PlannedTerm>;
public selectedSearchTerm$: Observable<string>;
public droppableTermCodes$: Observable<string[]>;
public searchTermSubscription: Subscription;

Paulina Nogal
committed
public termSubscription: Subscription;
public plannedCourses: ReadonlyArray<Course>;
constructor(
@Inject(MAT_DIALOG_DATA) public data: any,
private store: Store<GlobalState>,
private fb: FormBuilder,

Paulina Nogal
committed
public dialog: MatDialog,
ngOnInit() {
this.selectedSearchTerm$ = this.store.pipe(
select(selectors.getSelectedSearchTerm),
map(termCode => (termCode ? termCode.toString() : '0000')),
);
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();

Paulina Nogal
committed
const termCode = new TermCode(this.termSelector.value.term);
const subjectCode = this.courseDetails.subject.subjectCode;
const courseId = this.courseDetails.courseId;
const payload = {
courseId,
termCode,
subjectCode,
title: this.courseDetails.title,
catalogNumber: this.courseDetails.catalogNumber,
};

Paulina Nogal
committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
this.term$ = this.store.pipe(
select(selectors.selectVisibleTerm, {
termCode: new TermCode(this.termSelector.value.term),
}),
filter(isntUndefined),
distinctUntilChanged(),
);
this.termSubscription = this.term$.subscribe(term => {
this.plannedCourses = term.plannedCourses;
});
const isCourseInPlannedCourses = this.plannedCourses.some(
course => course.courseId === this.courseDetails.courseId,
);
if (isCourseInPlannedCourses) {
this.dialog
.open(ConfirmDialogComponent, {
data: {
title: "Can't add course to term",
confirmText: 'OK',
dialogClass: 'alertDialog',
text: `This course already exists in selected term`,
},
})
.afterClosed();
} else {
switch (this.type) {
case 'search':
this.store.dispatch(new AddCourse(payload));
break;

Paulina Nogal
committed
case 'saved':
this.store.dispatch(new AddCourse(payload));
this.store.dispatch(
new RemoveSaveForLater({ subjectCode, courseId }),
);
break;
}