summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repo/stores/actions.js
blob: ca2f2a5ce7ad924c4b84ca149a615de924658d40 (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
128
129
import Vue from 'vue';
import flash from '../../flash';
import service from '../services';
import * as types from './mutation_types';

export const redirectToUrl = url => gl.utils.visitUrl(url);

export const setInitialData = ({ commit }, data) => commit(types.SET_INITIAL_DATA, data);

export const closeDiscardPopup = ({ commit }) => commit(types.TOGGLE_DISCARD_POPUP, false);

export const discardAllChanges = ({ commit, getters, dispatch }) => {
  const changedFiles = getters.changedFiles;

  changedFiles.forEach((file) => {
    commit(types.DISCARD_FILE_CHANGES, file);

    if (file.tempFile) {
      dispatch('closeFile', { file, force: true });
    }
  });
};

export const closeAllFiles = ({ state, dispatch }) => {
  state.openFiles.forEach(file => dispatch('closeFile', { file }));
};

export const toggleEditMode = ({ state, commit, getters, dispatch }, force = false) => {
  const changedFiles = getters.changedFiles;

  if (changedFiles.length && !force) {
    commit(types.TOGGLE_DISCARD_POPUP, true);
  } else {
    commit(types.TOGGLE_EDIT_MODE);
    commit(types.TOGGLE_DISCARD_POPUP, false);
    dispatch('toggleBlobView');

    if (!state.editMode) {
      dispatch('discardAllChanges');
    }
  }
};

export const toggleBlobView = ({ commit, state }) => {
  if (state.editMode) {
    commit(types.SET_EDIT_MODE);
  } else {
    commit(types.SET_PREVIEW_MODE);
  }
};

export const checkCommitStatus = ({ state }) => service.getBranchData(
  state.project.id,
  state.currentBranch,
)
  .then((data) => {
    const { id } = data.commit;

    if (state.currentRef !== id) {
      return true;
    }

    return false;
  })
  .catch(() => flash('Error checking branch data. Please try again.'));

export const commitChanges = ({ commit, state, dispatch }, { payload, newMr }) =>
  service.commit(state.project.id, payload)
  .then((data) => {
    const { branch } = payload;
    if (!data.short_id) {
      flash(data.message);
      return;
    }

    flash(`Your changes have been committed. Commit ${data.short_id} with ${data.stats.additions} additions, ${data.stats.deletions} deletions.`, 'notice');

    if (newMr) {
      redirectToUrl(`${state.endpoints.newMergeRequestUrl}${branch}`);
    } else {
      commit(types.SET_COMMIT_REF, data.id);
      dispatch('discardAllChanges');
      dispatch('closeAllFiles');
      dispatch('toggleEditMode');

      window.scrollTo(0, 0);
    }
  })
  .catch(() => flash('Error committing changes. Please try again.'));

export const createTempEntry = ({ state, dispatch }, { name, type, content = '', base64 = false }) => {
  if (type === 'tree') {
    dispatch('createTempTree', name);
  } else if (type === 'blob') {
    dispatch('createTempFile', {
      tree: state,
      name,
      base64,
      content,
    });
  }
};

export const popHistoryState = ({ state, dispatch, getters }) => {
  const treeList = getters.treeList;
  const tree = treeList.find(file => file.url === state.previousUrl);

  if (!tree) return;

  if (tree.type === 'tree') {
    dispatch('toggleTreeOpen', { endpoint: tree.url, tree });
  }
};

export const scrollToTab = () => {
  Vue.nextTick(() => {
    const tabs = document.getElementById('tabs');

    if (tabs) {
      const tabEl = tabs.querySelector('.active .repo-tab');

      tabEl.focus();
    }
  });
};

export * from './actions/tree';
export * from './actions/file';
export * from './actions/branch';