Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/assets/javascripts/add_context_commits_modal/store')
-rw-r--r--app/assets/javascripts/add_context_commits_modal/store/actions.js134
-rw-r--r--app/assets/javascripts/add_context_commits_modal/store/index.js15
-rw-r--r--app/assets/javascripts/add_context_commits_modal/store/mutation_types.js20
-rw-r--r--app/assets/javascripts/add_context_commits_modal/store/mutations.js56
-rw-r--r--app/assets/javascripts/add_context_commits_modal/store/state.js13
5 files changed, 238 insertions, 0 deletions
diff --git a/app/assets/javascripts/add_context_commits_modal/store/actions.js b/app/assets/javascripts/add_context_commits_modal/store/actions.js
new file mode 100644
index 00000000000..d23955182b2
--- /dev/null
+++ b/app/assets/javascripts/add_context_commits_modal/store/actions.js
@@ -0,0 +1,134 @@
+import _ from 'lodash';
+import axios from '~/lib/utils/axios_utils';
+import { deprecatedCreateFlash as createFlash } from '~/flash';
+import { s__ } from '~/locale';
+import Api from '~/api';
+import * as types from './mutation_types';
+
+export const setBaseConfig = ({ commit }, options) => {
+ commit(types.SET_BASE_CONFIG, options);
+};
+
+export const setTabIndex = ({ commit }, tabIndex) => commit(types.SET_TABINDEX, tabIndex);
+
+export const searchCommits = ({ dispatch, commit, state }, searchText) => {
+ commit(types.FETCH_COMMITS);
+
+ let params = {};
+ if (searchText) {
+ params = {
+ params: {
+ search: searchText,
+ per_page: 40,
+ },
+ };
+ }
+
+ return axios
+ .get(state.contextCommitsPath, params)
+ .then(({ data }) => {
+ let commits = data.map(o => ({ ...o, isSelected: false }));
+ commits = commits.map(c => {
+ const isPresent = state.selectedCommits.find(
+ selectedCommit => selectedCommit.short_id === c.short_id && selectedCommit.isSelected,
+ );
+ if (isPresent) {
+ return { ...c, isSelected: true };
+ }
+ return c;
+ });
+ if (!searchText) {
+ dispatch('setCommits', { commits: [...commits, ...state.contextCommits] });
+ } else {
+ dispatch('setCommits', { commits });
+ }
+ })
+ .catch(() => {
+ commit(types.FETCH_COMMITS_ERROR);
+ });
+};
+
+export const setCommits = ({ commit }, { commits: data, silentAddition = false }) => {
+ let commits = _.uniqBy(data, 'short_id');
+ commits = _.orderBy(data, c => new Date(c.committed_date), ['desc']);
+ if (silentAddition) {
+ commit(types.SET_COMMITS_SILENT, commits);
+ } else {
+ commit(types.SET_COMMITS, commits);
+ }
+};
+
+export const createContextCommits = ({ state }, { commits, forceReload = false }) =>
+ Api.createContextCommits(state.projectId, state.mergeRequestIid, {
+ commits: commits.map(commit => commit.short_id),
+ })
+ .then(() => {
+ if (forceReload) {
+ window.location.reload();
+ }
+
+ return true;
+ })
+ .catch(() => {
+ if (forceReload) {
+ createFlash(s__('ContextCommits|Failed to create context commits. Please try again.'));
+ }
+
+ return false;
+ });
+
+export const fetchContextCommits = ({ dispatch, commit, state }) => {
+ commit(types.FETCH_CONTEXT_COMMITS);
+ return Api.allContextCommits(state.projectId, state.mergeRequestIid)
+ .then(({ data }) => {
+ const contextCommits = data.map(o => ({ ...o, isSelected: true }));
+ dispatch('setContextCommits', contextCommits);
+ dispatch('setCommits', {
+ commits: [...state.commits, ...contextCommits],
+ silentAddition: true,
+ });
+ dispatch('setSelectedCommits', contextCommits);
+ })
+ .catch(() => {
+ commit(types.FETCH_CONTEXT_COMMITS_ERROR);
+ });
+};
+
+export const setContextCommits = ({ commit }, data) => {
+ commit(types.SET_CONTEXT_COMMITS, data);
+};
+
+export const removeContextCommits = ({ state }, forceReload = false) =>
+ Api.removeContextCommits(state.projectId, state.mergeRequestIid, {
+ commits: state.toRemoveCommits,
+ })
+ .then(() => {
+ if (forceReload) {
+ window.location.reload();
+ }
+
+ return true;
+ })
+ .catch(() => {
+ if (forceReload) {
+ createFlash(s__('ContextCommits|Failed to delete context commits. Please try again.'));
+ }
+
+ return false;
+ });
+
+export const setSelectedCommits = ({ commit }, selected) => {
+ let selectedCommits = _.uniqBy(selected, 'short_id');
+ selectedCommits = _.orderBy(
+ selectedCommits,
+ selectedCommit => new Date(selectedCommit.committed_date),
+ ['desc'],
+ );
+ commit(types.SET_SELECTED_COMMITS, selectedCommits);
+};
+
+export const setSearchText = ({ commit }, searchText) => commit(types.SET_SEARCH_TEXT, searchText);
+
+export const setToRemoveCommits = ({ commit }, data) => commit(types.SET_TO_REMOVE_COMMITS, data);
+
+export const resetModalState = ({ commit }) => commit(types.RESET_MODAL_STATE);
diff --git a/app/assets/javascripts/add_context_commits_modal/store/index.js b/app/assets/javascripts/add_context_commits_modal/store/index.js
new file mode 100644
index 00000000000..0bf3441379b
--- /dev/null
+++ b/app/assets/javascripts/add_context_commits_modal/store/index.js
@@ -0,0 +1,15 @@
+import Vue from 'vue';
+import Vuex from 'vuex';
+import * as actions from './actions';
+import mutations from './mutations';
+import state from './state';
+
+Vue.use(Vuex);
+
+export default () =>
+ new Vuex.Store({
+ namespaced: true,
+ state: state(),
+ actions,
+ mutations,
+ });
diff --git a/app/assets/javascripts/add_context_commits_modal/store/mutation_types.js b/app/assets/javascripts/add_context_commits_modal/store/mutation_types.js
new file mode 100644
index 00000000000..eda82f3984d
--- /dev/null
+++ b/app/assets/javascripts/add_context_commits_modal/store/mutation_types.js
@@ -0,0 +1,20 @@
+export const SET_BASE_CONFIG = 'SET_BASE_CONFIG';
+
+export const SET_TABINDEX = 'SET_TABINDEX';
+
+export const FETCH_COMMITS = 'FETCH_COMMITS';
+export const SET_COMMITS = 'SET_COMMITS';
+export const SET_COMMITS_SILENT = 'SET_COMMITS_SILENT';
+export const FETCH_COMMITS_ERROR = 'FETCH_COMMITS_ERROR';
+
+export const FETCH_CONTEXT_COMMITS = 'FETCH_CONTEXT_COMMITS';
+export const SET_CONTEXT_COMMITS = 'SET_CONTEXT_COMMITS';
+export const FETCH_CONTEXT_COMMITS_ERROR = 'FETCH_CONTEXT_COMMITS_ERROR';
+
+export const SET_SELECTED_COMMITS = 'SET_SELECTED_COMMITS';
+
+export const SET_SEARCH_TEXT = 'SET_SEARCH_TEXT';
+
+export const SET_TO_REMOVE_COMMITS = 'SET_TO_REMOVE_COMMITS';
+
+export const RESET_MODAL_STATE = 'RESET_MODAL_STATE';
diff --git a/app/assets/javascripts/add_context_commits_modal/store/mutations.js b/app/assets/javascripts/add_context_commits_modal/store/mutations.js
new file mode 100644
index 00000000000..8a3da0ca248
--- /dev/null
+++ b/app/assets/javascripts/add_context_commits_modal/store/mutations.js
@@ -0,0 +1,56 @@
+import * as types from './mutation_types';
+
+export default {
+ [types.SET_BASE_CONFIG](state, options) {
+ Object.assign(state, { ...options });
+ },
+ [types.SET_TABINDEX](state, tabIndex) {
+ state.tabIndex = tabIndex;
+ },
+ [types.FETCH_COMMITS](state) {
+ state.isLoadingCommits = true;
+ state.commitsLoadingError = false;
+ },
+ [types.SET_COMMITS](state, commits) {
+ state.commits = commits;
+ state.isLoadingCommits = false;
+ state.commitsLoadingError = false;
+ },
+ [types.SET_COMMITS_SILENT](state, commits) {
+ state.commits = commits;
+ },
+ [types.FETCH_COMMITS_ERROR](state) {
+ state.commitsLoadingError = true;
+ state.isLoadingCommits = false;
+ },
+ [types.FETCH_CONTEXT_COMMITS](state) {
+ state.isLoadingContextCommits = true;
+ state.contextCommitsLoadingError = false;
+ },
+ [types.SET_CONTEXT_COMMITS](state, contextCommits) {
+ state.contextCommits = contextCommits;
+ state.isLoadingContextCommits = false;
+ state.contextCommitsLoadingError = false;
+ },
+ [types.FETCH_CONTEXT_COMMITS_ERROR](state) {
+ state.contextCommitsLoadingError = true;
+ state.isLoadingContextCommits = false;
+ },
+ [types.SET_SELECTED_COMMITS](state, commits) {
+ state.selectedCommits = commits;
+ },
+ [types.SET_SEARCH_TEXT](state, searchText) {
+ state.searchText = searchText;
+ },
+ [types.SET_TO_REMOVE_COMMITS](state, commits) {
+ state.toRemoveCommits = commits;
+ },
+ [types.RESET_MODAL_STATE](state) {
+ state.tabIndex = 0;
+ state.commits = [];
+ state.contextCommits = [];
+ state.selectedCommits = [];
+ state.toRemoveCommits = [];
+ state.searchText = '';
+ },
+};
diff --git a/app/assets/javascripts/add_context_commits_modal/store/state.js b/app/assets/javascripts/add_context_commits_modal/store/state.js
new file mode 100644
index 00000000000..37239adccbb
--- /dev/null
+++ b/app/assets/javascripts/add_context_commits_modal/store/state.js
@@ -0,0 +1,13 @@
+export default () => ({
+ contextCommitsPath: '',
+ tabIndex: 0,
+ isLoadingCommits: false,
+ commits: [],
+ commitsLoadingError: false,
+ selectedCommits: [],
+ isLoadingContextCommits: false,
+ contextCommits: [],
+ contextCommitsLoadingError: false,
+ searchText: '',
+ toRemoveCommits: [],
+});