2017-02-11 20:39:38 -05:00
|
|
|
var assign = require('object-assign');
|
|
|
|
|
|
|
|
var ReportConstants = require('../constants/ReportConstants');
|
|
|
|
var UserConstants = require('../constants/UserConstants');
|
|
|
|
|
2017-06-17 14:24:02 -04:00
|
|
|
var models = require('../models.js');
|
|
|
|
var Tabulation = models.Tabulation;
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
const initialState = {
|
|
|
|
map: {},
|
|
|
|
tabulations: {},
|
|
|
|
list: [],
|
|
|
|
selected: -1,
|
|
|
|
selectedTabulation: null,
|
|
|
|
seriesTraversal: []
|
|
|
|
};
|
|
|
|
|
2017-06-17 14:24:02 -04:00
|
|
|
function getFlattenedTabulation(tabulation, seriesTraversal) {
|
|
|
|
// Descend the tree to the right series to flatten
|
|
|
|
var series = tabulation;
|
|
|
|
for (var i=0; i < seriesTraversal.length; i++) {
|
|
|
|
if (!series.Series.hasOwnProperty(seriesTraversal[i])) {
|
|
|
|
dispatch(ErrorActions.clientError("Invalid series"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
series = series.Series[seriesTraversal[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually flatten the data
|
|
|
|
var flattenedSeries = series.mapReduceChildren(null,
|
|
|
|
function(accumulator, currentValue, currentIndex, array) {
|
|
|
|
return accumulator + currentValue;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add back in any values from the current level
|
|
|
|
if (series.hasOwnProperty('Values'))
|
|
|
|
flattenedSeries[Tabulation.topLevelSeriesName()] = series.Values;
|
|
|
|
|
|
|
|
var flattenedTabulation = new Tabulation();
|
|
|
|
|
|
|
|
flattenedTabulation.ReportId = tabulation.ReportId;
|
|
|
|
flattenedTabulation.Title = tabulation.Title;
|
|
|
|
flattenedTabulation.Subtitle = tabulation.Subtitle;
|
|
|
|
flattenedTabulation.Units = tabulation.Units;
|
|
|
|
flattenedTabulation.Labels = tabulation.Labels.slice();
|
|
|
|
flattenedTabulation.FlattenedSeries = flattenedSeries;
|
|
|
|
|
|
|
|
return flattenedTabulation;
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
module.exports = function(state = initialState, action) {
|
2017-02-11 20:39:38 -05:00
|
|
|
switch (action.type) {
|
2017-06-16 20:55:22 -04:00
|
|
|
case ReportConstants.REPORTS_FETCHED:
|
|
|
|
var selected = -1;
|
|
|
|
var reports = {};
|
|
|
|
var list = [];
|
|
|
|
for (var i = 0; i < action.reports.length; i++) {
|
|
|
|
var report = action.reports[i];
|
|
|
|
reports[report.ReportId] = report;
|
2017-06-23 06:00:10 -04:00
|
|
|
list.push(report);
|
2017-06-16 20:55:22 -04:00
|
|
|
if (state.selected == report.ReportId)
|
|
|
|
selected = state.selected;
|
|
|
|
}
|
2017-02-11 20:39:38 -05:00
|
|
|
return assign({}, state, {
|
2017-06-16 20:55:22 -04:00
|
|
|
map: reports,
|
|
|
|
list: list,
|
|
|
|
tabulations: {},
|
|
|
|
selected: selected
|
|
|
|
});
|
|
|
|
case ReportConstants.REPORT_CREATED:
|
|
|
|
case ReportConstants.REPORT_UPDATED:
|
2017-06-17 14:24:02 -04:00
|
|
|
var selectedTabulation = state.selectedTabulation;
|
|
|
|
var seriesTraversal = state.seriesTraversal;
|
|
|
|
if (state.selected == action.report.ReportId) {
|
|
|
|
selectedTabulation = initialState.selectedTabulation;
|
|
|
|
seriesTraversal = initialState.seriesTraversal;
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:55:22 -04:00
|
|
|
var report = action.report;
|
|
|
|
var reports = assign({}, state.map, {
|
2017-02-11 20:39:38 -05:00
|
|
|
[report.ReportId]: report
|
|
|
|
});
|
2017-06-16 20:55:22 -04:00
|
|
|
|
|
|
|
var list = [];
|
|
|
|
for (var reportId in reports) {
|
|
|
|
if (reports.hasOwnProperty(reportId))
|
2017-06-23 06:10:35 -04:00
|
|
|
list.push(reports[reportId]);
|
2017-06-16 20:55:22 -04:00
|
|
|
}
|
|
|
|
return assign({}, state, {
|
|
|
|
map: reports,
|
2017-06-17 14:24:02 -04:00
|
|
|
list: list,
|
|
|
|
selectedTabulation: selectedTabulation,
|
|
|
|
seriesTraversal: seriesTraversal
|
2017-06-16 20:55:22 -04:00
|
|
|
});
|
|
|
|
case ReportConstants.REPORT_REMOVED:
|
|
|
|
var selected = state.selected;
|
|
|
|
if (action.reportId == selected)
|
|
|
|
selected = -1;
|
|
|
|
var reports = assign({}, state.map);
|
|
|
|
delete reports[action.reportId];
|
|
|
|
return assign({}, state, {
|
|
|
|
map: reports,
|
|
|
|
selected: selected
|
|
|
|
});
|
|
|
|
case ReportConstants.REPORT_SELECTED:
|
2017-06-17 14:24:02 -04:00
|
|
|
var selectedTabulation = null;
|
|
|
|
if (state.tabulations.hasOwnProperty(action.report.ReportId)) {
|
|
|
|
selectedTabulation = getFlattenedTabulation(state.tabulations[action.report.ReportId], initialState.seriesTraversal)
|
|
|
|
}
|
2017-06-16 20:55:22 -04:00
|
|
|
return assign({}, state, {
|
|
|
|
selected: action.report.ReportId,
|
2017-06-17 14:24:02 -04:00
|
|
|
selectedTabulation: selectedTabulation,
|
|
|
|
seriesTraversal: initialState.seriesTraversal
|
2017-06-16 20:55:22 -04:00
|
|
|
});
|
2017-06-17 10:28:50 -04:00
|
|
|
case ReportConstants.REPORT_TABULATED:
|
2017-06-16 20:55:22 -04:00
|
|
|
var tabulation = action.tabulation;
|
2017-06-17 10:28:50 -04:00
|
|
|
var tabulations = assign({}, state.tabulations, {
|
2017-06-16 20:55:22 -04:00
|
|
|
[tabulation.ReportId]: tabulation
|
|
|
|
});
|
2017-06-17 14:24:02 -04:00
|
|
|
var selectedTabulation = state.selectedTabulation;
|
|
|
|
var seriesTraversal = state.seriesTraversal;
|
|
|
|
if (tabulation.ReportId == state.selected) {
|
|
|
|
selectedTabulation = getFlattenedTabulation(tabulation, initialState.seriesTraversal)
|
|
|
|
seriesTraversal = initialState.seriesTraversal;
|
|
|
|
}
|
2017-06-17 10:28:50 -04:00
|
|
|
return assign({}, state, {
|
2017-06-17 14:24:02 -04:00
|
|
|
tabulations: tabulations,
|
|
|
|
selectedTabulation: selectedTabulation,
|
|
|
|
seriesTraversal: seriesTraversal
|
2017-06-17 10:28:50 -04:00
|
|
|
});
|
2017-06-16 20:55:22 -04:00
|
|
|
case ReportConstants.SERIES_SELECTED:
|
2017-06-17 10:28:50 -04:00
|
|
|
return assign({}, state, {
|
2017-06-17 14:24:02 -04:00
|
|
|
selectedTabulation: getFlattenedTabulation(state.tabulations[state.selected], action.seriesTraversal),
|
2017-06-16 20:55:22 -04:00
|
|
|
seriesTraversal: action.seriesTraversal
|
2017-06-17 10:28:50 -04:00
|
|
|
});
|
2017-02-11 20:39:38 -05:00
|
|
|
case UserConstants.USER_LOGGEDOUT:
|
2017-06-16 20:55:22 -04:00
|
|
|
return initialState;
|
2017-02-11 20:39:38 -05:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|