All files / report middleware.js

95.45% Statements 42/44
91.67% Branches 33/36
100% Functions 7/7
95.24% Lines 40/42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139            3x 3x   3x 3x 3x   3x   3x 3x           17x 17x       17x   3x     3x               3x   3x     2x 1x                   2x                   4x                 4x   1x               1x   1x               1x   1x             1x   2x 2x 2x 2x     1x 1x 1x 1x               1x 1x   1x           1x   1x   17x    
import moment from 'moment-timezone';
import { actions, actionTypes as asyncDataFetchActionTypes } from '@bufferapp/async-data-fetch';
import { actionTypes as dateActionTypes } from '@bufferapp/analyze-date-picker';
import { actionTypes, actions as reportActions } from './reducer';
import PDFFormatter from './PDFFormatter';
 
export const DIRECTION_UP = 'up';
export const DIRECTION_DOWN = 'down';
 
const getReportId = (pathname) => {
  const routeMatch = pathname.match(/reports\/(.+)$/);
  return routeMatch ? routeMatch[1] : null;
};
const isReportDetailRoute = pathname => getReportId(pathname) !== null;
 
const dateRangesDoNotMatch = (reportDate, dateRange) => (
  reportDate.startDate !== dateRange.startDate ||
  reportDate.endDate !== dateRange.endDate ||
  (dateRange.preset && reportDate.range !== dateRange.preset.range) ||
  (!dateRange.preset && reportDate.range)
);
 
export default store => next => (action) => { // eslint-disable-line no-unused-vars
  const state = store.getState();
  let formatter;
  let logoFile;
  let fileReader;
  switch (action.type) {
    case dateActionTypes.SET_DATE_RANGE: {
      Eif (!state.report.loading &&
        isReportDetailRoute(state.router.location.pathname) &&
        dateRangesDoNotMatch(state.report.dateRange, action)) {
        const report = {
          ...state.report,
          dateRange: {
            range: action.preset ? action.preset.range : null,
            startDate: action.startDate,
            endDate: action.endDate,
          },
        };
        store.dispatch(reportActions.saveChanges(report));
      }
      break;
    }
    case `update_report_${asyncDataFetchActionTypes.FETCH_SUCCESS}`:
      if (!Object.is(state.report.dateRange, action.args.dateRange)) {
        store.dispatch(actions.fetch({
          name: 'get_report',
          args: {
            _id: action.result._id,
            timezone: moment.tz.guess(),
            startDate: state.date.startDate,
            endDate: state.date.endDate,
          },
        }));
      }
      break;
    case actionTypes.FETCH:
      store.dispatch(actions.fetch({
        name: 'get_report',
        args: {
          _id: getReportId(state.router.location.pathname),
        },
      }));
      break;
    case actionTypes.SAVE_CHANGES:
      store.dispatch(actions.fetch({
        name: 'update_report',
        args: {
          id: state.report.id,
          name: action.name ? action.name : state.report.name,
          description: action.description || action.description === '' ? action.description : state.report.description,
          dateRange: action.dateRange ? action.dateRange : state.report.dateRange,
        },
      }));
      break;
    case actionTypes.MOVE_CHART_UP:
      store.dispatch(actions.fetch({
        name: 'move_chart',
        args: {
          direction: DIRECTION_UP,
          chartId: action.chartId,
          reportId: state.report.id,
        },
      }));
      break;
    case actionTypes.MOVE_CHART_DOWN:
      store.dispatch(actions.fetch({
        name: 'move_chart',
        args: {
          direction: DIRECTION_DOWN,
          chartId: action.chartId,
          reportId: state.report.id,
        },
      }));
      break;
    case actionTypes.DELETE_CHART:
      store.dispatch(actions.fetch({
        name: 'delete_chart',
        args: {
          chartId: action.chartId,
          reportId: state.report.id,
        },
      }));
      break;
    case actionTypes.PARSE_PAGE_BREAKS:
      formatter = new PDFFormatter(document.getElementById('report-page'));
      PDFFormatter.formatWrapper(document.getElementById('root'));
      formatter.formatPage();
      break;
    case actionTypes.UPLOAD_LOGO:
      // get the logo file
      logoFile = action.logo[0];
      fileReader = new FileReader();
      fileReader.onload = (event) => {
        store.dispatch(actions.fetch({
          name: 'upload_report_logo',
          args: {
            reportId: state.report.id,
            logoImage: event.target.result,
          },
        }));
      };
      fileReader.readAsDataURL(logoFile);
      break;
    case actionTypes.DELETE_LOGO:
      store.dispatch(actions.fetch({
        name: 'delete_report_logo',
        args: {
          reportId: state.report.id,
        },
      }));
      break;
    default:
      break;
  }
  return next(action);
};