From 3252ba66844e43dcb4b9b5796380a03ce4356dc6 Mon Sep 17 00:00:00 2001
From: "jvanboxtel@wisc.edu" <jvanboxtel@wisc.edu>
Date: Tue, 12 Feb 2019 11:41:24 -0600
Subject: [PATCH] ROENROLL-1359

---
 src/app/core/models/year.ts                     | 11 ++++++-----
 .../degree-planner.component.html               |  2 +-
 .../degree-planner/degree-planner.component.ts  | 14 +++++++++++++-
 .../degree-planner/store/actions/ui.actions.ts  | 10 ++++++++++
 .../store/effects/plan.effects.ts               | 17 +++++++++++++++++
 src/app/degree-planner/store/reducer.ts         | 12 +++++++++++-
 src/app/degree-planner/store/selectors.ts       |  2 ++
 src/app/degree-planner/store/state.ts           |  2 ++
 8 files changed, 62 insertions(+), 8 deletions(-)
 create mode 100644 src/app/degree-planner/store/actions/ui.actions.ts

diff --git a/src/app/core/models/year.ts b/src/app/core/models/year.ts
index ed4cef9..f462c4f 100644
--- a/src/app/core/models/year.ts
+++ b/src/app/core/models/year.ts
@@ -1,9 +1,10 @@
 import { PlannedTerm } from '@app/core/models/planned-term';
 
 export interface Year {
-	century: 0 | 1;
-	twoDigitYearCode: number;
-	fall: PlannedTerm;
-	spring: PlannedTerm;
-	summer: PlannedTerm;
+  century: 0 | 1;
+  twoDigitYearCode: number;
+  fall: PlannedTerm;
+  spring: PlannedTerm;
+  summer: PlannedTerm;
+  expandedState: boolean;
 }
diff --git a/src/app/degree-planner/degree-planner.component.html b/src/app/degree-planner/degree-planner.component.html
index 6aec034..5c0face 100644
--- a/src/app/degree-planner/degree-planner.component.html
+++ b/src/app/degree-planner/degree-planner.component.html
@@ -45,7 +45,7 @@
 		<div fxLayout="column" fxLayoutGap="20px" fxLayoutAlign="start stretch" style="margin: 24px">
 			<mat-accordion multi="true" *ngIf="(firstActiveTermCode$ | async) as cutoffTermCode">
 				<ng-container *ngFor="let year of termsByYear$ | async">
-					<mat-expansion-panel class="year-container"[expanded]="true">
+					<mat-expansion-panel class="year-container" [expanded]="year.expandedState"  (opened)="handleAcademicYearToggle(year)" (closed)="handleAcademicYearToggle(year)">
 						<mat-expansion-panel-header>
 							<mat-panel-title>
 								{{ year.twoDigitYearCode | academicYearState:cutoffTermCode }}
diff --git a/src/app/degree-planner/degree-planner.component.ts b/src/app/degree-planner/degree-planner.component.ts
index ebfa23d..1226c10 100644
--- a/src/app/degree-planner/degree-planner.component.ts
+++ b/src/app/degree-planner/degree-planner.component.ts
@@ -1,9 +1,14 @@
+import { tap } from 'rxjs/operators';
 // Libraries
 import { OnInit } from '@angular/core';
 import { Observable } from 'rxjs';
 import { select } from '@ngrx/store';
 import { Component } from '@angular/core';
-import { MatSelectChange } from '@angular/material';
+import {
+  MatSelectChange,
+  MatExpansionPanelState,
+  MatExpansionPanel,
+} from '@angular/material';
 import { MatDialog } from '@angular/material';
 import { Store } from '@ngrx/store';
 import { MediaMatcher } from '@angular/cdk/layout';
@@ -30,6 +35,7 @@ import {
 } from '@app/degree-planner/store/actions/plan.actions';
 
 import { ModifyPlanDialogComponent } from './dialogs/modify-plan-dialog/modify-plan-dialog.component';
+import { ToggleAcademicYear } from './store/actions/ui.actions';
 
 @Component({
   selector: 'cse-degree-planner',
@@ -65,6 +71,12 @@ export class DegreePlannerComponent implements OnInit {
     this.termsByYear$ = this.store.pipe(select(getAllVisibleTermsByYear));
   }
 
+  public handleAcademicYearToggle(year: Year): void {
+    // this.store.dispatch(
+    // new ToggleAcademicYear({ year: year.twoDigitYearCode.toString() }),
+    // );
+  }
+
   public handleDegreePlanChange(event: MatSelectChange): void {
     if (typeof event.value === 'number') {
       this.store.dispatch(new SwitchPlan({ newVisibleRoadmapId: event.value }));
diff --git a/src/app/degree-planner/store/actions/ui.actions.ts b/src/app/degree-planner/store/actions/ui.actions.ts
new file mode 100644
index 0000000..21964d3
--- /dev/null
+++ b/src/app/degree-planner/store/actions/ui.actions.ts
@@ -0,0 +1,10 @@
+import { Action } from '@ngrx/store';
+
+export enum UIActionTypes {
+  ToggleAcademicYear = '[UI] Toggle Academic Year',
+}
+
+export class ToggleAcademicYear implements Action {
+  public readonly type = UIActionTypes.ToggleAcademicYear;
+  constructor(public payload: { year: string }) {}
+}
diff --git a/src/app/degree-planner/store/effects/plan.effects.ts b/src/app/degree-planner/store/effects/plan.effects.ts
index 5c6d97f..c590aae 100644
--- a/src/app/degree-planner/store/effects/plan.effects.ts
+++ b/src/app/degree-planner/store/effects/plan.effects.ts
@@ -1,3 +1,4 @@
+import { tap } from 'rxjs/operators';
 // Libraries
 import { Injectable } from '@angular/core';
 import { ROOT_EFFECTS_INIT, Actions, Effect, ofType } from '@ngrx/effects';
@@ -81,6 +82,18 @@ export class DegreePlanEffects {
       });
     }),
 
+    map(payload => {
+      const allTerms = payload.visibleTerms.map(term => term.termCode);
+      const currentIndex = allTerms.indexOf(payload.activeTermCodes[0]);
+      const expandedTerms = allTerms.slice(currentIndex - allTerms.length);
+      const expandedYearsDups = expandedTerms.map(term => term.substr(1, 2));
+      const expandedYears = expandedYearsDups.filter(function(item, pos, self) {
+        return self.indexOf(item) === pos;
+      });
+
+      return { ...payload, expandedYears };
+    }),
+
     map(payload => new InitialLoadSuccess(payload)),
 
     catchError(error => {
@@ -278,3 +291,7 @@ const pickPrimaryDegreePlan = (plans: DegreePlan[]): DegreePlan => {
   const primary = plans.find(plan => plan.primary);
   return primary ? primary : plans[0];
 };
+
+const checkExpanded = (activeTermCodes, visibleTerms) => {
+  console.log(visibleTerms);
+};
diff --git a/src/app/degree-planner/store/reducer.ts b/src/app/degree-planner/store/reducer.ts
index 5927ac1..08e3c46 100644
--- a/src/app/degree-planner/store/reducer.ts
+++ b/src/app/degree-planner/store/reducer.ts
@@ -1,3 +1,4 @@
+import { UIActionTypes } from './actions/ui.actions';
 import {
   DegreePlannerState,
   INITIAL_DEGREE_PLANNER_STATE,
@@ -29,6 +30,7 @@ import {
   AddAcademicYearActionTypes,
   AddAcademicYearRequest,
 } from '@app/degree-planner/store/actions/addAcademicYear.actions';
+import { ToggleAcademicYear } from '@app/degree-planner/store/actions/ui.actions';
 import { SavedForLaterCourse } from '@app/core/models/saved-for-later-course';
 import { DegreePlan } from '@app/core/models/degree-plan';
 
@@ -47,7 +49,8 @@ type SupportedActions =
   | MakePlanPrimarySuccess
   | MakePlanPrimaryFailure
   | ChangePlanNameSuccess
-  | ChangePlanNameFailure;
+  | ChangePlanNameFailure
+  | ToggleAcademicYear;
 
 export function degreePlannerReducer(
   state = INITIAL_DEGREE_PLANNER_STATE,
@@ -93,6 +96,13 @@ export function degreePlannerReducer(
       return { ...state, visibleTerms: newVisibleTerms };
     }
 
+    /**
+     * The `ToggleAcademicYear` action toggles the expand/collapse UI state
+     */
+    case UIActionTypes.ToggleAcademicYear: {
+      return { ...state };
+    }
+
     /**
      * The `WriteNoteResponse` action is dispatched by the `Note.write$` effect
      * upon a successful response from the `updateNote` or `createNote` API
diff --git a/src/app/degree-planner/store/selectors.ts b/src/app/degree-planner/store/selectors.ts
index 0ff6c63..bbfa157 100644
--- a/src/app/degree-planner/store/selectors.ts
+++ b/src/app/degree-planner/store/selectors.ts
@@ -76,6 +76,8 @@ export const getAllVisibleTermsByYear = createSelector(
         fall: getTermForCode(`${year}2`, state.visibleTerms),
         spring: getTermForCode(`${year}4`, state.visibleTerms),
         summer: getTermForCode(`${year}6`, state.visibleTerms),
+        expandedState:
+          state.expandedYears.indexOf(twoDigitYearCode.toString()) > -1,
       };
     });
   },
diff --git a/src/app/degree-planner/store/state.ts b/src/app/degree-planner/store/state.ts
index 9cc6a2a..89d4062 100644
--- a/src/app/degree-planner/store/state.ts
+++ b/src/app/degree-planner/store/state.ts
@@ -11,6 +11,7 @@ export interface DegreePlannerState {
   activeTermCodes: string[];
   allDegreePlans: DegreePlan[];
   subjects: SubjectMapping;
+  expandedYears: string[];
 }
 
 export const INITIAL_DEGREE_PLANNER_STATE: DegreePlannerState = {
@@ -20,4 +21,5 @@ export const INITIAL_DEGREE_PLANNER_STATE: DegreePlannerState = {
   activeTermCodes: [],
   allDegreePlans: [],
   subjects: {},
+  expandedYears: [],
 };
-- 
GitLab