UNPKG

trc-client-core

Version:
169 lines (151 loc) 5.97 kB
var _ = require('lodash'); var Reflux = require('reflux'); var ImmutableStoreMixin = require('reflux-immutable/ImmutableStoreMixin'); var ReportActions = require('trc-client-core/src/report/ReportActions'); var TechnicalReportStore = Reflux.createStore({ listenables: ReportActions, mixins: [ImmutableStoreMixin], init: function () { this.setState({ classSizes: {}, classSizesFetching: true, classSizesAccumulative: {}, classSizesAccumulativeFetching: true, streamCertification: {}, streamCertificationFetching: true, streamEligibility: {}, streamEligibilityFetching: true, trainingDelivery: {}, trainingDeliveryFetching: true }); }, substringThreeChars: function(str) { return str.substring(0, 3); }, transformToState: function(map, dataObject, keyParse) { var newState = {}; var max = 0; // loop through each state option _.forEach(map, function(chosenKeys, key) { newState[key] = _.map(dataObject, function (data, subkey) { var series = _.map(chosenKeys, function(keyName){ var value = data[keyName] || 0; max = Math.max(max, value); // round to max 2 places return Math.round(value * 100) / 100; }); var keySting = (keyParse) ? keyParse(subkey) : subkey; return {series: series, xlabel: keySting}; }); }); newState.chartMax = max; return newState; }, // // Training Delivery onFetchTrainingDelivery: function () { this.setState({trainingDeliveryFetching: true}); }, onFetchTrainingDeliveryCompleted: function(data) { this.setState({ trainingDeliveryFetching: false, trainingDelivery: { trainingHours: this.trainingDeliveryShape('TrainingHours', data), trainingDays: this.trainingDeliveryShape('TrainingDays', data), days: { actual: this.transformToState({ graph: ['plannedDays'], benchmark: ['bmDays'], table: ['plannedDays', 'bmDays'] }, data, this.substringThreeChars), accumulate: this.transformToState({ graph: ['plannedAccuDays'], benchmark: ['bmAccuDays'], table: ['plannedAccuDays', 'bmAccuDays'] }, data, this.substringThreeChars) } } }); }, trainingDeliveryShape: function (key, data) { return { actual: this.transformToState({ graph: ['planned' + key, 'enrol' + key, 'actual' + key].reverse(), benchmark: ['bm' + key], table: ['planned' + key, 'enrol' + key, 'actual' + key, 'bm' + key] }, data, this.substringThreeChars), accumulate: this.transformToState({ graph: ['plannedAccu' + key, 'enrolAccu' + key, 'actualAccu' + key].reverse(), benchmark: ['bmAccu' + key], table: ['plannedAccu' + key, 'enrolAccu' + key, 'actualAccu' + key, 'bmAccu' + key] }, data, this.substringThreeChars) }; }, // // Class Sizes onFetchClassSizes: function () { this.setState({classSizesFetching: true}); }, onFetchClassSizesCompleted: function(data) { this.setState({ classSizesFetching: false, classSizes: this.classSizesShape(data) }); }, onFetchClassSizesAccumulative: function () { this.setState({classSizesAccumulativeFetching: true}); }, onFetchClassSizesAccumulativeCompleted: function(data) { this.setState({ classSizesAccumulativeFetching: false, classSizesAccumulative: this.classSizesShape(data) }); }, classSizesShape: function(data) { return this.transformToState({ classSizes: ['complete', 'enrolled'], classSizesBenchmark: ['benchmark'], classSizesTable: ['benchmark', 'complete', 'enrolled', 'gap'] }, data, this.substringThreeChars); }, // // Stream Certification onFetchStreamCertification: function () { this.setState({streamCertificationFetching: true}); }, onFetchStreamCertificationCompleted: function(data) { var dataObject = data; var keys = _.keys(data); if(keys.length === 1) { dataObject = data[keys[0]].regions; } this.setState({ streamCertificationFetching: false, streamCertification: this.transformToState({ streamCertification: ['achieved', 'eligible'], streamCertificationTotal: ['total'], streamCertificationBenchmark: ['benchmark'], streamCertificationTable: ['achieved', 'eligible', 'total', 'benchmark', 'gap'] }, dataObject) }); }, // // Stream Eligibility onFetchStreamEligibility: function () { this.setState({streamEligibilityFetching: true}); }, onFetchStreamEligibilityCompleted: function(data) { var prerequisite = this.streamEligibilityShape(data.prerequisite); var techCourses = this.streamEligibilityShape(data.techCourses); this.setState({ streamEligibilityFetching: false, streamEligibility: prerequisite.concat(techCourses) }); }, streamEligibilityShape: function (data) { return _.map(data, function(value, key) { return {xlabel: key, series: [value.completed, value.eligible]}; }); } }); module.exports = TechnicalReportStore;