All files / app/assets/javascripts/deploy_freeze/store actions.js

97.83% Statements 45/46
100% Branches 0/0
95.24% Functions 20/21
97.83% Lines 45/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            5x 2x     5x 2x     5x 2x     5x 6x 6x   6x   4x 4x 4x     4x     4x       5x 3x 3x             5x 3x 3x               5x 2x   2x 1x   1x     1x   1x       5x 4x   4x   1x     1x           5x 1x 1x 1x 1x     5x 1x     5x 1x     5x 1x     5x      
import Api from '~/api';
import createFlash from '~/flash';
import { logError } from '~/lib/logger';
import { __ } from '~/locale';
import * as types from './mutation_types';
 
export const requestFreezePeriod = ({ commit }) => {
  commit(types.REQUEST_ADD_FREEZE_PERIOD);
};
 
export const receiveFreezePeriodSuccess = ({ commit }) => {
  commit(types.RECEIVE_ADD_FREEZE_PERIOD_SUCCESS);
};
 
export const receiveFreezePeriodError = ({ commit }, error) => {
  commit(types.RECEIVE_ADD_FREEZE_PERIOD_ERROR, error);
};
 
const receiveFreezePeriod = (store, request) => {
  const { dispatch, commit } = store;
  dispatch('requestFreezePeriod');
 
  request(store)
    .then(() => {
      dispatch('receiveFreezePeriodSuccess');
      commit(types.RESET_MODAL);
      dispatch('fetchFreezePeriods');
    })
    .catch((error) => {
      createFlash({
        message: __('Error: Unable to create deploy freeze'),
      });
      dispatch('receiveFreezePeriodError', error);
    });
};
 
export const addFreezePeriod = (store) =>
  receiveFreezePeriod(store, ({ state }) =>
    Api.createFreezePeriod(state.projectId, {
      freeze_start: state.freezeStartCron,
      freeze_end: state.freezeEndCron,
      cron_timezone: state.selectedTimezoneIdentifier,
    }),
  );
 
export const updateFreezePeriod = (store) =>
  receiveFreezePeriod(store, ({ state }) =>
    Api.updateFreezePeriod(state.projectId, {
      id: state.selectedId,
      freeze_start: state.freezeStartCron,
      freeze_end: state.freezeEndCron,
      cron_timezone: state.selectedTimezoneIdentifier,
    }),
  );
 
export const deleteFreezePeriod = ({ state, commit }, { id }) => {
  commit(types.REQUEST_DELETE_FREEZE_PERIOD, id);
 
  return Api.deleteFreezePeriod(state.projectId, id)
    .then(() => commit(types.RECEIVE_DELETE_FREEZE_PERIOD_SUCCESS, id))
    .catch((e) => {
      createFlash({
        message: __('Error: Unable to delete deploy freeze'),
      });
      commit(types.RECEIVE_DELETE_FREEZE_PERIOD_ERROR, id);
 
      logError(`Unable to delete deploy freeze`, e);
    });
};
 
export const fetchFreezePeriods = ({ commit, state }) => {
  commit(types.REQUEST_FREEZE_PERIODS);
 
  return Api.freezePeriods(state.projectId)
    .then(({ data }) => {
      commit(types.RECEIVE_FREEZE_PERIODS_SUCCESS, data);
    })
    .catch(() => {
      createFlash({
        message: __('There was an error fetching the deploy freezes.'),
      });
    });
};
 
export const setFreezePeriod = ({ commit }, freezePeriod) => {
  commit(types.SET_SELECTED_ID, freezePeriod.id);
  commit(types.SET_SELECTED_TIMEZONE, freezePeriod.cronTimezone);
  commit(types.SET_FREEZE_START_CRON, freezePeriod.freezeStart);
  commit(types.SET_FREEZE_END_CRON, freezePeriod.freezeEnd);
};
 
export const setSelectedTimezone = ({ commit }, timezone) => {
  commit(types.SET_SELECTED_TIMEZONE, timezone);
};
 
export const setFreezeStartCron = ({ commit }, { freezeStartCron }) => {
  commit(types.SET_FREEZE_START_CRON, freezeStartCron);
};
 
export const setFreezeEndCron = ({ commit }, { freezeEndCron }) => {
  commit(types.SET_FREEZE_END_CRON, freezeEndCron);
};
 
export const resetModal = ({ commit }) => {
  commit(types.RESET_MODAL);
};