Newer
Older
import { Component, Input, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material';
import { Store, select } from '@ngrx/store';
import {
AddCourse,
AddSaveForLater,
MoveCourseBetweenTerms,
import { GlobalState } from '@app/core/state';
import * as selectors from '@app/degree-planner/store/selectors';
import { DegreePlannerApiService } from '@app/degree-planner/services/api.service';
import { ConfirmDialogComponent } from '@app/shared/dialogs/confirm-dialog/confirm-dialog.component';
import { CourseDetailsDialogComponent } from '@app/degree-planner/dialogs/course-details-dialog/course-details-dialog.component';
selector: 'cse-course-item',
templateUrl: './course-item.component.html',
styleUrls: ['./course-item.component.scss'],
export class CourseItemComponent implements OnInit {
@Input() isCurrentTerm: boolean;
@Input() isPastTerm: boolean;
@Input() disabled: boolean;
activeTerm: any;
status: string;
public visibleTermCodes$: Observable<string[]>;
public droppableTermCodes$: Observable<string[]>;
constructor(
private api: DegreePlannerApiService,
public dialog: MatDialog,
private store: Store<GlobalState>,
) {}
ngOnInit() {
switch (true) {
case this.course.studentEnrollmentStatus === 'Enrolled' &&
this.isCurrentTerm:
this.status = 'Enrolled';
break;
case this.course.studentEnrollmentStatus === 'Waitlisted':
this.status = 'Waitlisted';
break;
case !this.course.grade && this.isPastTerm:
this.status = 'Incomplete';
break;
default:
this.status = '';
}
}
this.droppableTermCodes$ = this.store.pipe(
select(selectors.selectAllVisibleYears),
utils.yearsToDroppableTermCodes(),
distinctUntilChanged(utils.compareStringArrays),
);
this.store.dispatch(
new AddSaveForLater({
courseId: course.courseId,
subjectCode: course.subjectCode,
title: course.title,
catalogNumber: course.catalogNumber,
/**
*
* Handle moving a course to different terms based on course type
*
*/
onMove(termCode: string) {
switch (this.type) {
case 'course':
const { id, termCode: from } = this.course as {
id: number;
termCode: string;
};
this.store.dispatch(
new MoveCourseBetweenTerms({ to: termCode, from, id, newIndex: 0 }),
);
break;
case 'saved':
const { subjectCode, courseId } = this.course;
this.store.dispatch(new RemoveSaveForLater({ subjectCode, courseId }));
break;
case 'search':
break;
}
}
/**
*
* Handle saving a course for later (This is not possible if a course is already saved)
*
*/
onSaveForLater() {
const {
courseId,
subjectCode,
title,
catalogNumber,
termCode,
} = this.course;
// Dispatch a save for later event
this.store.dispatch(
new AddSaveForLater({
courseId: courseId,
subjectCode: subjectCode,
title: title,
catalogNumber: catalogNumber,
newIndex: 0,
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
}),
);
// If course is in a term, we need to remove it
if (this.type === 'course') {
this.store.dispatch(
new RemoveCourse({
fromTermCode: termCode,
recordId: this.course.id as number,
}),
);
}
}
/**
*
* Handle removing a course (This is not possible for type 'search')
*
*/
onRemove() {
const dialogOptions = {
title: 'Remove Course?',
text: '',
confirmText: 'Remove Course',
confirmColor: 'accent',
};
switch (this.type) {
case 'saved':
dialogOptions.text = `This will remove "${
this.course.title
}" from your saved courses.`;
break;
default:
dialogOptions.text = `This will remove "${
this.course.title
}" from your degree plan and your cart.`;
}
this.dialog
.open(ConfirmDialogComponent, { data: dialogOptions })
.afterClosed()
.subscribe((result: { confirmed: boolean }) => {
// If the user confirmed the removal, remove course
if (result.confirmed) {
console.log(this.type);
console.log({
type: this.type,
fromTermCode: this.course.termCode,
recordId: this.course.id,
});
switch (this.type) {
case 'course':
this.store.dispatch(
new RemoveCourse({
fromTermCode: this.course.termCode,
recordId: this.course.id as number,
}),
);
break;
case 'saved':
const { subjectCode, courseId } = this.course;
this.store.dispatch(
new RemoveSaveForLater({ subjectCode, courseId }),
);
break;
}
}
});
}
addToTerm(toTermCode: string) {
this.store.dispatch(
new AddCourse({
courseId: this.course.courseId,
termCode: toTermCode,
subjectCode: this.course.subjectCode,
title: this.course.title,
catalogNumber: this.course.catalogNumber,
}),
);
this.api
.getCourseDetails(course.subjectCode, course.courseId)
.subscribe(courseDetails => {
const dialogRef = this.dialog.open(CourseDetailsDialogComponent, {
width: '80%',
data: { courseDetails: courseDetails, courseType: this.type },