From ea4cba1f7634a719d51434ac430720ff98e0e798 Mon Sep 17 00:00:00 2001 From: Scott Berg <saberg3@wisc.edu> Date: Tue, 5 Mar 2019 11:28:06 -0600 Subject: [PATCH] ROENROLL-1399 --- .../degree-planner/services/api.service.ts | 17 +++++++ .../store/effects/plan.effects.ts | 45 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/app/degree-planner/services/api.service.ts b/src/app/degree-planner/services/api.service.ts index 432273b..88824e1 100644 --- a/src/app/degree-planner/services/api.service.ts +++ b/src/app/degree-planner/services/api.service.ts @@ -266,6 +266,23 @@ export class DegreePlannerApiService { ); } + public getUserPreferences(): Observable<{ [key: string]: any }> { + return this.http.get( + `${environment.apiPlannerUrl}/preferences`, + HTTP_OPTIONS, + ); + } + + public updateUserPreferences(payload: { + [key: string]: any; + }): Observable<{ [key: string]: any }> { + return this.http.post( + `${environment.apiPlannerUrl}/preferences`, + payload, + HTTP_OPTIONS, + ); + } + /** * Helper function for building API endpoint URLs */ diff --git a/src/app/degree-planner/store/effects/plan.effects.ts b/src/app/degree-planner/store/effects/plan.effects.ts index 920423f..59c69d3 100644 --- a/src/app/degree-planner/store/effects/plan.effects.ts +++ b/src/app/degree-planner/store/effects/plan.effects.ts @@ -76,13 +76,25 @@ export class DegreePlanEffects { subjects: this.api.getAllSubjects(), subjectDescriptions: this.api.getAllSubjectDescriptions(), activeTermCodes, + userPreferences: this.api.getUserPreferences(), }); }), // Load data specific to the primary degree plan. flatMap( - ({ allDegreePlans, subjects, subjectDescriptions, activeTermCodes }) => { + ({ + allDegreePlans, + subjects, + subjectDescriptions, + activeTermCodes, + userPreferences, + }) => { const savedForLaterCourses = this.loadSavedForLaterCourses(subjects); - const visibleDegreePlan = pickPrimaryDegreePlan(allDegreePlans); + const visibleDegreePlan = userPreferences.degreePlannerSelectedPlan + ? pickDegreePlanById( + userPreferences.degreePlannerSelectedPlan, + allDegreePlans, + ) + : pickPrimaryDegreePlan(allDegreePlans); const visibleYears = loadPlanYears( this.api, visibleDegreePlan.roadmapId, @@ -166,6 +178,9 @@ export class DegreePlanEffects { const touchedPlan = state.payload.visibleDegreePlan.name; const message = `Switched to ${touchedPlan}`; this.snackBar.open(message, undefined, {}); + + // Get the users current preferences and update the selected roadmapId + this.updateSelectedPlan(state.payload.visibleDegreePlan.roadmapId); }), catchError(error => { return of( @@ -261,6 +276,7 @@ export class DegreePlanEffects { }); }), map(({ newPlan, newYears }) => { + this.updateSelectedPlan(newPlan.roadmapId); return new CreatePlanSuccess({ newPlan, newYears }); }), tap(() => { @@ -312,6 +328,23 @@ export class DegreePlanEffects { }), ); } + + private updateSelectedPlan(roadmapId: number) { + // Get the users current preferences and update the selected roadmapId + + this.api + .getUserPreferences() + .toPromise() + .then(prefs => { + this.api + .updateUserPreferences({ + ...prefs, + degreePlannerSelectedPlan: roadmapId, + }) + .toPromise(); + // We have to .toPromise this to actually fire the API call + }); + } } type SimpleMap = { [name: string]: any }; @@ -461,3 +494,11 @@ const pickPrimaryDegreePlan = (plans: DegreePlan[]): DegreePlan => { const primary = plans.find(plan => plan.primary); return primary ? primary : plans[0]; }; + +const pickDegreePlanById = ( + roadmapId: number, + plans: DegreePlan[], +): DegreePlan => { + const plan = plans.find(plan => plan.roadmapId === roadmapId); + return plan ? plan : plans[0]; +}; -- GitLab