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

export const requestProjects = ({ commit }) => {
  commit(types.RESET_CONNECT);
};
export const receiveProjectsSuccess = ({ commit }, projects) => {
  commit(types.UPDATE_CONNECT_SUCCESS);
  commit(types.RECEIVE_PROJECTS, projects);
};
export const receiveProjectsError = ({ commit }) => {
  commit(types.UPDATE_CONNECT_ERROR);
  commit(types.RECEIVE_PROJECTS, null);
};

export const fetchProjects = ({ dispatch, state }, data) => {
  dispatch('requestProjects');
  return axios
    .post(`${data.listProjectsEndpoint}.json`, {
      error_tracking_setting: {
        api_host: state.apiHost,
        token: state.token,
      },
    })
    .then(res => {
      dispatch('receiveProjectsSuccess', res.data.projects.map(transformBackendProject));
    })
    .catch(() => {
      dispatch('receiveProjectsError');
    });
};

export const requestSettings = ({ commit }) => {
  commit(types.UPDATE_SETTINGS_LOADING, true);
};
export const receiveSettingsSuccess = ({ commit }) => {
  createFlash(__('Your changes have been saved.'), 'notice');
  commit(types.UPDATE_SETTINGS_LOADING, false);
};
export const receiveSettingsError = (
  { commit },
  { response: { data: { message = '' } = {} } = {} } = {},
) => {
  createFlash(`${__('There was an error saving your changes.')} ${message}`, 'alert');
  commit(types.UPDATE_SETTINGS_LOADING, false);
};

export const updateSettings = ({ dispatch, state }, data) => {
  dispatch('requestSettings');
  return axios
    .patch(data.operationsSettingsEndpoint, {
      project: {
        error_tracking_setting_attributes: {
          ...transformFrontendSettings(state),
        },
      },
    })
    .then(() => {
      dispatch('receiveSettingsSuccess');
    })
    .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 updateToken = ({ commit }, token) => {
  commit(types.UPDATE_TOKEN, token);
  commit(types.RESET_CONNECT);
};
export const updateSelectedProject = ({ commit }, selectedProject) => {
  commit(types.UPDATE_SELECTED_PROJECT, selectedProject);
};

export default () => {};