UNPKG

trc-client-core

Version:
223 lines (187 loc) 8.13 kB
import reflux from 'reflux'; import Immutable from 'immutable'; import RouterContainer from 'trc-client-core/src/global/RouterContainer'; import RegistrationActions from 'trc-client-core/src/course/RegistrationActions'; import UserStore from 'trc-client-core/src/user/UserStore'; import LoadingActions from 'trc-client-core/src/global/LoadingActions'; import NavigationStore from 'trc-client-core/src/global/NavigationStore'; import history from 'trc-client-core/src/global/history'; import React from 'react'; function registrationShape(participant, soId, status, regId) { return Immutable.Map({ participantId: participant.participantId, soId: soId, regId: regId, status: status }); } var RegistrationStore = reflux.createStore({ listenables: RegistrationActions, localStorageKey: 'Registration', contextTypes: { history: React.PropTypes.history, }, mixins: [ require('reflux-immutable/ImmutableStoreMixin') ], init: function () { this.setState({ registrations: null, cancellations: {}, candidates: null, error: null, soData: null, successfulRegistrations: null }); }, onFetchRegistrationDataFailed: function () { this.setState({ error: UserStore.get('username') + ' cannot enrol staff.' }); }, onFetchRegistrationDataCompleted: function (data) { var registrations = {}; var soData = {}; data.forEach(function(item){ soData[item.soData.soId] = item.soData; soData[item.soData.soId].otherEnrolledParticipants = item.soData.soNoParticipants - item.participantsData.length; if(item.participantsData.length) { registrations[item.soData.soId] = item.participantsData.map(function(pp){ return registrationShape(pp, item.soData.soId, pp.status, pp.regId); }); } }); this.setState({ registrations: registrations, soData: soData }); }, onFetchCandidatesCompleted: function (data) { data = data || []; var candidates = {}; data.forEach(function(pp) { candidates[pp.participantId] = pp; }); this.setState({ candidates: candidates }); }, getFlatListOfEnrollments() { // Colate registrations and cancellations return this.get('registrations').toList().flatten(true).concat(this.get('cancellations').toList().flatten(true)); }, onEnrol: function (formData) { // set the correct operation on each item // and apply unenroll messages var payload = this.getFlatListOfEnrollments() .filter(function(item){ // remove pre-exisitng enrolments var status = item.get('status'); return (status !== 'ENROLL' && status !== 'WAITLIST'); }) .map(function(item){ // item = item.set('operation', 'ENROLL'); switch (item.get('status')) { case 'UNENROLL': case 'UNWAITLIST': item = item.set('operation', 'CANCEL'); item = item.set('message', formData.reason); break; } return item; }); this.setState({ courseCode: formData.courseCode }); RegistrationActions.sendRegistrationData(payload.toJS()); }, onRequestTraining: function (managersParticipantId, soId) { RegistrationActions.sendRegistrationData([{ operation: 'REQUEST', participantId: managersParticipantId, soId: soId }]); }, onSendRegistrationData: function (registrations) { LoadingActions.startLoading(); }, onSendRegistrationDataFailed: function () { LoadingActions.stopLoading(); LoadingActions.flashMessage('failure long', 'Sending registrations failed', 5000); }, onSendRegistrationDataCompleted: function (data) { LoadingActions.stopLoading(); this.setState({ successfulRegistrations: data, registrations: null, cancellations: {} }); history.pushState(null, `/course/${this.state.get('courseCode')}/registration_results`); }, onAddRegistration: function(participantId, soId) { var id = soId.toString(); var participant = this.get('candidates').get(participantId).toJS(); var registrations = this.get('registrations').get(id) || Immutable.List(); this.setState({ registrations: this.get('registrations').set(id, registrations.push(registrationShape(participant, soId))) }); }, onUpdateRegistration: function (participantId, soId) { var id = soId.toString(); var participant = this.get('candidates').get(participantId).toJS(); var currentRegistrationList = this.get('registrations').get(id); var currentCancellationList = this.get('cancellations').get(id) || Immutable.List(); var status; var regId; var index = currentRegistrationList.findIndex(function(pp){ return pp.get('participantId') === participantId; }); if(index === -1) { // registration wasnt found in the registration list // recalc the index index = currentCancellationList.findIndex(function(pp){ return pp.get('participantId') === participantId; }); status = currentCancellationList.get(index).get('status'); regId = currentCancellationList.get(index).get('regId'); } else { status = currentRegistrationList.get(index).get('status'); regId = currentRegistrationList.get(index).get('regId'); } switch (status) { case 'ENROLL': // Unenroll this.setState({ cancellations: this.get('cancellations').set(id, currentCancellationList.push(registrationShape(participant, soId, 'UNENROLL', regId))), registrations: this.get('registrations').set(id, currentRegistrationList.delete(index)) }); break; case 'WAITLIST': // Remove waitlist this.setState({ cancellations: this.get('cancellations').set(id, currentCancellationList.push(registrationShape(participant, soId, 'UNWAITLIST', regId))), registrations: this.get('registrations').set(id, currentRegistrationList.delete(index)) }); break; case 'UNWAITLIST': // Re-waitlist (undo) this.setState({ registrations: this.get('registrations').set(id, currentRegistrationList.unshift(registrationShape(participant, soId, 'WAITLIST', regId))), cancellations: this.get('cancellations').set(id, currentCancellationList.delete(currentCancellationList.findIndex(function(pp){ return pp.get('participantId') === participantId; }))) }); break; case 'UNENROLL': // Re-enroll (undo) this.setState({ registrations: this.get('registrations').set(id, currentRegistrationList.unshift(registrationShape(participant, soId, 'ENROLL', regId))), cancellations: this.get('cancellations').set(id, currentCancellationList.delete(currentCancellationList.findIndex(function(pp){ return pp.get('participantId') === participantId; }))) }); break; default: // Delete Method this.setState({ registrations: this.get('registrations').set(id, currentRegistrationList.delete(index)) }); break; } } }); module.exports = RegistrationStore;