summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/error_tracking_settings/store/actions.js
blob: 972ad58c6172b38d817d9bae5d93c12dff4f87da (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
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { refreshCurrentPage } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
import { transformFrontendSettings } from '../utils';
import * as types from './mutation_types';

export const requestProjects = ({ commit }) => {
  commit(types.SET_PROJECTS_LOADING, true);
  commit(types.RESET_CONNECT);
};

export const receiveProjectsSuccess = ({ commit }, projects) => {
  commit(types.UPDATE_CONNECT_SUCCESS);
  commit(types.RECEIVE_PROJECTS, projects);
  commit(types.SET_PROJECTS_LOADING, false);
};

export const receiveProjectsError = ({ commit }) => {
  commit(types.UPDATE_CONNECT_ERROR);
  commit(types.CLEAR_PROJECTS);
  commit(types.SET_PROJECTS_LOADING, false);
};

export const fetchProjects = ({ dispatch, state }) => {
  dispatch('requestProjects');
  return axios
    .get(state.listProjectsEndpoint, {
      params: {
        api_host: state.apiHost,
        token: state.token,
      },
    })
    .then(({ data: { projects } }) => {
      dispatch('receiveProjectsSuccess', projects);
    })
    .catch(() => {
      dispatch('receiveProjectsError');
    });
};

export const requestSettings = ({ commit }) => {
  commit(types.UPDATE_SETTINGS_LOADING, true);
};

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

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

export const updateSettings = ({ dispatch, state }) => {
  dispatch('requestSettings');
  return axios
    .patch(state.operationsSettingsEndpoint, {
      project: {
        error_tracking_setting_attributes: {
          ...transformFrontendSettings(state),
        },
      },
    })
    .then(() => {
      refreshCurrentPage();
    })
    .catch((err) => {
      dispatch('receiveSettingsError', err);
    });
};

export const updateApiHost = ({ commit }, apiHost) => {
  commit(types.UPDATE_API_HOST, apiHost);
  commit(types.RESET_CONNECT);
};

export const updateEnabled = ({ commit }, enabled) => {
  commit(types.UPDATE_ENABLED, enabled);
};

export const updateIntegrated = ({ commit }, integrated) => {
  commit(types.UPDATE_INTEGRATED, integrated);
};

export const updateToken = ({ commit }, token) => {
  commit(types.UPDATE_TOKEN, token);
  commit(types.RESET_CONNECT);
};

export const updateSelectedProject = ({ commit }, selectedProject) => {
  commit(types.UPDATE_SELECTED_PROJECT, selectedProject);
};

export const setInitialState = ({ commit }, data) => {
  commit(types.SET_INITIAL_STATE, data);
};