mirror of
				https://github.com/aclindsa/moneygo.git
				synced 2025-11-03 18:13:27 -05:00 
			
		
		
		
	reports: Move flattening tabulations into reducer
This removes some circular control dependencies
This commit is contained in:
		@@ -87,10 +87,9 @@ function selectionCleared() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function seriesSelected(flattenedTabulation, seriesTraversal) {
 | 
					function seriesSelected(seriesTraversal) {
 | 
				
			||||||
	return {
 | 
						return {
 | 
				
			||||||
		type: ReportConstants.SERIES_SELECTED,
 | 
							type: ReportConstants.SERIES_SELECTED,
 | 
				
			||||||
		tabulation: flattenedTabulation,
 | 
					 | 
				
			||||||
		seriesTraversal: seriesTraversal
 | 
							seriesTraversal: seriesTraversal
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -228,45 +227,6 @@ function tabulate(report) {
 | 
				
			|||||||
	};
 | 
						};
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function selectSeries(tabulation, seriesTraversal) {
 | 
					 | 
				
			||||||
	return function (dispatch) {
 | 
					 | 
				
			||||||
		if (!seriesTraversal)
 | 
					 | 
				
			||||||
			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;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		dispatch(seriesSelected(flattenedTabulation, seriesTraversal));
 | 
					 | 
				
			||||||
	};
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
	fetchAll: fetchAll,
 | 
						fetchAll: fetchAll,
 | 
				
			||||||
	create: create,
 | 
						create: create,
 | 
				
			||||||
@@ -274,5 +234,5 @@ module.exports = {
 | 
				
			|||||||
	remove: remove,
 | 
						remove: remove,
 | 
				
			||||||
	tabulate: tabulate,
 | 
						tabulate: tabulate,
 | 
				
			||||||
	select: reportSelected,
 | 
						select: reportSelected,
 | 
				
			||||||
	selectSeries: selectSeries
 | 
						selectSeries: seriesSelected
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -156,10 +156,6 @@ class ReportsTab extends React.Component {
 | 
				
			|||||||
				nextProps.onTabulateReport(nextProps.reports.map[nextProps.reports.list[0]]);
 | 
									nextProps.onTabulateReport(nextProps.reports.map[nextProps.reports.list[0]]);
 | 
				
			||||||
				this.setState({initialized: true});
 | 
									this.setState({initialized: true});
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else if (selected != -1 &&
 | 
					 | 
				
			||||||
				nextProps.reports.tabulations.hasOwnProperty(selected) &&
 | 
					 | 
				
			||||||
				nextProps.reports.selectedTabulation == null) {
 | 
					 | 
				
			||||||
			nextProps.onSelectSeries(nextProps.reports.tabulations[selected]);
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	handleSelectSeries(series) {
 | 
						handleSelectSeries(series) {
 | 
				
			||||||
@@ -167,8 +163,7 @@ class ReportsTab extends React.Component {
 | 
				
			|||||||
			return;
 | 
								return;
 | 
				
			||||||
		var seriesTraversal = this.props.reports.seriesTraversal.slice();
 | 
							var seriesTraversal = this.props.reports.seriesTraversal.slice();
 | 
				
			||||||
		seriesTraversal.push(series);
 | 
							seriesTraversal.push(series);
 | 
				
			||||||
		var selectedTabulation = this.props.reports.tabulations[this.props.reports.selected];
 | 
							this.props.onSelectSeries(seriesTraversal);
 | 
				
			||||||
		this.props.onSelectSeries(selectedTabulation, seriesTraversal);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	handleSelectReport(report) {
 | 
						handleSelectReport(report) {
 | 
				
			||||||
		this.props.onSelectReport(report);
 | 
							this.props.onSelectReport(report);
 | 
				
			||||||
@@ -214,10 +209,9 @@ class ReportsTab extends React.Component {
 | 
				
			|||||||
				var self = this;
 | 
									var self = this;
 | 
				
			||||||
				var navOnClick = function() {
 | 
									var navOnClick = function() {
 | 
				
			||||||
					var onSelectSeries = self.props.onSelectSeries;
 | 
										var onSelectSeries = self.props.onSelectSeries;
 | 
				
			||||||
					var tabulation = self.props.reports.tabulations[self.props.reports.selected];
 | 
					 | 
				
			||||||
					var mySeriesTraversal = seriesTraversal.slice();
 | 
										var mySeriesTraversal = seriesTraversal.slice();
 | 
				
			||||||
					return function() {
 | 
										return function() {
 | 
				
			||||||
						onSelectSeries(tabulation, mySeriesTraversal);
 | 
											onSelectSeries(mySeriesTraversal);
 | 
				
			||||||
					};
 | 
										};
 | 
				
			||||||
				}();
 | 
									}();
 | 
				
			||||||
				titleTracks.push((
 | 
									titleTracks.push((
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,7 +23,7 @@ function mapDispatchToProps(dispatch) {
 | 
				
			|||||||
		onDeleteReport: function(report) {dispatch(ReportActions.remove(report))},
 | 
							onDeleteReport: function(report) {dispatch(ReportActions.remove(report))},
 | 
				
			||||||
		onSelectReport: function(report) {dispatch(ReportActions.select(report))},
 | 
							onSelectReport: function(report) {dispatch(ReportActions.select(report))},
 | 
				
			||||||
		onTabulateReport: function(report) {dispatch(ReportActions.tabulate(report))},
 | 
							onTabulateReport: function(report) {dispatch(ReportActions.tabulate(report))},
 | 
				
			||||||
		onSelectSeries: function(tabulation, seriesTraversal) {dispatch(ReportActions.selectSeries(tabulation, seriesTraversal))}
 | 
							onSelectSeries: function(seriesTraversal) {dispatch(ReportActions.selectSeries(seriesTraversal))}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,9 @@ var assign = require('object-assign');
 | 
				
			|||||||
var ReportConstants = require('../constants/ReportConstants');
 | 
					var ReportConstants = require('../constants/ReportConstants');
 | 
				
			||||||
var UserConstants = require('../constants/UserConstants');
 | 
					var UserConstants = require('../constants/UserConstants');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var models = require('../models.js');
 | 
				
			||||||
 | 
					var Tabulation = models.Tabulation;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const initialState = {
 | 
					const initialState = {
 | 
				
			||||||
	map: {},
 | 
						map: {},
 | 
				
			||||||
	tabulations: {},
 | 
						tabulations: {},
 | 
				
			||||||
@@ -12,6 +15,40 @@ const initialState = {
 | 
				
			|||||||
	seriesTraversal: []
 | 
						seriesTraversal: []
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = function(state = initialState, action) {
 | 
					module.exports = function(state = initialState, action) {
 | 
				
			||||||
	switch (action.type) {
 | 
						switch (action.type) {
 | 
				
			||||||
		case ReportConstants.REPORTS_FETCHED:
 | 
							case ReportConstants.REPORTS_FETCHED:
 | 
				
			||||||
@@ -33,6 +70,13 @@ module.exports = function(state = initialState, action) {
 | 
				
			|||||||
			});
 | 
								});
 | 
				
			||||||
		case ReportConstants.REPORT_CREATED:
 | 
							case ReportConstants.REPORT_CREATED:
 | 
				
			||||||
		case ReportConstants.REPORT_UPDATED:
 | 
							case ReportConstants.REPORT_UPDATED:
 | 
				
			||||||
 | 
								var selectedTabulation = state.selectedTabulation;
 | 
				
			||||||
 | 
								var seriesTraversal = state.seriesTraversal;
 | 
				
			||||||
 | 
								if (state.selected == action.report.ReportId) {
 | 
				
			||||||
 | 
									selectedTabulation = initialState.selectedTabulation;
 | 
				
			||||||
 | 
									seriesTraversal = initialState.seriesTraversal;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			var report = action.report;
 | 
								var report = action.report;
 | 
				
			||||||
			var reports = assign({}, state.map, {
 | 
								var reports = assign({}, state.map, {
 | 
				
			||||||
				[report.ReportId]: report
 | 
									[report.ReportId]: report
 | 
				
			||||||
@@ -45,7 +89,9 @@ module.exports = function(state = initialState, action) {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
			return assign({}, state, {
 | 
								return assign({}, state, {
 | 
				
			||||||
				map: reports,
 | 
									map: reports,
 | 
				
			||||||
				list: list
 | 
									list: list,
 | 
				
			||||||
 | 
									selectedTabulation: selectedTabulation,
 | 
				
			||||||
 | 
									seriesTraversal: seriesTraversal
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		case ReportConstants.REPORT_REMOVED:
 | 
							case ReportConstants.REPORT_REMOVED:
 | 
				
			||||||
			var selected = state.selected;
 | 
								var selected = state.selected;
 | 
				
			||||||
@@ -58,22 +104,34 @@ module.exports = function(state = initialState, action) {
 | 
				
			|||||||
				selected: selected
 | 
									selected: selected
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		case ReportConstants.REPORT_SELECTED:
 | 
							case ReportConstants.REPORT_SELECTED:
 | 
				
			||||||
 | 
								var selectedTabulation = null;
 | 
				
			||||||
 | 
								if (state.tabulations.hasOwnProperty(action.report.ReportId)) {
 | 
				
			||||||
 | 
									selectedTabulation = getFlattenedTabulation(state.tabulations[action.report.ReportId], initialState.seriesTraversal)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			return assign({}, state, {
 | 
								return assign({}, state, {
 | 
				
			||||||
				selected: action.report.ReportId,
 | 
									selected: action.report.ReportId,
 | 
				
			||||||
				selectedTabulation: null,
 | 
									selectedTabulation: selectedTabulation,
 | 
				
			||||||
				seriesTraversal: []
 | 
									seriesTraversal: initialState.seriesTraversal
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		case ReportConstants.REPORT_TABULATED:
 | 
							case ReportConstants.REPORT_TABULATED:
 | 
				
			||||||
			var tabulation = action.tabulation;
 | 
								var tabulation = action.tabulation;
 | 
				
			||||||
			var tabulations = assign({}, state.tabulations, {
 | 
								var tabulations = assign({}, state.tabulations, {
 | 
				
			||||||
				[tabulation.ReportId]: tabulation
 | 
									[tabulation.ReportId]: tabulation
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
 | 
								var selectedTabulation = state.selectedTabulation;
 | 
				
			||||||
 | 
								var seriesTraversal = state.seriesTraversal;
 | 
				
			||||||
 | 
								if (tabulation.ReportId == state.selected) {
 | 
				
			||||||
 | 
									selectedTabulation = getFlattenedTabulation(tabulation, initialState.seriesTraversal)
 | 
				
			||||||
 | 
									seriesTraversal = initialState.seriesTraversal;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			return assign({}, state, {
 | 
								return assign({}, state, {
 | 
				
			||||||
				tabulations: tabulations
 | 
									tabulations: tabulations,
 | 
				
			||||||
 | 
									selectedTabulation: selectedTabulation,
 | 
				
			||||||
 | 
									seriesTraversal: seriesTraversal
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		case ReportConstants.SERIES_SELECTED:
 | 
							case ReportConstants.SERIES_SELECTED:
 | 
				
			||||||
			return assign({}, state, {
 | 
								return assign({}, state, {
 | 
				
			||||||
				selectedTabulation: action.tabulation,
 | 
									selectedTabulation: getFlattenedTabulation(state.tabulations[state.selected], action.seriesTraversal),
 | 
				
			||||||
				seriesTraversal: action.seriesTraversal
 | 
									seriesTraversal: action.seriesTraversal
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		case UserConstants.USER_LOGGEDOUT:
 | 
							case UserConstants.USER_LOGGEDOUT:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user