Skip to content
Snippets Groups Projects
Commit 9b6c4037 authored by Isaac Evavold's avatar Isaac Evavold
Browse files

ROENROLL-1352

parent 9bb40be0
No related branches found
No related tags found
No related merge requests found
interface CourseBase {
id: number;
id: number | null;
courseId: string;
termCode: string;
termCode: string | null;
topicId: number;
title: string;
subjectCode: string;
......
......@@ -41,7 +41,12 @@ export class RemoveCourseConfirmDialogComponent implements OnInit {
case 'course':
console.log('remove course from term');
console.log(this.course);
this.store.dispatch(new RemoveCourseRequest({ id: this.course.id }));
const id = this.course.id;
if (typeof id === 'number') {
this.store.dispatch(new RemoveCourseRequest({ id }));
} else {
throw new Error('cannot remove a course that does not have an ID');
}
break;
}
}
......
......@@ -314,31 +314,41 @@ export class DegreePlanEffects {
this.api.getActiveTerms(),
).pipe(
// Combine courses and notes by term.
map(([notes, termCourses, currentTerms]) => {
map(([notes, termCourses, activeTerms]) => {
const noteTermCodes = notes.map(note => note.termCode);
const courseTermCodes = termCourses.map(term => term.termCode);
const activeTermCodes = activeTerms.map(term => term.termCode);
/**
* Using the notes & courses relevant to the current degree plan and
* the active terms, generate a sorted list of all unqiue term codes.
*/
const uniqueTermCodes = termCourses.map(course => course.termCode);
const uniqueTermCodes = unique([
...noteTermCodes,
...courseTermCodes,
...activeTermCodes,
]).sort();
/**
* For each unique termCode, build a Term object that includes any
* courses or notes relevant to that termCode.
* Group the notes and courses into a list of visible terms.
*/
const visibleTerms: PlannedTerm[] = uniqueTermCodes.map(termCode => {
return {
termCode,
note: notes.find(note => note.termCode === termCode),
courses: termCourses.filter(term => term.termCode === termCode)[0]
.courses,
};
const note = notes.find(matchesTermCode(termCode));
const termCourse = termCourses.find(matchesTermCode(termCode));
const courses = termCourse ? termCourse.courses : [];
return { termCode, note, courses };
});
const activeTermCodes = uniqueTermCodes.filter(termCode => {
return termCode >= currentTerms[0].termCode;
});
return Object.assign({}, stdin, { visibleTerms }, { activeTermCodes });
return { ...stdin, visibleTerms, activeTermCodes };
}),
);
}
}
const unique = <T>(things: T[]): T[] => {
return things.filter((thing, index, all) => all.indexOf(thing) === index);
};
const matchesTermCode = (termCode: string) => (thing: { termCode: string }) => {
return thing.termCode === termCode;
};
......@@ -6,6 +6,7 @@ import { GlobalState } from '@app/core/state';
import { Year } from '@app/core/models/year';
import { Note } from '@app/core/models/note';
import { Course } from '@app/core/models/course';
import { PlannedTerm } from '@app/core/models/planned-term';
import { DegreePlannerState } from './state';
export const getDegreePlannerState = ({ degreePlanner }: GlobalState) => {
......@@ -61,27 +62,15 @@ export const getAllVisibleTermsByYear = createSelector(
.filter((year, index, self) => self.indexOf(year) === index)
.sort();
const allNotes = state.visibleTerms
.map(term => term.note)
.filter((note): note is Note => note !== undefined);
const allCourses = state.visibleTerms
.map(term => term.courses)
.reduce((flat, nested) => flat.concat(nested), [])
.map(course => {
course.subject = state.subjects[course.subjectCode];
return course;
});
return unqiueYears.map<Year>(year => {
const century = year[0] === '0' ? 0 : 1;
const twoDigitYearCode = parseInt(year.substr(1, 2), 10);
return {
century,
twoDigitYearCode,
fall: getTermForCode(`${year}2`, allCourses, allNotes),
spring: getTermForCode(`${year}4`, allCourses, allNotes),
summer: getTermForCode(`${year}6`, allCourses, allNotes),
fall: getTermForCode(`${year}2`, state.visibleTerms),
spring: getTermForCode(`${year}4`, state.visibleTerms),
summer: getTermForCode(`${year}6`, state.visibleTerms),
};
});
},
......@@ -107,10 +96,12 @@ export const isActiveTerm = (termCode: string) =>
},
);
const getTermForCode = (termCode: string, courses: Course[], notes: Note[]) => {
return {
termCode,
note: notes.find(note => note.termCode === termCode),
courses: courses.filter(course => course.termCode === termCode),
};
const getTermForCode = (termCode: string, terms: PlannedTerm[]) => {
const foundTerm = terms.find(term => term.termCode === termCode);
if (foundTerm !== undefined) {
return foundTerm;
}
return { termCode, courses: [] };
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment