Skip to content
Snippets Groups Projects
Commit ea4cba1f authored by Scott Berg's avatar Scott Berg
Browse files

ROENROLL-1399

parent 72f9e7a9
No related branches found
No related tags found
No related merge requests found
Pipeline #34124 passed
......@@ -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
*/
......
......@@ -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];
};
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