import { actions, actionTypes as fetchActions } from '@bufferapp/async-data-fetch';
import { actions as analyticsActions } from '@bufferapp/analytics-middleware';
import { getNumOfDaysForDateRange } from '@bufferapp/analyze-shared-components/utils';
import { actionTypes } from './actions';
const isChartWithNoProfilesAttached = chartId =>
chartId === 'comparison' ||
chartId.match(/aggregate/) ||
chartId === 'recent-posts';
export default store => next => (action) => { // eslint-disable-line no-unused-vars
switch (action.type) {
case actionTypes.ADD_TO_REPORT:
store.dispatch(analyticsActions.trackEvent('Report Module Added', {
module: action.chart_id.replace('-', ' '),
reportId: action.reportId,
channel: isChartWithNoProfilesAttached(action.chart_id) ?
null :
store.getState().profiles.selectedProfile.service,
}));
store.dispatch(actions.fetch({
name: 'add_chart_to_report',
args: {
reportId: action.reportId,
profileId: isChartWithNoProfilesAttached(action.chart_id) ?
null :
store.getState().profiles.selectedProfileId,
chartId: action.chart_id,
state: action.state,
},
}));
break;
case actionTypes.CREATE_REPORT:
store.dispatch(actions.fetch({
name: 'create_report',
args: {
userId: store.getState().appSidebar.user.id,
organizationId: store.getState().profiles.organizationId,
profileId: isChartWithNoProfilesAttached(action.chart_id) ?
null :
store.getState().profiles.selectedProfileId,
chartId: action.chart_id,
name: action.name,
state: action.state,
dateRange: {
range: store.getState().date.presets.find(preset => preset.selected).range,
start: store.getState().date.startDate,
end: store.getState().date.endDate,
},
},
}));
break;
case `create_report_${fetchActions.FETCH_SUCCESS}`:
store.dispatch(analyticsActions.trackEvent('Report Created', {
title: action.result.name,
days: getNumOfDaysForDateRange(store.getState().date),
reportId: action.result._id,
}));
break;
default:
break;
}
return next(action);
};
|