All files middleware.js

100% Statements 20/20
85.71% Branches 6/7
100% Functions 5/5
100% Lines 19/19
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            1x 1x 1x 3x   1x     5x 5x 5x   1x 1x   1x 1x   1x                 1x   1x 1x                   1x   1x   5x    
import { actions } from '@bufferapp/async-data-fetch';
import { actionTypes as profileActionTypes } from '@bufferapp/analyze-profile-selector';
import { actionTypes as dateActionTypes } from '@bufferapp/analyze-date-picker';
import { actionTypes as exportActionTypes, actions as exportActions } from '@bufferapp/analyze-png-export';
import { actionTypes as exportCSVActionTypes, actions as exportCSVActions } from '@bufferapp/analyze-csv-export';
 
const formatDataForCSVExport = ({ metrics }) => {
  const data = {};
  metrics.forEach((metric) => {
    data[metric.label] = metric.value;
  });
  return data;
};
 
export default store => next => (action) => { // eslint-disable-line no-unused-vars
  const { dispatch, getState } = store;
  switch (action.type) {
    case exportCSVActionTypes.EXPORT_TO_CSV_START:
      dispatch(exportCSVActions.exportChart('posts-summary', formatDataForCSVExport(getState().postsSummary)));
      break;
    case exportActionTypes.EXPORT_TO_PNG_START:
      dispatch(exportActions.exportChart('js-dom-to-png-posts-summary', 'posts-summary'));
      break;
    case profileActionTypes.SELECT_PROFILE:
      dispatch(actions.fetch({
        name: 'posts_summary',
        args: {
          profileId: action.profile.id,
          profileService: action.profile.service,
          startDate: getState().date.startDate,
          endDate: getState().date.endDate,
        },
      }));
      break;
    case dateActionTypes.SET_DATE_RANGE:
      Eif (getState().profiles.selectedProfileId) {
        dispatch(actions.fetch({
          name: 'posts_summary',
          args: {
            profileId: getState().profiles.selectedProfileId,
            profileService: getState().profiles.selectedProfileService,
            startDate: action.startDate,
            endDate: action.endDate,
          },
        }));
      }
      break;
    default:
      break;
  }
  return next(action);
};