From 28ad769a714bb43b6b8db0a3cb7c376633d12258 Mon Sep 17 00:00:00 2001 From: Lloyd Wheeler Date: Tue, 28 Nov 2017 11:19:05 -0800 Subject: [PATCH] StudentSupportForm: Adding ui tracking to state #1450 --- .../instructionalSupportApp.js | 7 +- .../services/studentActions.js | 98 ++++++++++ .../services/studentReducers.js | 173 ++++++++++++++++++ .../services/studentService.js | 77 ++++++++ 4 files changed, 354 insertions(+), 1 deletion(-) create mode 100644 app/instructionalSupport/studentSupportCallForm/services/studentActions.js create mode 100644 app/instructionalSupport/studentSupportCallForm/services/studentReducers.js create mode 100644 app/instructionalSupport/studentSupportCallForm/services/studentService.js diff --git a/app/instructionalSupport/instructionalSupportApp.js b/app/instructionalSupport/instructionalSupportApp.js index 7f2213c2a..d43df9b17 100644 --- a/app/instructionalSupport/instructionalSupportApp.js +++ b/app/instructionalSupport/instructionalSupportApp.js @@ -43,6 +43,7 @@ instructionalSupportApp.config(function ($routeProvider) { }); }); +// UNSORTED ACTIONS var INIT_STATE = "INIT_STATE"; var ADD_ASSIGNMENT_SLOTS = "ADD_ASSIGNMENT_SLOTS"; var TOGGLE_ASSIGNMENT_PIVOT_VIEW = "TOGGLE_ASSIGNMENT_PIVOT_VIEW"; @@ -67,4 +68,8 @@ var UPDATE_TABLE_FILTER = "UPDATE_TABLE_FILTER"; var UPDATE_INSTRUCTOR_SUPPORT_CALL_REVIEW = "UPDATE_INSTRUCTOR_SUPPORT_CALL_REVIEW"; var UPDATE_SUPPORT_STAFF_SUPPORT_CALL_REVIEW = "UPDATE_SUPPORT_STAFF_SUPPORT_CALL_REVIEW"; var UPDATE_PREFERENCE = "UPDATE_PREFERENCE"; -var ASSIGN_STAFF_TO_SECTION_GROUP_SLOT = "ASSIGN_STAFF_TO_SECTION_GROUP_SLOT"; \ No newline at end of file +var ASSIGN_STAFF_TO_SECTION_GROUP_SLOT = "ASSIGN_STAFF_TO_SECTION_GROUP_SLOT"; + +// Student Support Call Form +var OPEN_PREFERENCE_COMMENT_MODAL = "OPEN_PREFERENCE_COMMENT_MODAL"; +var CLOSE_PREFERENCE_COMMENT_MODAL = "CLOSE_PREFERENCE_COMMENT_MODAL"; \ No newline at end of file diff --git a/app/instructionalSupport/studentSupportCallForm/services/studentActions.js b/app/instructionalSupport/studentSupportCallForm/services/studentActions.js new file mode 100644 index 000000000..123196908 --- /dev/null +++ b/app/instructionalSupport/studentSupportCallForm/services/studentActions.js @@ -0,0 +1,98 @@ +instructionalSupportApp.service('supportStaffFormActionCreators', function ($rootScope, $window, studentService, studentReducers) { + return { + getInitialState: function (workgroupId, year, termShortCode) { + studentService.getInitialState(workgroupId, year, termShortCode).then(function (payload) { + var action = { + type: INIT_STATE, + payload: payload, + year: year + }; + studentReducers.reduce(action); + }, function (err) { + $rootScope.$emit('toast', { message: "Could not load support staff form initial state.", type: "ERROR" }); + }); + }, + addStudentPreference: function (preference) { + studentService.addStudentPreference(preference).then(function (payload) { + $rootScope.$emit('toast', { message: "Added Preference", type: "SUCCESS" }); + var action = { + type: ADD_STUDENT_PREFERENCE, + payload: payload + }; + studentReducers.reduce(action); + }, function (err) { + $rootScope.$emit('toast', { message: "Could not add preference.", type: "ERROR" }); + }); + }, + updateSupportCallResponse: function (supportCallResponse) { + studentService.updateSupportCallResponse(supportCallResponse).then(function (payload) { + $rootScope.$emit('toast', { message: "Updated preferences", type: "SUCCESS" }); + var action = { + type: UPDATE_SUPPORT_CALL_RESPONSE, + payload: payload + }; + studentReducers.reduce(action); + }, function (err) { + $rootScope.$emit('toast', { message: "Could not update preferences.", type: "ERROR" }); + }); + }, + deleteStudentPreference: function (preference) { + studentService.deleteStudentPreference(preference.id).then(function (payload) { + $rootScope.$emit('toast', { message: "Removed Preference", type: "SUCCESS" }); + var action = { + type: DELETE_STUDENT_PREFERENCE, + payload: preference + }; + studentReducers.reduce(action); + }, function (err) { + $rootScope.$emit('toast', { message: "Could not remove preference.", type: "ERROR" }); + }); + }, + submitPreferences: function (supportCallResponse, workgroupId, year) { + studentService.updateSupportCallResponse(supportCallResponse).then(function (payload) { + $rootScope.$emit('toast', { message: "Updated preferences", type: "SUCCESS" }); + var studentSummaryUrl = "/summary/" + workgroupId + "/" + year + "?mode=instructionalSupport"; + $window.location.href = studentSummaryUrl; + }, function (err) { + $rootScope.$emit('toast', { message: "Could not update preferences.", type: "ERROR" }); + }); + }, + updatePreferencesOrder: function (preferenceIds, scheduleId, termCode) { + studentService.updatePreferencesOrder(preferenceIds, scheduleId, termCode).then(function (payload) { + $rootScope.$emit('toast', { message: "Updated preferences", type: "SUCCESS" }); + var action = { + type: UPDATE_PREFERENCES_ORDER, + payload: payload + }; + studentReducers.reduce(action); + }, function (err) { + $rootScope.$emit('toast', { message: "Could not update preference order.", type: "ERROR" }); + }); + }, + updatePreference: function (scheduleId, preference) { + studentService.updatePreference(scheduleId, preference).then(function (payload) { + $rootScope.$emit('toast', { message: "Updated preference comments", type: "SUCCESS" }); + var action = { + type: UPDATE_PREFERENCE, + payload: payload + }; + studentReducers.reduce(action); + }, function (err) { + $rootScope.$emit('toast', { message: "Could not update preference comments.", type: "ERROR" }); + }); + }, + pretendToastMessage: function () { + $rootScope.$emit('toast', { message: "Updated preferences", type: "SUCCESS" }); + }, + openPreferenceCommentsModal: function() { + studentReducers.reduce({ + type: OPEN_PREFERENCE_COMMENT_MODAL + }); + }, + closePreferenceCommentsModal: function() { + studentReducers.reduce({ + type: CLOSE_PREFERENCE_COMMENT_MODAL + }); + } + }; +}); \ No newline at end of file diff --git a/app/instructionalSupport/studentSupportCallForm/services/studentReducers.js b/app/instructionalSupport/studentSupportCallForm/services/studentReducers.js new file mode 100644 index 000000000..2de2cb92b --- /dev/null +++ b/app/instructionalSupport/studentSupportCallForm/services/studentReducers.js @@ -0,0 +1,173 @@ +instructionalSupportApp.service('studentReducers', function ($rootScope, $log, supportStaffFormSelectors) { + return { + _state: {}, + _sectionGroupReducers: function (action, sectionGroups) { + switch (action.type) { + case INIT_STATE: + sectionGroups = { + ids: [], + list: {} + }; + action.payload.sectionGroups.forEach( function(sectionGroup) { + sectionGroups.ids.push(sectionGroup.id); + sectionGroups.list[sectionGroup.id] = sectionGroup; + }); + return sectionGroups; + default: + return sectionGroups; + } + }, + _courseReducers: function (action, courses) { + switch (action.type) { + case INIT_STATE: + courses = { + ids: [], + list: [] + }; + action.payload.courses.forEach( function(course) { + courses.ids.push(course.id); + courses.list[course.id] = course; + }); + return courses; + default: + return courses; + } + }, + _preferenceReducers: function (action, preferences) { + switch (action.type) { + case INIT_STATE: + preferences = { + ids: [], + list: [] + }; + action.payload.studentSupportPreferences.forEach( function(preference) { + preferences.ids.push(preference.id); + preferences.list[preference.id] = preference; + }); + return preferences; + case UPDATE_PREFERENCES_ORDER: + for (var i = 0; i < action.payload.length; i++) { + var preferenceId = action.payload[i]; + var priority = i + 1; + preferences.list[preferenceId].priority = priority; + } + return preferences; + case ADD_STUDENT_PREFERENCE: + var preference = action.payload; + preferences.ids.push(preference.id); + preferences.list[preference.id] = preference; + return preferences; + case DELETE_STUDENT_PREFERENCE: + var preference = action.payload; + var index = preferences.ids.indexOf(preference.id); + preferences.ids.splice(index, 1); + var priority = preference.priority; + preferences.ids.forEach(function(slotPreferenceId) { + var slotPreference = preferences.list[slotPreferenceId]; + if (slotPreference.priority > priority) { + slotPreference.priority--; + } + }); + return preferences; + default: + return preferences; + } + }, + _supportAssignmentReducers: function (action, supportAssignments) { + switch (action.type) { + case INIT_STATE: + supportAssignments = { + ids: [], + list: [] + }; + action.payload.supportAssignments.forEach( function(supportAssignment) { + supportAssignments.ids.push(supportAssignment.id); + supportAssignments.list[supportAssignment.id] = supportAssignment; + }); + return supportAssignments; + default: + return supportAssignments; + } + }, + _miscReducers: function (action, misc) { + switch (action.type) { + case INIT_STATE: + misc = {}; + misc.scheduleId = action.payload.scheduleId; + misc.supportStaffId = action.payload.supportStaffId; + return misc; + default: + return misc; + } + }, + _uiReducers: function (action, ui) { + switch (action.type) { + case INIT_STATE: + ui = { + isPreferenceCommentModalOpen: false + }; + + case OPEN_PREFERENCE_COMMENT_MODAL: + ui.isPreferenceCommentModalOpen = true; + return ui; + case CLOSE_PREFERENCE_COMMENT_MODAL: + ui.isPreferenceCommentModalOpen = false; + return ui; + default: + return ui; + } + }, + _supportCallResponseReducers: function (action, supportCallResponse) { + switch (action.type) { + case INIT_STATE: + supportCallResponse = action.payload.studentSupportCallResponse; + if (!supportCallResponse) { + return null; + } + supportCallResponse.dueDateDescription = millisecondsToDate(supportCallResponse.dueDate); + return supportCallResponse; + case UPDATE_SUPPORT_CALL_RESPONSE: + supportCallResponse = action.payload; + supportCallResponse.dueDateDescription = millisecondsToDate(supportCallResponse.dueDate); + return supportCallResponse; + default: + return supportCallResponse; + } + }, + reduce: function (action) { + newState = {}; + newState.courses = scope._courseReducers(action, scope._state.courses); + newState.sectionGroups = scope._sectionGroupReducers(action, scope._state.sectionGroups); + newState.supportAssignments = scope._supportAssignmentReducers(action, scope._state.supportAssignments); + newState.misc = scope._miscReducers(action, scope._state.misc); + newState.preferences = scope._preferenceReducers(action, scope._state.preferences); + newState.supportCallResponse = scope._supportCallResponseReducers(action, scope._state.supportCallResponse); + newState.ui = scope._uiReducers(action, scope._state.ui); + + scope._state = newState; + + // Build new 'page state' + // This is the 'view friendly' version of the store + newPageState = {}; + newPageState.supportCallResponse = angular.copy(scope._state.supportCallResponse); + newPageState.misc = angular.copy(scope._state.misc); + newPageState.supportAssignments = angular.copy(scope._state.supportAssignments); + + newPageState.preferences = supportStaffFormSelectors.generatePreferences( + scope._state.preferences, + scope._state.courses, + scope._state.sectionGroups + ); + + newPageState.potentialPreferences = supportStaffFormSelectors.generatePotentialPreferences( + scope._state.supportAssignments, + scope._state.courses, + scope._state.sectionGroups, + scope._state.preferences, + scope._state.supportCallResponse + ); + + $rootScope.$emit('studentStateChanged', newPageState); + } + }; +}); \ No newline at end of file diff --git a/app/instructionalSupport/studentSupportCallForm/services/studentService.js b/app/instructionalSupport/studentSupportCallForm/services/studentService.js new file mode 100644 index 000000000..25ac01e91 --- /dev/null +++ b/app/instructionalSupport/studentSupportCallForm/services/studentService.js @@ -0,0 +1,77 @@ +instructionalSupportApp.factory("studentService", this.studentService = function($http, $q, $window) { + return { + getInitialState: function(workgroupId, year, termShortCode) { + var deferred = $q.defer(); + + $http.get(serverRoot + "/api/instructionalSupportStudentFormView/workgroups/" + workgroupId + "/years/" + year + "/termCode/" + termShortCode, { withCredentials: true }) + .success(function(assignmentView) { + deferred.resolve(assignmentView); + }) + .error(function() { + deferred.reject(); + }); + + return deferred.promise; + }, + addStudentPreference: function(preference) { + var deferred = $q.defer(); + $http.post(serverRoot + "/api/instructionalSupportStudentFormView/sectionGroups/" + preference.sectionGroupId + "/preferenceType/" + preference.appointmentType + "/percentage/" + preference.appointmentPercentage, { withCredentials: true }) + .success(function(assignmentView) { + deferred.resolve(assignmentView); + }) + .error(function() { + deferred.reject(); + }); + + return deferred.promise; + }, + updateSupportCallResponse: function(supportCallResponse) { + var deferred = $q.defer(); + $http.put(serverRoot + "/api/instructionalSupportStudentFormView/studentSupportCallResponses/" + supportCallResponse.id, supportCallResponse, { withCredentials: true }) + .success(function(assignmentView) { + deferred.resolve(assignmentView); + }) + .error(function() { + deferred.reject(); + }); + + return deferred.promise; + }, + updatePreferencesOrder: function(preferenceIds, scheduleId, termCode) { + var deferred = $q.defer(); + $http.put(serverRoot + "/api/instructionalSupportStudentFormView/schedules/" + scheduleId + "/terms/" + termCode, preferenceIds, { withCredentials: true }) + .success(function(payload) { + deferred.resolve(payload); + }) + .error(function() { + deferred.reject(); + }); + + return deferred.promise; + }, + updatePreference: function(scheduleId, preference) { + var deferred = $q.defer(); + $http.put(serverRoot + "/api/instructionalSupportStudentFormView/schedules/" + scheduleId + "/preferences/" + preference.id, preference, { withCredentials: true }) + .success(function(payload) { + deferred.resolve(payload); + }) + .error(function() { + deferred.reject(); + }); + + return deferred.promise; + }, + deleteStudentPreference: function(preferenceId) { + var deferred = $q.defer(); + $http.delete(serverRoot + "/api/instructionalSupportStudentFormView/studentInstructionalSupportPreferences/" + preferenceId, { withCredentials: true }) + .success(function(payload) { + deferred.resolve(payload); + }) + .error(function() { + deferred.reject(); + }); + + return deferred.promise; + } + }; +});