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

export const setExternalDashboardUrl = ({ commit }, url) =>
  commit(mutationTypes.SET_EXTERNAL_DASHBOARD_URL, url);

export const setDashboardTimezone = ({ commit }, selected) =>
  commit(mutationTypes.SET_DASHBOARD_TIMEZONE, selected);

export const saveChanges = ({ state, dispatch }) =>
  axios
    .patch(state.operationsSettingsEndpoint, {
      project: {
        metrics_setting_attributes: {
          dashboard_timezone: state.dashboardTimezone.selected,
          external_dashboard_url: state.externalDashboard.url,
        },
      },
    })
    .then(() => dispatch('receiveSaveChangesSuccess'))
    .catch(error => dispatch('receiveSaveChangesError', error));

export const receiveSaveChangesSuccess = () => {
  /**
   * The operations_controller currently handles successful requests
   * by creating a flash banner messsage to notify the user.
   */
  refreshCurrentPage();
};

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

  createFlash(`${__('There was an error saving your changes.')} ${message}`, 'alert');
};