Commit b38b2a4d authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'winh-eslint-promise/no-nesting-notes-actions' into 'master'

Remove nested Promise from notes actions

See merge request gitlab-org/gitlab!17686
parents d109b8f5 0d308dbd
...@@ -251,11 +251,12 @@ export const saveNote = ({ commit, dispatch }, noteData) => { ...@@ -251,11 +251,12 @@ export const saveNote = ({ commit, dispatch }, noteData) => {
} }
} }
return dispatch(methodToDispatch, postData, { root: true }).then(res => { const processErrors = res => {
const { errors } = res; const { errors } = res;
const commandsChanges = res.commands_changes; if (!errors || !Object.keys(errors).length) {
return res;
}
if (errors && Object.keys(errors).length) {
/* /*
The following reply means that quick actions have been successfully applied: The following reply means that quick actions have been successfully applied:
...@@ -265,19 +266,28 @@ export const saveNote = ({ commit, dispatch }, noteData) => { ...@@ -265,19 +266,28 @@ export const saveNote = ({ commit, dispatch }, noteData) => {
eTagPoll.makeRequest(); eTagPoll.makeRequest();
$('.js-gfm-input').trigger('clear-commands-cache.atwho'); $('.js-gfm-input').trigger('clear-commands-cache.atwho');
Flash(__('Commands applied'), 'notice', noteData.flashContainer);
} else { const { commands_only: message } = errors;
throw new Error(__('Failed to save comment!')); Flash(message || __('Commands applied'), 'notice', noteData.flashContainer);
return res;
} }
throw new Error(__('Failed to save comment!'));
};
const processEmojiAward = res => {
const { commands_changes: commandsChanges } = res;
const { emoji_award: emojiAward } = commandsChanges || {};
if (!emojiAward) {
return res;
} }
if (commandsChanges) {
if (commandsChanges.emoji_award) {
const votesBlock = $('.js-awards-block').eq(0); const votesBlock = $('.js-awards-block').eq(0);
loadAwardsHandler() return loadAwardsHandler()
.then(awardsHandler => { .then(awardsHandler => {
awardsHandler.addAwardToEmojiBar(votesBlock, commandsChanges.emoji_award); awardsHandler.addAwardToEmojiBar(votesBlock, emojiAward);
awardsHandler.scrollToAwards(); awardsHandler.scrollToAwards();
}) })
.catch(() => { .catch(() => {
...@@ -286,23 +296,35 @@ export const saveNote = ({ commit, dispatch }, noteData) => { ...@@ -286,23 +296,35 @@ export const saveNote = ({ commit, dispatch }, noteData) => {
'alert', 'alert',
noteData.flashContainer, 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,
}); });
} }
if (commandsChanges.spend_time != null || commandsChanges.time_estimate != null) { return res;
sidebarTimeTrackingEventHub.$emit('timeTrackingUpdated', res); };
}
}
if (errors && errors.commands_only) { const removePlaceholder = res => {
Flash(errors.commands_only, 'notice', noteData.flashContainer);
}
if (replyId) { if (replyId) {
commit(types.REMOVE_PLACEHOLDER_NOTES); commit(types.REMOVE_PLACEHOLDER_NOTES);
} }
return res; return res;
}); };
return dispatch(methodToDispatch, postData, { root: true })
.then(processErrors)
.then(processEmojiAward)
.then(processTimeTracking)
.then(removePlaceholder);
}; };
const pollSuccessCallBack = (resp, commit, state, getters, dispatch) => { const pollSuccessCallBack = (resp, commit, state, getters, dispatch) => {
...@@ -430,10 +452,13 @@ export const updateResolvableDiscussionsCounts = ({ commit }) => ...@@ -430,10 +452,13 @@ export const updateResolvableDiscussionsCounts = ({ commit }) =>
export const submitSuggestion = ( export const submitSuggestion = (
{ commit, dispatch }, { commit, dispatch },
{ discussionId, noteId, suggestionId, flashContainer }, { discussionId, noteId, suggestionId, flashContainer },
) => ) => {
Api.applySuggestion(suggestionId) const dispatchResolveDiscussion = () =>
dispatch('resolveDiscussion', { discussionId }).catch(() => {});
return Api.applySuggestion(suggestionId)
.then(() => commit(types.APPLY_SUGGESTION, { discussionId, noteId, suggestionId })) .then(() => commit(types.APPLY_SUGGESTION, { discussionId, noteId, suggestionId }))
.then(() => dispatch('resolveDiscussion', { discussionId }).catch(() => {})) .then(dispatchResolveDiscussion)
.catch(err => { .catch(err => {
const defaultMessage = __( const defaultMessage = __(
'Something went wrong while applying the suggestion. Please try again.', 'Something went wrong while applying the suggestion. Please try again.',
...@@ -442,6 +467,7 @@ export const submitSuggestion = ( ...@@ -442,6 +467,7 @@ export const submitSuggestion = (
Flash(__(flashMessage), 'alert', flashContainer); Flash(__(flashMessage), 'alert', flashContainer);
}); });
};
export const convertToDiscussion = ({ commit }, noteId) => export const convertToDiscussion = ({ commit }, noteId) =>
commit(types.CONVERT_TO_DISCUSSION, noteId); commit(types.CONVERT_TO_DISCUSSION, noteId);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment