debian-mirror-gitlab/app/assets/javascripts/notes/stores/actions.js

496 lines
15 KiB
JavaScript
Raw Normal View History

2019-02-15 15:39:39 +05:30
import Vue from 'vue';
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-11-08 19:23:39 +05:30
import axios from '~/lib/utils/axios_utils';
2018-03-17 18:26:18 +05:30
import Visibility from 'visibilityjs';
2019-02-15 15:39:39 +05:30
import TaskList from '../../task_list';
2018-03-17 18:26:18 +05:30
import Flash from '../../flash';
import Poll from '../../lib/utils/poll';
import * as types from './mutation_types';
import * as utils from './utils';
import * as constants from '../constants';
import service from '../services/notes_service';
import loadAwardsHandler from '../../awards_handler';
import sidebarTimeTrackingEventHub from '../../sidebar/event_hub';
2019-02-15 15:39:39 +05:30
import { isInViewport, scrollToElement, isInMRPage } from '../../lib/utils/common_utils';
2019-12-26 22:10:19 +05:30
import { mergeUrlParams } from '../../lib/utils/url_utility';
2018-12-05 23:21:45 +05:30
import mrWidgetEventHub from '../../vue_merge_request_widget/event_hub';
2018-12-13 13:39:08 +05:30
import { __ } from '~/locale';
2019-09-30 21:07:59 +05:30
import Api from '~/api';
2018-03-17 18:26:18 +05:30
let eTagPoll;
2019-02-15 15:39:39 +05:30
export const expandDiscussion = ({ commit, dispatch }, data) => {
if (data.discussionId) {
dispatch('diffs/renderFileForDiscussionId', data.discussionId, { root: true });
}
commit(types.EXPAND_DISCUSSION, data);
};
2018-11-08 19:23:39 +05:30
export const collapseDiscussion = ({ commit }, data) => commit(types.COLLAPSE_DISCUSSION, data);
export const setNotesData = ({ commit }, data) => commit(types.SET_NOTES_DATA, data);
export const setNoteableData = ({ commit }, data) => commit(types.SET_NOTEABLE_DATA, data);
export const setUserData = ({ commit }, data) => commit(types.SET_USER_DATA, data);
export const setLastFetchedAt = ({ commit }, data) => commit(types.SET_LAST_FETCHED_AT, data);
export const setInitialNotes = ({ commit }, discussions) =>
commit(types.SET_INITIAL_DISCUSSIONS, discussions);
export const setTargetNoteHash = ({ commit }, data) => commit(types.SET_TARGET_NOTE_HASH, data);
export const setNotesFetchedState = ({ commit }, state) =>
commit(types.SET_NOTES_FETCHED_STATE, state);
export const toggleDiscussion = ({ commit }, data) => commit(types.TOGGLE_DISCUSSION, data);
2019-10-12 21:52:04 +05:30
export const fetchDiscussions = ({ commit, dispatch }, { path, filter, persistFilter }) =>
2019-12-04 20:38:33 +05:30
service.fetchDiscussions(path, filter, persistFilter).then(({ data }) => {
commit(types.SET_INITIAL_DISCUSSIONS, data);
dispatch('updateResolvableDiscussionsCounts');
});
2018-11-08 19:23:39 +05:30
2018-12-05 23:21:45 +05:30
export const updateDiscussion = ({ commit, state }, discussion) => {
commit(types.UPDATE_DISCUSSION, discussion);
return utils.findNoteObjectById(state.discussions, discussion.id);
};
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
export const removeNote = ({ commit, dispatch, state }, note) => {
const discussion = state.discussions.find(({ id }) => id === note.discussion_id);
2019-02-15 15:39:39 +05:30
2019-12-04 20:38:33 +05:30
commit(types.DELETE_NOTE, note);
2018-12-05 23:21:45 +05:30
2019-12-04 20:38:33 +05:30
dispatch('updateMergeRequestWidget');
dispatch('updateResolvableDiscussionsCounts');
2019-02-15 15:39:39 +05:30
2019-12-04 20:38:33 +05:30
if (isInMRPage()) {
dispatch('diffs/removeDiscussionsFromDiff', discussion);
}
};
export const deleteNote = ({ dispatch }, note) =>
axios.delete(note.path).then(() => {
dispatch('removeNote', note);
2018-03-17 18:26:18 +05:30
});
2019-02-15 15:39:39 +05:30
export const updateNote = ({ commit, dispatch }, { endpoint, note }) =>
2019-12-04 20:38:33 +05:30
service.updateNote(endpoint, note).then(({ data }) => {
commit(types.UPDATE_NOTE, data);
dispatch('startTaskList');
});
2018-03-17 18:26:18 +05:30
2019-07-07 11:18:12 +05:30
export const updateOrCreateNotes = ({ commit, state, getters, dispatch }, notes) => {
const { notesById } = getters;
notes.forEach(note => {
if (notesById[note.id]) {
commit(types.UPDATE_NOTE, note);
} else if (note.type === constants.DISCUSSION_NOTE || note.type === constants.DIFF_NOTE) {
const discussion = utils.findNoteObjectById(state.discussions, note.discussion_id);
if (discussion) {
commit(types.ADD_NEW_REPLY_TO_DISCUSSION, note);
} else if (note.type === constants.DIFF_NOTE) {
dispatch('fetchDiscussions', { path: state.notesData.discussionsPath });
} else {
commit(types.ADD_NEW_NOTE, note);
}
} else {
commit(types.ADD_NEW_NOTE, note);
}
});
};
2019-12-04 20:38:33 +05:30
export const replyToDiscussion = (
{ commit, state, getters, dispatch },
{ endpoint, data: reply },
) =>
service.replyToDiscussion(endpoint, reply).then(({ data }) => {
if (data.discussion) {
commit(types.UPDATE_DISCUSSION, data.discussion);
2019-07-07 11:18:12 +05:30
2019-12-04 20:38:33 +05:30
updateOrCreateNotes({ commit, state, getters, dispatch }, data.discussion.notes);
2019-07-07 11:18:12 +05:30
2019-12-04 20:38:33 +05:30
dispatch('updateMergeRequestWidget');
dispatch('startTaskList');
dispatch('updateResolvableDiscussionsCounts');
} else {
commit(types.ADD_NEW_REPLY_TO_DISCUSSION, data);
}
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
return data;
});
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
export const createNewNote = ({ commit, dispatch }, { endpoint, data: reply }) =>
service.createNewNote(endpoint, reply).then(({ data }) => {
if (!data.errors) {
commit(types.ADD_NEW_NOTE, data);
dispatch('updateMergeRequestWidget');
dispatch('startTaskList');
dispatch('updateResolvableDiscussionsCounts');
}
return data;
});
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
export const removePlaceholderNotes = ({ commit }) => commit(types.REMOVE_PLACEHOLDER_NOTES);
2018-03-17 18:26:18 +05:30
2019-07-31 22:56:46 +05:30
export const resolveDiscussion = ({ state, dispatch, getters }, { discussionId }) => {
const discussion = utils.findNoteObjectById(state.discussions, discussionId);
const isResolved = getters.isDiscussionResolved(discussionId);
if (!discussion) {
return Promise.reject();
} else if (isResolved) {
return Promise.resolve();
}
return dispatch('toggleResolveNote', {
endpoint: discussion.resolve_path,
isResolved,
discussion: true,
});
};
2018-12-05 23:21:45 +05:30
export const toggleResolveNote = ({ commit, dispatch }, { endpoint, isResolved, discussion }) =>
2019-12-04 20:38:33 +05:30
service.toggleResolveNote(endpoint, isResolved).then(({ data }) => {
const mutationType = discussion ? types.UPDATE_DISCUSSION : types.UPDATE_NOTE;
2018-03-27 19:54:05 +05:30
2019-12-04 20:38:33 +05:30
commit(mutationType, data);
2018-12-05 23:21:45 +05:30
2019-12-04 20:38:33 +05:30
dispatch('updateResolvableDiscussionsCounts');
2019-02-15 15:39:39 +05:30
2019-12-04 20:38:33 +05:30
dispatch('updateMergeRequestWidget');
});
2018-03-27 19:54:05 +05:30
export const closeIssue = ({ commit, dispatch, state }) => {
dispatch('toggleStateButtonLoading', true);
2019-12-04 20:38:33 +05:30
return service.toggleIssueState(state.notesData.closePath).then(({ data }) => {
commit(types.CLOSE_ISSUE);
dispatch('emitStateChangedEvent', data);
dispatch('toggleStateButtonLoading', false);
});
2018-03-27 19:54:05 +05:30
};
export const reopenIssue = ({ commit, dispatch, state }) => {
dispatch('toggleStateButtonLoading', true);
2019-12-04 20:38:33 +05:30
return service.toggleIssueState(state.notesData.reopenPath).then(({ data }) => {
commit(types.REOPEN_ISSUE);
dispatch('emitStateChangedEvent', data);
dispatch('toggleStateButtonLoading', false);
});
2018-03-27 19:54:05 +05:30
};
export const toggleStateButtonLoading = ({ commit }, value) =>
commit(types.TOGGLE_STATE_BUTTON_LOADING, value);
2018-11-08 19:23:39 +05:30
export const emitStateChangedEvent = ({ getters }, data) => {
2018-05-09 12:01:36 +05:30
const event = new CustomEvent('issuable_vue_app:change', {
detail: {
data,
isClosed: getters.openState === constants.CLOSED,
},
});
2018-03-27 19:54:05 +05:30
document.dispatchEvent(event);
};
export const toggleIssueLocalState = ({ commit }, newState) => {
if (newState === constants.CLOSED) {
commit(types.CLOSE_ISSUE);
} else if (newState === constants.REOPENED) {
commit(types.REOPEN_ISSUE);
}
};
2018-03-17 18:26:18 +05:30
export const saveNote = ({ commit, dispatch }, noteData) => {
2018-11-08 19:23:39 +05:30
// For MR discussuions we need to post as `note[note]` and issue we use `note.note`.
2018-12-05 23:21:45 +05:30
// For batch comments, we use draft_note
const note = noteData.data.draft_note || noteData.data['note[note]'] || noteData.data.note.note;
2018-03-17 18:26:18 +05:30
let placeholderText = note;
const hasQuickActions = utils.hasQuickActions(placeholderText);
const replyId = noteData.data.in_reply_to_discussion_id;
2018-12-05 23:21:45 +05:30
let methodToDispatch;
const postData = Object.assign({}, noteData);
if (postData.isDraft === true) {
methodToDispatch = replyId
? 'batchComments/addDraftToDiscussion'
: 'batchComments/createNewDraft';
if (!postData.draft_note && noteData.note) {
postData.draft_note = postData.note;
delete postData.note;
}
} else {
methodToDispatch = replyId ? 'replyToDiscussion' : 'createNewNote';
}
2018-03-17 18:26:18 +05:30
$('.notes-form .flash-container').hide(); // hide previous flash notification
2018-11-20 20:47:30 +05:30
commit(types.REMOVE_PLACEHOLDER_NOTES); // remove previous placeholders
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
if (replyId) {
if (hasQuickActions) {
placeholderText = utils.stripQuickActions(placeholderText);
}
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
if (placeholderText.length) {
commit(types.SHOW_PLACEHOLDER_NOTE, {
noteBody: placeholderText,
replyId,
});
}
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
if (hasQuickActions) {
commit(types.SHOW_PLACEHOLDER_NOTE, {
isSystemNote: true,
noteBody: utils.getQuickActionText(note),
replyId,
});
}
2018-03-17 18:26:18 +05:30
}
2019-12-21 20:55:43 +05:30
const processErrors = res => {
2018-05-09 12:01:36 +05:30
const { errors } = res;
2019-12-21 20:55:43 +05:30
if (!errors || !Object.keys(errors).length) {
return res;
}
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
/*
The following reply means that quick actions have been successfully applied:
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
{"commands_changes":{},"valid":false,"errors":{"commands_only":["Commands applied"]}}
*/
if (hasQuickActions) {
eTagPoll.makeRequest();
2019-09-04 21:01:54 +05:30
2019-12-21 20:55:43 +05:30
$('.js-gfm-input').trigger('clear-commands-cache.atwho');
const { commands_only: message } = errors;
Flash(message || __('Commands applied'), 'notice', noteData.flashContainer);
return res;
2018-05-09 12:01:36 +05:30
}
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
throw new Error(__('Failed to save comment!'));
};
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
const processEmojiAward = res => {
const { commands_changes: commandsChanges } = res;
const { emoji_award: emojiAward } = commandsChanges || {};
if (!emojiAward) {
return res;
2018-05-09 12:01:36 +05:30
}
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
const votesBlock = $('.js-awards-block').eq(0);
return loadAwardsHandler()
.then(awardsHandler => {
awardsHandler.addAwardToEmojiBar(votesBlock, emojiAward);
awardsHandler.scrollToAwards();
})
.catch(() => {
Flash(
__('Something went wrong while adding your award. Please try again.'),
'alert',
noteData.flashContainer,
);
})
.then(() => res);
};
const processTimeTracking = res => {
const { commands_changes: commandsChanges } = res;
const { spend_time: spendTime, time_estimate: timeEstimate } = commandsChanges || {};
if (spendTime != null || timeEstimate != null) {
sidebarTimeTrackingEventHub.$emit('timeTrackingUpdated', {
commands_changes: commandsChanges,
});
2018-05-09 12:01:36 +05:30
}
2019-12-21 20:55:43 +05:30
return res;
};
const removePlaceholder = res => {
2018-11-20 20:47:30 +05:30
if (replyId) {
commit(types.REMOVE_PLACEHOLDER_NOTES);
}
2018-05-09 12:01:36 +05:30
return res;
2019-12-21 20:55:43 +05:30
};
return dispatch(methodToDispatch, postData, { root: true })
.then(processErrors)
.then(processEmojiAward)
.then(processTimeTracking)
.then(removePlaceholder);
2018-03-17 18:26:18 +05:30
};
2018-11-08 19:23:39 +05:30
const pollSuccessCallBack = (resp, commit, state, getters, dispatch) => {
2018-03-17 18:26:18 +05:30
if (resp.notes && resp.notes.length) {
2019-07-07 11:18:12 +05:30
updateOrCreateNotes({ commit, state, getters, dispatch }, resp.notes);
2019-02-15 15:39:39 +05:30
dispatch('startTaskList');
2018-03-17 18:26:18 +05:30
}
2018-03-27 19:54:05 +05:30
commit(types.SET_LAST_FETCHED_AT, resp.last_fetched_at);
2018-03-17 18:26:18 +05:30
return resp;
};
2018-11-08 19:23:39 +05:30
export const poll = ({ commit, state, getters, dispatch }) => {
2018-03-17 18:26:18 +05:30
eTagPoll = new Poll({
resource: service,
method: 'poll',
2018-03-27 19:54:05 +05:30
data: state,
2019-12-04 20:38:33 +05:30
successCallback: ({ data }) => pollSuccessCallBack(data, commit, state, getters, dispatch),
2019-07-31 22:56:46 +05:30
errorCallback: () => Flash(__('Something went wrong while fetching latest comments.')),
2018-03-17 18:26:18 +05:30
});
if (!Visibility.hidden()) {
eTagPoll.makeRequest();
} else {
2018-03-27 19:54:05 +05:30
service.poll(state);
2018-03-17 18:26:18 +05:30
}
Visibility.change(() => {
if (!Visibility.hidden()) {
eTagPoll.restart();
} else {
eTagPoll.stop();
}
});
};
export const stopPolling = () => {
2019-09-30 21:07:59 +05:30
if (eTagPoll) eTagPoll.stop();
2018-03-17 18:26:18 +05:30
};
export const restartPolling = () => {
2019-09-30 21:07:59 +05:30
if (eTagPoll) eTagPoll.restart();
2018-03-17 18:26:18 +05:30
};
export const fetchData = ({ commit, state, getters }) => {
2018-05-09 12:01:36 +05:30
const requestData = {
endpoint: state.notesData.notesPath,
lastFetchedAt: state.lastFetchedAt,
};
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
service
.poll(requestData)
2019-12-04 20:38:33 +05:30
.then(({ data }) => pollSuccessCallBack(data, commit, state, getters))
2019-07-31 22:56:46 +05:30
.catch(() => Flash(__('Something went wrong while fetching latest comments.')));
2018-03-17 18:26:18 +05:30
};
2018-11-08 19:23:39 +05:30
export const toggleAward = ({ commit, getters }, { awardName, noteId }) => {
2018-03-17 18:26:18 +05:30
commit(types.TOGGLE_AWARD, { awardName, note: getters.notesById[noteId] });
};
2018-11-08 19:23:39 +05:30
export const toggleAwardRequest = ({ dispatch }, data) => {
2018-03-17 18:26:18 +05:30
const { endpoint, awardName } = data;
2019-09-30 21:07:59 +05:30
return axios.post(endpoint, { name: awardName }).then(() => {
dispatch('toggleAward', data);
});
2018-03-17 18:26:18 +05:30
};
export const scrollToNoteIfNeeded = (context, el) => {
if (!isInViewport(el[0])) {
scrollToElement(el);
}
};
2018-10-15 14:42:47 +05:30
2018-11-08 19:23:39 +05:30
export const fetchDiscussionDiffLines = ({ commit }, discussion) =>
2019-02-15 15:39:39 +05:30
axios.get(discussion.truncated_diff_lines_path).then(({ data }) => {
2018-11-08 19:23:39 +05:30
commit(types.SET_DISCUSSION_DIFF_LINES, {
discussionId: discussion.id,
diffLines: data.truncated_diff_lines,
});
});
2018-12-05 23:21:45 +05:30
export const updateMergeRequestWidget = () => {
mrWidgetEventHub.$emit('mr.discussion.updated');
};
2018-12-13 13:39:08 +05:30
export const setLoadingState = ({ commit }, data) => {
commit(types.SET_NOTES_LOADING_STATE, data);
};
2019-10-12 21:52:04 +05:30
export const filterDiscussion = ({ dispatch }, { path, filter, persistFilter }) => {
2018-12-13 13:39:08 +05:30
dispatch('setLoadingState', true);
2019-10-12 21:52:04 +05:30
dispatch('fetchDiscussions', { path, filter, persistFilter })
2018-12-13 13:39:08 +05:30
.then(() => {
dispatch('setLoadingState', false);
dispatch('setNotesFetchedState', true);
})
.catch(() => {
dispatch('setLoadingState', false);
dispatch('setNotesFetchedState', true);
Flash(__('Something went wrong while fetching comments. Please try again.'));
});
};
export const setCommentsDisabled = ({ commit }, data) => {
commit(types.DISABLE_COMMENTS, data);
};
2019-02-15 15:39:39 +05:30
export const startTaskList = ({ dispatch }) =>
Vue.nextTick(
() =>
new TaskList({
dataType: 'note',
fieldName: 'note',
selector: '.notes .is-editable',
onSuccess: () => dispatch('startTaskList'),
}),
);
2019-09-30 21:07:59 +05:30
export const updateResolvableDiscussionsCounts = ({ commit }) =>
2019-02-15 15:39:39 +05:30
commit(types.UPDATE_RESOLVABLE_DISCUSSIONS_COUNTS);
export const submitSuggestion = (
2019-07-31 22:56:46 +05:30
{ commit, dispatch },
{ discussionId, noteId, suggestionId, flashContainer },
2019-12-21 20:55:43 +05:30
) => {
const dispatchResolveDiscussion = () =>
dispatch('resolveDiscussion', { discussionId }).catch(() => {});
return Api.applySuggestion(suggestionId)
2019-07-31 22:56:46 +05:30
.then(() => commit(types.APPLY_SUGGESTION, { discussionId, noteId, suggestionId }))
2019-12-21 20:55:43 +05:30
.then(dispatchResolveDiscussion)
2019-02-15 15:39:39 +05:30
.catch(err => {
const defaultMessage = __(
'Something went wrong while applying the suggestion. Please try again.',
);
const flashMessage = err.response.data ? `${err.response.data.message}.` : defaultMessage;
Flash(__(flashMessage), 'alert', flashContainer);
});
2019-12-21 20:55:43 +05:30
};
2019-02-15 15:39:39 +05:30
2019-03-02 22:35:43 +05:30
export const convertToDiscussion = ({ commit }, noteId) =>
commit(types.CONVERT_TO_DISCUSSION, noteId);
2019-07-07 11:18:12 +05:30
export const removeConvertedDiscussion = ({ commit }, noteId) =>
commit(types.REMOVE_CONVERTED_DISCUSSION, noteId);
2019-12-26 22:10:19 +05:30
export const fetchDescriptionVersion = (_, { endpoint, startingVersion }) => {
let requestUrl = endpoint;
if (startingVersion) {
requestUrl = mergeUrlParams({ start_version_id: startingVersion }, requestUrl);
}
return axios
.get(requestUrl)
.then(res => res.data)
.catch(() => {
Flash(__('Something went wrong while fetching description changes. Please try again.'));
});
};
2018-10-15 14:42:47 +05:30
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};