var ReportConstants = require('../constants/ReportConstants'); var ErrorActions = require('./ErrorActions'); var Report = models.Report; var Error = models.Error; function fetchReport(reportId) { return { type: ReportConstants.FETCH_REPORT, reportId: reportId } } function reportFetched(report) { return { type: ReportConstants.REPORT_FETCHED, report: report } } function fetch(reportId) { return function (dispatch) { dispatch(fetchReport(reportId)); $.ajax({ type: "GET", dataType: "json", url: "report/?id="+reportId, 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(e)); } }); }; } module.exports = { fetch: fetch };