All files / hourly-chart middleware.js

100% Statements 24/24
83.33% Branches 5/6
100% Functions 6/6
100% Lines 22/22
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            2x   2x 1x       1x 1x 1x 24x 24x 24x   1x     4x 4x 4x   1x     1x   1x 1x                 1x   1x               1x   1x   4x    
import moment from 'moment';
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 exportCSVActionTypes, actions as exportCSVActions } from '@bufferapp/analyze-csv-export';
 
const EXPORT_FILENAME = 'hourly-engagements';
 
const formatDataForCSVExport = ({ metrics, selectedMetric }) => {
  const data = {
    hour: [],
    [selectedMetric]: [],
  };
  const { hourlyMetrics } = metrics.find(metric => metric.label === selectedMetric);
  const hour = moment().startOf('day');
  hourlyMetrics.forEach((metric) => {
    data[selectedMetric].push(metric);
    data.hour.push(hour.format('h A'));
    hour.add(1, 'hour');
  });
  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(EXPORT_FILENAME, formatDataForCSVExport(getState().hourly)),
      );
      break;
    case dateActionTypes.SET_DATE_RANGE:
      Eif (getState().profiles.selectedProfileId) {
        dispatch(actions.fetch({
          name: 'hourly',
          args: {
            profileId: getState().profiles.selectedProfileId,
            startDate: action.startDate,
            endDate: action.endDate,
          },
        }));
      }
      break;
    case profileActionTypes.SELECT_PROFILE:
      dispatch(actions.fetch({
        name: 'hourly',
        args: {
          profileId: action.profile.id,
          startDate: getState().date.startDate,
          endDate: getState().date.endDate,
        },
      }));
      break;
    default:
      break;
  }
  return next(action);
};