From 94547f25bf0f9a53ae2c7685adb5909c89d916dd Mon Sep 17 00:00:00 2001
From: ievavold <ievavold@wisc.edu>
Date: Mon, 3 Jun 2019 10:11:53 -0500
Subject: [PATCH] fix termcode factory reinitialization error

Error caused when user loaded the degree planner (thus initializing the termcode factory), switched to the dars view (which doesn't reset the termcode factory), then switched back to the degree planner (which attempted to initialize an already initialized termcode factory).

This fixes the bug by only attempting to initialize the termcode factory if the termcode factory is uninitialized
---
 src/app/degree-planner/services/termcode.factory.ts  | 10 +++++++---
 src/app/degree-planner/store/effects/plan.effects.ts |  5 ++++-
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/app/degree-planner/services/termcode.factory.ts b/src/app/degree-planner/services/termcode.factory.ts
index bcdef14..9fc13eb 100644
--- a/src/app/degree-planner/services/termcode.factory.ts
+++ b/src/app/degree-planner/services/termcode.factory.ts
@@ -23,14 +23,18 @@ export class TermCodeFactory {
   }
 
   private requireInitialization() {
-    if (this.state !== 'initialized') {
+    if (this.isNotInitialized()) {
       throw new Error('cannot use TermCodeFactory without active terms');
     }
   }
 
+  public isNotInitialized(): boolean {
+    return this.state !== 'initialized';
+  }
+
   public setActiveTermCodes(active: RawTermCode[]) {
-    if (this.state !== 'uninitialized') {
-      throw new Error('the TermCodeFactory was not uninitialized');
+    if (this.isNotInitialized() === false) {
+      throw new Error('the TermCodeFactory was already initialized');
     } else if (active.length === 0) {
       throw new Error('app cannot have 0 active terms, must have at least 1');
     }
diff --git a/src/app/degree-planner/store/effects/plan.effects.ts b/src/app/degree-planner/store/effects/plan.effects.ts
index 743117b..9301e8d 100644
--- a/src/app/degree-planner/store/effects/plan.effects.ts
+++ b/src/app/degree-planner/store/effects/plan.effects.ts
@@ -68,7 +68,10 @@ export class DegreePlanEffects {
     }),
 
     flatMap(({ allDegreePlans, activeTermCodes, userPreferences }) => {
-      this.termCodeService.setActiveTermCodes(activeTermCodes);
+      if (this.termCodeService.isNotInitialized()) {
+        this.termCodeService.setActiveTermCodes(activeTermCodes);
+      }
+
       const savedForLaterCourses = this.api.getSavedForLaterCourses();
       const visibleDegreePlan = userPreferences.degreePlannerSelectedPlan
         ? pickDegreePlanById(
-- 
GitLab