1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-10-30 15:50:04 -04:00
moneygo/js/actions/ReportActions.js

110 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-02-11 20:39:38 -05:00
var ReportConstants = require('../constants/ReportConstants');
var ErrorActions = require('./ErrorActions');
var models = require('../models.js');
var Report = models.Report;
var Error = models.Error;
2017-02-17 10:01:31 -05:00
function fetchReport(reportName) {
2017-02-11 20:39:38 -05:00
return {
2017-02-17 10:01:31 -05:00
type: ReportConstants.FETCH_REPORT,
reportName: reportName
2017-02-11 20:39:38 -05:00
}
}
function reportFetched(report) {
return {
type: ReportConstants.REPORT_FETCHED,
report: report
}
}
2017-02-17 10:01:31 -05:00
function selectReport(report, seriesTraversal) {
return {
type: ReportConstants.SELECT_REPORT,
report: report,
seriesTraversal: seriesTraversal
}
}
function reportSelected(flattenedReport, seriesTraversal) {
return {
type: ReportConstants.REPORT_SELECTED,
report: flattenedReport,
seriesTraversal: seriesTraversal
}
}
2017-02-11 20:39:38 -05:00
function fetch(report) {
return function (dispatch) {
2017-02-17 10:01:31 -05:00
dispatch(fetchReport(report));
2017-02-11 20:39:38 -05:00
$.ajax({
type: "GET",
dataType: "json",
url: "report/"+report+"/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
dispatch(ErrorActions.serverError(e));
} else {
var r = new Report();
r.fromJSON(data);
dispatch(reportFetched(r));
}
},
error: function(jqXHR, status, error) {
dispatch(ErrorActions.ajaxError(error));
}
});
};
}
2017-02-17 10:01:31 -05:00
function select(report, seriesTraversal) {
return function (dispatch) {
if (!seriesTraversal)
seriesTraversal = [];
dispatch(selectReport(report, seriesTraversal));
// Descend the tree to the right series to flatten
var series = report;
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[Report.topLevelAccountName()] = series.Values;
2017-02-17 10:01:31 -05:00
var flattenedReport = new Report();
flattenedReport.ReportId = report.ReportId;
flattenedReport.Title = report.Title;
flattenedReport.Subtitle = report.Subtitle;
flattenedReport.XAxisLabel = report.XAxisLabel;
flattenedReport.YAxisLabel = report.YAxisLabel;
flattenedReport.Labels = report.Labels.slice();
flattenedReport.FlattenedSeries = flattenedSeries;
dispatch(reportSelected(flattenedReport, seriesTraversal));
};
}
2017-02-11 20:39:38 -05:00
module.exports = {
2017-02-17 10:01:31 -05:00
fetch: fetch,
select: select
2017-02-11 20:39:38 -05:00
};