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,58 +251,80 @@ export const saveNote = ({ commit, dispatch }, noteData) => { ...@@ -251,58 +251,80 @@ 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:
{"commands_changes":{},"valid":false,"errors":{"commands_only":["Commands applied"]}} {"commands_changes":{},"valid":false,"errors":{"commands_only":["Commands applied"]}}
*/ */
if (hasQuickActions) { if (hasQuickActions) {
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;
} }
if (commandsChanges) { throw new Error(__('Failed to save comment!'));
if (commandsChanges.emoji_award) { };
const votesBlock = $('.js-awards-block').eq(0);
loadAwardsHandler()
.then(awardsHandler => {
awardsHandler.addAwardToEmojiBar(votesBlock, commandsChanges.emoji_award);
awardsHandler.scrollToAwards();
})
.catch(() => {
Flash(
__('Something went wrong while adding your award. Please try again.'),
'alert',
noteData.flashContainer,
);
});
}
if (commandsChanges.spend_time != null || commandsChanges.time_estimate != null) { const processEmojiAward = res => {
sidebarTimeTrackingEventHub.$emit('timeTrackingUpdated', res); const { commands_changes: commandsChanges } = res;
} const { emoji_award: emojiAward } = commandsChanges || {};
if (!emojiAward) {
return res;
} }
if (errors && errors.commands_only) { const votesBlock = $('.js-awards-block').eq(0);
Flash(errors.commands_only, 'notice', noteData.flashContainer);
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,
});
} }
return res;
};
const removePlaceholder = res => {
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