summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/cycle_analytics/store/actions.js
blob: fe3c6d6b3ba7fa718e86d9c095ee3946976112fb (plain)
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
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import { DEFAULT_DAYS_TO_DISPLAY } from '../constants';
import * as types from './mutation_types';

export const fetchCycleAnalyticsData = ({
  state: { requestPath, startDate },
  dispatch,
  commit,
}) => {
  commit(types.REQUEST_CYCLE_ANALYTICS_DATA);

  return axios
    .get(requestPath, {
      params: { 'cycle_analytics[start_date]': startDate },
    })
    .then(({ data }) => commit(types.RECEIVE_CYCLE_ANALYTICS_DATA_SUCCESS, data))
    .then(() => dispatch('setSelectedStage'))
    .then(() => dispatch('fetchStageData'))
    .catch(() => {
      commit(types.RECEIVE_CYCLE_ANALYTICS_DATA_ERROR);
      createFlash({
        message: __('There was an error while fetching value stream analytics data.'),
      });
    });
};

export const fetchStageData = ({ state: { requestPath, selectedStage, startDate }, commit }) => {
  commit(types.REQUEST_STAGE_DATA);

  return axios
    .get(`${requestPath}/events/${selectedStage.name}.json`, {
      params: { 'cycle_analytics[start_date]': startDate },
    })
    .then(({ data }) => commit(types.RECEIVE_STAGE_DATA_SUCCESS, data))
    .catch(() => commit(types.RECEIVE_STAGE_DATA_ERROR));
};

export const setSelectedStage = ({ commit, state: { stages } }, selectedStage = null) => {
  const stage = selectedStage || stages[0];
  commit(types.SET_SELECTED_STAGE, stage);
};

export const setDateRange = ({ commit }, { startDate = DEFAULT_DAYS_TO_DISPLAY }) =>
  commit(types.SET_DATE_RANGE, { startDate });

export const initializeVsa = ({ commit, dispatch }, initialData = {}) => {
  commit(types.INITIALIZE_VSA, initialData);
  return dispatch('fetchCycleAnalyticsData');
};