summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/self_monitor/store/actions.js
blob: 99731309a6208d9973342083ea358f631f218696 (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
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
import { __, s__ } from '~/locale';
import axios from '~/lib/utils/axios_utils';
import statusCodes from '~/lib/utils/http_status';
import { backOff } from '~/lib/utils/common_utils';
import * as types from './mutation_types';

const TWO_MINUTES = 120000;

function backOffRequest(makeRequestCallback) {
  return backOff((next, stop) => {
    makeRequestCallback()
      .then((resp) => {
        if (resp.status === statusCodes.ACCEPTED) {
          next();
        } else {
          stop(resp);
        }
      })
      .catch(stop);
  }, TWO_MINUTES);
}

export const setSelfMonitor = ({ commit }, enabled) => commit(types.SET_ENABLED, enabled);

export const createProject = ({ dispatch }) => dispatch('requestCreateProject');

export const resetAlert = ({ commit }) => commit(types.SET_SHOW_ALERT, false);

export const requestCreateProject = ({ dispatch, state, commit }) => {
  commit(types.SET_LOADING, true);
  axios
    .post(state.createProjectEndpoint)
    .then((resp) => {
      if (resp.status === statusCodes.ACCEPTED) {
        dispatch('requestCreateProjectStatus', resp.data.job_id);
      }
    })
    .catch((error) => {
      dispatch('requestCreateProjectError', error);
    });
};

export const requestCreateProjectStatus = ({ dispatch, state }, jobId) => {
  backOffRequest(() => axios.get(state.createProjectStatusEndpoint, { params: { job_id: jobId } }))
    .then((resp) => {
      if (resp.status === statusCodes.OK) {
        dispatch('requestCreateProjectSuccess', resp.data);
      }
    })
    .catch((error) => {
      dispatch('requestCreateProjectError', error);
    });
};

export const requestCreateProjectSuccess = ({ commit, dispatch }, selfMonitorData) => {
  commit(types.SET_LOADING, false);
  commit(types.SET_PROJECT_URL, selfMonitorData.project_full_path);
  commit(types.SET_ALERT_CONTENT, {
    message: s__('SelfMonitoring|Self monitoring project has been successfully created.'),
    actionText: __('View project'),
    actionName: 'viewSelfMonitorProject',
  });
  commit(types.SET_SHOW_ALERT, true);
  commit(types.SET_PROJECT_CREATED, true);
  dispatch('setSelfMonitor', true);
};

export const requestCreateProjectError = ({ commit }, error) => {
  const { response } = error;
  const message = response.data && response.data.message ? response.data.message : '';

  commit(types.SET_ALERT_CONTENT, {
    message: `${__('There was an error saving your changes.')} ${message}`,
  });
  commit(types.SET_SHOW_ALERT, true);
  commit(types.SET_LOADING, false);
};

export const deleteProject = ({ dispatch }) => dispatch('requestDeleteProject');

export const requestDeleteProject = ({ dispatch, state, commit }) => {
  commit(types.SET_LOADING, true);
  axios
    .delete(state.deleteProjectEndpoint)
    .then((resp) => {
      if (resp.status === statusCodes.ACCEPTED) {
        dispatch('requestDeleteProjectStatus', resp.data.job_id);
      }
    })
    .catch((error) => {
      dispatch('requestDeleteProjectError', error);
    });
};

export const requestDeleteProjectStatus = ({ dispatch, state }, jobId) => {
  backOffRequest(() => axios.get(state.deleteProjectStatusEndpoint, { params: { job_id: jobId } }))
    .then((resp) => {
      if (resp.status === statusCodes.OK) {
        dispatch('requestDeleteProjectSuccess', resp.data);
      }
    })
    .catch((error) => {
      dispatch('requestDeleteProjectError', error);
    });
};

export const requestDeleteProjectSuccess = ({ commit }) => {
  commit(types.SET_PROJECT_URL, '');
  commit(types.SET_PROJECT_CREATED, false);
  commit(types.SET_ALERT_CONTENT, {
    message: s__('SelfMonitoring|Self monitoring project has been successfully deleted.'),
    actionText: __('Undo'),
    actionName: 'createProject',
  });
  commit(types.SET_SHOW_ALERT, true);
  commit(types.SET_LOADING, false);
};

export const requestDeleteProjectError = ({ commit }, error) => {
  const { response } = error;
  const message = response.data && response.data.message ? response.data.message : '';

  commit(types.SET_ALERT_CONTENT, {
    message: `${__('There was an error saving your changes.')} ${message}`,
  });
  commit(types.SET_LOADING, false);
};