UNPKG

trc-client-core

Version:
143 lines (132 loc) 4.45 kB
import Immutable from 'immutable'; import xhr from '../utils/xhr'; import reflux from 'reflux'; import _ from 'lodash'; import ImmutableStoreMixin from 'reflux-immutable/ImmutableStoreMixin'; import SodataActions from 'trc-client-core/src/sodata/SodataActions'; import CourseActions from 'trc-client-core/src/course/CourseActions'; import RegistrationActions from 'trc-client-core/src/course/RegistrationActions'; var CourseStore = reflux.createStore({ listenables: [ CourseActions, RegistrationActions ], mixins: [ ImmutableStoreMixin ], init: function () { this.setState({ courses: [], course: null, similarCourses: null }); }, onPublishCompleted: function () { CourseActions.fetchCourses(); }, onFetchCoursesCompleted(courses) { this.setState({ courses: courses }); }, onFetchCourse: function () { this.setState({ course: null }) }, onFetchCourseFailed: function () {}, onFetchCourseCompleted: function(course) { this.setState({ course: course }); }, onFetchParticipation: function() { this.setState({ participation: null }); }, onFetchParticipationCompleted: function(participation) { var newState = participation; newState.history = Immutable.fromJS(participation.history) .sortBy(function(item){ return item.date; }) .reverse() .filter(function(item, key){ return item.process !== 'ENROLLED' || key !== 0; }); if(newState.history.get(0) && newState.history.get(0).process === 'ENROLLED') { SodataActions.fetchSoData(newState.history.get(0).soId); } this.setState({ participation: newState }); }, onFetchSimilarCoursesCompleted: function(similarCourses) { this.setState({ similarCourses: similarCourses }); }, // From Registration Store onSendRegistrationData() {}, onSendRegistrationDataFailed() {}, onSendRegistrationDataCompleted() { var participation = this.get('participation'); if (participation) { CourseActions.fetchParticipation({ courseCode: participation.get('courseCode'), participantId: participation.get('participantId'), withEligibility: true }); } }, filterCourses: function(query) { return function(course) { if(query.type !== 'ALL' && query.type !== course.type && course.type) { return false; } else if(query.stream !== 'ALL' && query.stream !== course.stream && course.stream) { return false; } else if (query.departmentCategory !== 'ALL' && !_.contains(course.departmentCategory, query.departmentCategory)) { return false; } else if(query.search) { // generate string from keys var string = _(course).pick(['workshopName', 'courseCode']).invert().keys().toString().toLowerCase(); return (string.indexOf(query.search.toLowerCase()) !== -1); } return true; }; }, course: { get: function (query) { if(query.id) { return xhr.get('/api/course/' + query.id); } }, put: function (query) { if(query.id) { return xhr.put('/api/course/' + query.id, this.parseCourse(query)); } }, post: function (query) { return xhr.post('/api/course/', this.parseCourse(query)); }, del: function (id) { return xhr.del('/api/course/' + id); }, parseCourse: function(query) { if(query.price) { query.price = parseFloat(query.price, 10); } if(query.prerequisiteExpression) { delete query.prerequisiteExpression; } return query; }, completionData: function(query) { if(query) { return xhr.get('/course/completion/participant/' + query.participantId + '/course/' + query.courseCode); } } } }); module.exports = CourseStore;