Skip to content
Snippets Groups Projects
Commit 6ca0121e authored by Isaac Evavold's avatar Isaac Evavold
Browse files

update state when course moved inside same term

parent f5005a0f
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ export enum CourseActionTypes { ...@@ -8,6 +8,7 @@ export enum CourseActionTypes {
RemoveCourse = '[Course] Remove', RemoveCourse = '[Course] Remove',
RemoveCourseSuccess = '[Course] Remove (Success)', RemoveCourseSuccess = '[Course] Remove (Success)',
MoveCourseInsideTerm = '[Course] Move Inside Term',
MoveCourseBetweenTerms = '[Course] Move Between Terms', MoveCourseBetweenTerms = '[Course] Move Between Terms',
MoveCourseBetweenTermsSuccess = '[Course] Move Between Terms (Success)', MoveCourseBetweenTermsSuccess = '[Course] Move Between Terms (Success)',
...@@ -23,6 +24,13 @@ export enum CourseActionTypes { ...@@ -23,6 +24,13 @@ export enum CourseActionTypes {
CourseError = '[Course] Error', CourseError = '[Course] Error',
} }
export class MoveCourseInsideTerm implements Action {
public readonly type = CourseActionTypes.MoveCourseInsideTerm;
constructor(
public payload: { termCode: string; recordId: number; newIndex: number },
) {}
}
export class MoveCourseBetweenTerms implements Action { export class MoveCourseBetweenTerms implements Action {
public readonly type = CourseActionTypes.MoveCourseBetweenTerms; public readonly type = CourseActionTypes.MoveCourseBetweenTerms;
constructor( constructor(
......
...@@ -17,6 +17,7 @@ import { ...@@ -17,6 +17,7 @@ import {
DeletePlanSuccess, DeletePlanSuccess,
} from '@app/degree-planner/store/actions/plan.actions'; } from '@app/degree-planner/store/actions/plan.actions';
import { import {
MoveCourseInsideTerm,
CourseActionTypes, CourseActionTypes,
RemoveCourse, RemoveCourse,
MoveCourseBetweenTerms, MoveCourseBetweenTerms,
...@@ -53,6 +54,7 @@ type SupportedActions = ...@@ -53,6 +54,7 @@ type SupportedActions =
| SwitchPlanSuccess | SwitchPlanSuccess
| WriteNoteSuccess | WriteNoteSuccess
| DeleteNoteSuccess | DeleteNoteSuccess
| MoveCourseInsideTerm
| MoveCourseBetweenTerms | MoveCourseBetweenTerms
| RemoveCourse | RemoveCourse
| AddCourseSuccess | AddCourseSuccess
...@@ -207,6 +209,38 @@ export function degreePlannerReducer( ...@@ -207,6 +209,38 @@ export function degreePlannerReducer(
return { ...state, visibleYears }; return { ...state, visibleYears };
} }
case CourseActionTypes.MoveCourseInsideTerm: {
const { termCode, recordId, newIndex } = action.payload;
const { yearCode, termName } = parseTermCode(termCode);
const year = state.visibleYears[yearCode];
if (year) {
const courses = year[termName].courses;
const course = courses.find(course => course.id === recordId);
const oldIndex = courses.findIndex(course => course.id === recordId);
if (course) {
const newCourses = courses.slice();
newCourses.splice(oldIndex, 1);
newCourses.splice(newIndex, 0, course);
const visibleYears = {
...state.visibleYears,
[yearCode]: {
...state.visibleYears[yearCode],
[termName]: {
...state.visibleYears[yearCode][termName],
courses: newCourses,
},
},
};
return { ...state, visibleYears };
}
}
return state;
}
case CourseActionTypes.MoveCourseBetweenTerms: { case CourseActionTypes.MoveCourseBetweenTerms: {
const { const {
to: toTermCode, to: toTermCode,
......
// Libraries
import { Component, Input, OnInit } from '@angular/core'; import { Component, Input, OnInit } from '@angular/core';
import { import { CdkDragDrop } from '@angular/cdk/drag-drop';
CdkDragDrop,
moveItemInArray,
transferArrayItem,
} from '@angular/cdk/drag-drop';
import { MatDialog } from '@angular/material'; import { MatDialog } from '@angular/material';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { filter, map, distinctUntilChanged } from 'rxjs/operators'; import { filter, map, distinctUntilChanged } from 'rxjs/operators';
import { Store, select } from '@ngrx/store'; import { Store, select } from '@ngrx/store';
import { DegreePlannerState } from '@app/degree-planner/store/state'; import { DegreePlannerState } from '@app/degree-planner/store/state';
import { import { ToggleCourseSearch } from '@app/degree-planner/store/actions/ui.actions';
MoveCourseBetweenTerms,
AddCourse,
RemoveSaveForLater,
} from '@app/degree-planner/store/actions/course.actions';
import {
ToggleCourseSearch,
OpenCourseSearch,
CloseCourseSearch,
} from '@app/degree-planner/store/actions/ui.actions';
// Services
import { SidenavService } from './../../core/service/sidenav.service';
// Models // Models
import * as actions from '@app/degree-planner/store/actions/course.actions'; import * as actions from '@app/degree-planner/store/actions/course.actions';
...@@ -127,24 +109,29 @@ export class TermContainerComponent implements OnInit { ...@@ -127,24 +109,29 @@ export class TermContainerComponent implements OnInit {
this.store.dispatch(new ToggleCourseSearch()); this.store.dispatch(new ToggleCourseSearch());
} }
drop(event: CdkDragDrop<string[]>) { drop(event: CdkDragDrop<string>) {
const newContainer = event.container.id; const newContainer = event.container.id;
const previousContainer = event.previousContainer.id; const previousContainer = event.previousContainer.id;
if (newContainer === previousContainer) { if (newContainer === previousContainer) {
// If the user dropped a course into the same container do nothing const newIndex = event.currentIndex;
moveItemInArray( const { id: recordId, termCode } = event.item.data as Course;
event.container.data,
event.previousIndex, if (recordId !== null) {
event.currentIndex, const action = new actions.MoveCourseInsideTerm({
); termCode,
return; recordId,
newIndex,
});
this.store.dispatch(action);
}
} else if (previousContainer.indexOf('term-') === 0) { } else if (previousContainer.indexOf('term-') === 0) {
// If moving from term to term // If moving from term to term
// Get the pervious and new term code, and the record ID // Get the pervious and new term code, and the record ID
const to = newContainer.substr(5); const from = event.previousContainer.data;
const { termCode: from, id } = event.item.data; const to = event.container.data;
const { id } = event.item.data;
const newIndex = event.currentIndex; const newIndex = event.currentIndex;
// Dispatch a new change request // Dispatch a new change request
......
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