diff --git a/src/app/degree-planner/services/api.service.ts b/src/app/degree-planner/services/api.service.ts index 432273b6bbb1e4758b35729f4ea04eaf7f4e62ea..88824e17b43c2a317a091d43f9eed4f900b4a864 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 920423fa4328e47db2d17f176cf6bc859c21a756..59c69d36c00fc1049cafb1d13967d41f7c6156d2 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]; +};