From 0d990b7cdccd28f65b4035b77a2f3f3624d725b1 Mon Sep 17 00:00:00 2001 From: ievavold <ievavold@wisc.edu> Date: Fri, 1 Feb 2019 14:29:39 -0600 Subject: [PATCH] change 'visibleRoadmapId' -> 'visibleDegreePlan' --- .../store/actions/plan.actions.ts | 7 +- .../store/effects/note.effects.ts | 9 ++- .../store/effects/plan.effects.ts | 76 ++++++++++--------- src/app/degree-planner/store/selectors.ts | 16 ++-- src/app/degree-planner/store/state.ts | 7 +- 5 files changed, 62 insertions(+), 53 deletions(-) diff --git a/src/app/degree-planner/store/actions/plan.actions.ts b/src/app/degree-planner/store/actions/plan.actions.ts index 7095e93..1ce3e17 100644 --- a/src/app/degree-planner/store/actions/plan.actions.ts +++ b/src/app/degree-planner/store/actions/plan.actions.ts @@ -1,5 +1,5 @@ import { Action } from '@ngrx/store'; - +import { DegreePlan } from '@app/core/models/degree-plan'; import { PlannedTerm } from '@app/core/models/planned-term'; import { DegreePlannerState } from '@app/degree-planner/store/state'; import { Course } from '@app/core/models/course'; @@ -42,7 +42,10 @@ export class ChangeVisiblePlanRequest implements Action { export class ChangeVisiblePlanResponse implements Action { public readonly type = PlanActionTypes.ChangeVisiblePlanResponse; constructor( - public payload: { visibleRoadmapId: number; visibleTerms: PlannedTerm[] }, + public payload: { + visibleDegreePlan: DegreePlan; + visibleTerms: PlannedTerm[]; + }, ) {} } diff --git a/src/app/degree-planner/store/effects/note.effects.ts b/src/app/degree-planner/store/effects/note.effects.ts index eb7a0ff..387f658 100644 --- a/src/app/degree-planner/store/effects/note.effects.ts +++ b/src/app/degree-planner/store/effects/note.effects.ts @@ -7,6 +7,7 @@ import { mergeMap, withLatestFrom, map, filter } from 'rxjs/operators'; // Models import { Note } from '@app/core/models/note'; +import { DegreePlan } from '@app/core/models/degree-plan'; // Services import { DegreePlannerApiService } from '@app/degree-planner/services/api.service'; @@ -40,13 +41,13 @@ export class NoteEffects { withLatestFrom(this.store$.select(getDegreePlannerState)), // Only handle WriteNote actions when a current plan ID is set. - filter(([_, state]) => typeof state.visibleRoadmapId === 'number'), + filter(([_, state]) => typeof state.visibleDegreePlan !== undefined), // Using the action and State objects, determine whether to fire the // `updateNote` or `createNote` API. Both of these API calls return // an observable wrapper around the modified/created Note object. mergeMap(([action, state]) => { - const planId = state.visibleRoadmapId as number; + const planId = (state.visibleDegreePlan as DegreePlan).roadmapId; const termCode = action.payload.termCode; const noteText = action.payload.noteText; const existingNote = getExistingNote(state, termCode); @@ -74,11 +75,11 @@ export class NoteEffects { withLatestFrom(this.store$.select(getDegreePlannerState)), // Only handle DeleteNote actions when a current plan ID is set. - filter(([_, state]) => typeof state.visibleRoadmapId === 'number'), + filter(([_, state]) => typeof state.visibleDegreePlan !== undefined), // Using the action and State objects, fire the `deleteNote` API. mergeMap(([action, state]) => { - const planId = state.visibleRoadmapId as number; + const planId = (state.visibleDegreePlan as DegreePlan).roadmapId; const termCode = action.payload.termCode; const existingNote = getExistingNote(state, termCode); if (existingNote !== undefined) { diff --git a/src/app/degree-planner/store/effects/plan.effects.ts b/src/app/degree-planner/store/effects/plan.effects.ts index 7f92568..b387a80 100644 --- a/src/app/degree-planner/store/effects/plan.effects.ts +++ b/src/app/degree-planner/store/effects/plan.effects.ts @@ -24,6 +24,7 @@ import { } from '@app/degree-planner/store/actions/plan.actions'; // Models +import { DegreePlan } from '@app/core/models/degree-plan'; import { PlannedTerm } from '@app/core/models/planned-term'; @Injectable() @@ -43,10 +44,11 @@ export class DegreePlanEffects { // Pick one of the plans to use as the visible plan. map(allDegreePlans => { - const visibleRoadmapId = ( - allDegreePlans.find(plan => plan.primary) || allDegreePlans[0] - ).roadmapId; - return { visibleRoadmapId, allDegreePlans }; + return { + visibleDegreePlan: + allDegreePlans.find(plan => plan.primary) || allDegreePlans[0], + allDegreePlans, + }; }), // Get term data for the degree plan specified by the roadmap ID and any @@ -55,15 +57,9 @@ export class DegreePlanEffects { return forkJoin( this.loadTermsForPlan(stdin), this.api.getSavedForLaterCourses(), - this.api.getAllSubjects(), ).pipe( - map(([planDetails, savedForLater, subjects]) => { - const savedForLaterCourses = savedForLater.map(course => { - course.subject = subjects[course.subjectCode]; - return course; - }); - - return { ...planDetails, savedForLaterCourses, subjects }; + map(([planDetails, savedForLaterCourses]) => { + return { ...planDetails, savedForLaterCourses }; }), ); }), @@ -76,8 +72,19 @@ export class DegreePlanEffects { switch$: Observable<ChangeVisiblePlanResponse> = this.actions$.pipe( ofType<ChangeVisiblePlanRequest>(PlanActionTypes.ChangeVisiblePlanRequest), - // Get the roadmap ID from the action. - map(action => ({ visibleRoadmapId: action.payload.newVisibleRoadmapId })), + // Get the most recent Degree Planner state object from the store. This is + // used to decide to fire either the `updateNote` API or `createNote` API. + withLatestFrom(this.store$.select(getDegreePlannerState)), + + // Get the roadmap ID from the action and use that ID to lookup the + // corresponding degree plan object. + map(([action, state]) => { + return { + visibleDegreePlan: state.allDegreePlans.find(plan => { + return plan.roadmapId === action.payload.newVisibleRoadmapId; + }) as DegreePlan, + }; + }), // Get term data for the degree plan specified by the roadmap ID. flatMap(stdin => this.loadTermsForPlan(stdin)), @@ -91,7 +98,7 @@ export class DegreePlanEffects { ofType<any>(PlanActionTypes.ChangeCourseTermRequest), withLatestFrom(this.store$.select(getDegreePlannerState)), - filter(([_, state]) => typeof state.visibleRoadmapId === 'number'), + filter(([_, state]) => typeof state.visibleDegreePlan !== undefined), // Get the roadmap ID from the action. tap(([action, state]) => { @@ -104,7 +111,7 @@ export class DegreePlanEffects { // TODO error handle the API calls return this.api .updateCourseTerm( - state.visibleRoadmapId, + (state.visibleDegreePlan as DegreePlan).roadmapId, action.payload.id, action.payload.to, ) @@ -136,7 +143,7 @@ export class DegreePlanEffects { ofType<any>(PlanActionTypes.AddCourseRequest), withLatestFrom(this.store$.select(getDegreePlannerState)), - filter(([_, state]) => typeof state.visibleRoadmapId === 'number'), + filter(([_, state]) => state.visibleDegreePlan !== undefined), // Get the roadmap ID from the action. tap(([action, state]) => { @@ -147,7 +154,7 @@ export class DegreePlanEffects { // Get term data for the degree plan specified by the roadmap ID. flatMap(([action, state]) => { // TODO error handle the API calls - const roadmapId = state.visibleRoadmapId; + const roadmapId = (state.visibleDegreePlan as DegreePlan).roadmapId; const { subjectCode, termCode, courseId } = action.payload; return this.api .addCourse(roadmapId as number, subjectCode, courseId, termCode) @@ -173,7 +180,7 @@ export class DegreePlanEffects { ofType<any>(PlanActionTypes.RemoveCourseRequest), withLatestFrom(this.store$.select(getDegreePlannerState)), - filter(([_, state]) => typeof state.visibleRoadmapId === 'number'), + filter(([_, state]) => state.visibleDegreePlan !== undefined), // Get the roadmap ID from the action. // tap(([action, state]) => { @@ -184,16 +191,15 @@ export class DegreePlanEffects { // Get term data for the degree plan specified by the roadmap ID. flatMap(([action, state]) => { // TODO error handle the API calls - return this.api - .removeCourse(state.visibleRoadmapId as number, action.payload.id) - .pipe( - map(response => { - return { - response, - action, - }; - }), - ); + const roadmapId = (state.visibleDegreePlan as DegreePlan).roadmapId; + return this.api.removeCourse(roadmapId, action.payload.id).pipe( + map(response => { + return { + response, + action, + }; + }), + ); }), // Wrap data in an Action for dispatch @@ -211,7 +217,7 @@ export class DegreePlanEffects { ofType<any>(PlanActionTypes.RemoveSavedForLaterReqeust), withLatestFrom(this.store$.select(getDegreePlannerState)), - filter(([_, state]) => typeof state.visibleRoadmapId === 'number'), + filter(([_, state]) => state.visibleDegreePlan !== undefined), // tap(([action, state]) => { // console.log('REMOVE SAVED FOR LATER ----------'); @@ -256,7 +262,7 @@ export class DegreePlanEffects { ofType<any>(PlanActionTypes.AddSavedForLaterReqeust), withLatestFrom(this.store$.select(getDegreePlannerState)), - filter(([_, state]) => typeof state.visibleRoadmapId === 'number'), + filter(([_, state]) => state.visibleDegreePlan !== undefined), // Get the roadmap ID from the action. // tap(([action, state]) => { @@ -294,10 +300,12 @@ export class DegreePlanEffects { }), ); - private loadTermsForPlan<T extends { visibleRoadmapId: number }>(stdin: T) { + private loadTermsForPlan<T extends { visibleDegreePlan: DegreePlan }>( + stdin: T, + ) { return forkJoin( - this.api.getAllNotes(stdin.visibleRoadmapId), - this.api.getAllTermCourses(stdin.visibleRoadmapId), + this.api.getAllNotes(stdin.visibleDegreePlan.roadmapId), + this.api.getAllTermCourses(stdin.visibleDegreePlan.roadmapId), this.api.getActiveTerms(), ).pipe( // Combine courses and notes by term. diff --git a/src/app/degree-planner/store/selectors.ts b/src/app/degree-planner/store/selectors.ts index b50e153..ba2171f 100644 --- a/src/app/degree-planner/store/selectors.ts +++ b/src/app/degree-planner/store/selectors.ts @@ -35,12 +35,16 @@ export const getAllDegreePlans = createSelector( export const getVisibleRoadmapId = createSelector( getDegreePlannerState, - state => state.visibleRoadmapId, + state => { + return state.visibleDegreePlan + ? state.visibleDegreePlan.roadmapId + : undefined; + }, ); -export const getAllSubjects = createSelector( +export const getVisibleDegreePlan = createSelector( getDegreePlannerState, - state => state.subjects, + state => state.visibleDegreePlan, ); export const getAllVisibleTerms = createSelector( @@ -63,11 +67,7 @@ export const getAllVisibleTermsByYear = createSelector( const allCourses = state.visibleTerms .map(term => term.courses) - .reduce((flat, nested) => flat.concat(nested), []) - .map(course => { - course.subject = state.subjects[course.subjectCode]; - return course; - }); + .reduce((flat, nested) => flat.concat(nested), []); return unqiueYears.map<Year>(year => { const century = year[0] === '0' ? 0 : 1; diff --git a/src/app/degree-planner/store/state.ts b/src/app/degree-planner/store/state.ts index 5c49b35..4268f9b 100644 --- a/src/app/degree-planner/store/state.ts +++ b/src/app/degree-planner/store/state.ts @@ -1,23 +1,20 @@ // Models -import { Term } from '@app/core/models/term'; import { PlannedTerm } from '@app/core/models/planned-term'; import { DegreePlan } from '@app/core/models/degree-plan'; import { SavedForLaterCourse } from '@app/core/models/saved-for-later-course'; export interface DegreePlannerState { - visibleRoadmapId?: number; + visibleDegreePlan: DegreePlan | undefined; visibleTerms: PlannedTerm[]; savedForLaterCourses: SavedForLaterCourse[]; activeTermCodes: string[]; allDegreePlans: DegreePlan[]; - subjects: Object; } export const INITIAL_DEGREE_PLANNER_STATE: DegreePlannerState = { - visibleRoadmapId: undefined, + visibleDegreePlan: undefined, visibleTerms: [], savedForLaterCourses: [], activeTermCodes: [], allDegreePlans: [], - subjects: Object }; -- GitLab