Commit eae72cc7 authored by Fatih Acet's avatar Fatih Acet

IssueNotesRefactor: Move quick actions logic to Store.

parent 4dce23c0
<script> <script>
/* global Flash */ /* global Flash */
import AjaxCache from '~/lib/utils/ajax_cache';
import UserAvatarLink from '../../vue_shared/components/user_avatar/user_avatar_link.vue'; import UserAvatarLink from '../../vue_shared/components/user_avatar/user_avatar_link.vue';
import MarkdownField from '../../vue_shared/components/markdown/field.vue'; import MarkdownField from '../../vue_shared/components/markdown/field.vue';
import IssueNoteSignedOutWidget from './issue_note_signed_out_widget.vue'; import IssueNoteSignedOutWidget from './issue_note_signed_out_widget.vue';
import eventHub from '../event_hub'; import eventHub from '../event_hub';
const REGEX_QUICK_ACTIONS = /^\/\w+.*$/gm;
export default { export default {
data() { data() {
const { create_note_path, state } = window.gl.issueData; const { create_note_path, state } = window.gl.issueData;
...@@ -51,9 +49,10 @@ export default { ...@@ -51,9 +49,10 @@ export default {
methods: { methods: {
handleSave(withIssueAction) { handleSave(withIssueAction) {
if (this.note.length) { if (this.note.length) {
const data = { const noteData = {
endpoint: this.endpoint, endpoint: this.endpoint,
noteData: { flashContainer: this.$el,
data: {
full_data: true, full_data: true,
note: { note: {
noteable_type: 'Issue', noteable_type: 'Issue',
...@@ -64,42 +63,13 @@ export default { ...@@ -64,42 +63,13 @@ export default {
}; };
if (this.noteType === 'discussion') { if (this.noteType === 'discussion') {
data.noteData.note.type = 'DiscussionNote'; noteData.data.note.type = 'DiscussionNote';
} }
let placeholderText = this.note; this.$store.dispatch('saveNote', noteData)
const hasQuickActions = this.hasQuickActions();
if (hasQuickActions) {
placeholderText = this.stripQuickActions();
}
if (placeholderText.length) {
this.$store.commit('showPlaceholderNote', {
noteBody: placeholderText,
});
}
if (hasQuickActions) {
this.$store.commit('showPlaceholderNote', {
isSystemNote: true,
noteBody: this.getQuickActionText(),
});
}
this.$store.dispatch('createNewNote', data)
.then((res) => { .then((res) => {
const { errors } = res; if (res.errors) {
if (res.errors.commands_only) {
if (hasQuickActions) {
this.$store.dispatch('poll');
$(this.$refs.textarea).trigger('clear-commands-cache.atwho');
new Flash('Commands applied', 'notice', $(this.$el)); // eslint-disable-line
}
if (errors) {
if (errors.commands_only) {
new Flash(errors.commands_only, 'notice', $(this.$el)); // eslint-disable-line
this.discard(); this.discard();
} else { } else {
this.handleError(); this.handleError();
...@@ -107,8 +77,6 @@ export default { ...@@ -107,8 +77,6 @@ export default {
} else { } else {
this.discard(); this.discard();
} }
this.$store.commit('removePlaceholderNotes');
}) })
.catch(this.handleError); .catch(this.handleError);
} }
...@@ -153,33 +121,6 @@ export default { ...@@ -153,33 +121,6 @@ export default {
} }
} }
}, },
getQuickActionText() {
let text = 'Applying command';
const quickActions = AjaxCache.get(gl.GfmAutoComplete.dataSources.commands);
const { note } = this;
const executedCommands = quickActions.filter((command) => {
const commandRegex = new RegExp(`/${command.name}`);
return commandRegex.test(note);
});
if (executedCommands && executedCommands.length) {
if (executedCommands.length > 1) {
text = 'Applying multiple commands';
} else {
const commandDescription = executedCommands[0].description.toLowerCase();
text = `Applying command to ${commandDescription}`;
}
}
return text;
},
hasQuickActions() {
return REGEX_QUICK_ACTIONS.test(this.note);
},
stripQuickActions() {
return this.note.replace(REGEX_QUICK_ACTIONS, '').trim();
},
}, },
mounted() { mounted() {
const issuableDataEl = document.getElementById('js-issuable-app-initial-data'); const issuableDataEl = document.getElementById('js-issuable-app-initial-data');
......
...@@ -63,9 +63,9 @@ export default { ...@@ -63,9 +63,9 @@ export default {
this.isReplying = false; this.isReplying = false;
}, },
saveReply({ note }) { saveReply({ note }) {
const data = { const replyData = {
endpoint: this.newNotePath, endpoint: this.newNotePath,
reply: { data: {
in_reply_to_discussion_id: this.note.reply_id, in_reply_to_discussion_id: this.note.reply_id,
target_type: 'issue', target_type: 'issue',
target_id: this.discussion.noteable_id, target_id: this.discussion.noteable_id,
...@@ -74,7 +74,7 @@ export default { ...@@ -74,7 +74,7 @@ export default {
}, },
}; };
this.$store.dispatch('replyToDiscussion', data) this.$store.dispatch('saveNote', replyData)
.then(() => { .then(() => {
this.isReplying = false; this.isReplying = false;
}) })
......
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
import service from '../services/issue_notes_service'; import service from '../services/issue_notes_service';
import utils from './issue_notes_utils';
const findNoteObjectById = (notes, id) => notes.filter(n => n.id === id)[0]; const findNoteObjectById = (notes, id) => notes.filter(n => n.id === id)[0];
...@@ -147,31 +148,31 @@ const actions = { ...@@ -147,31 +148,31 @@ const actions = {
context.commit('deleteNote', note); context.commit('deleteNote', note);
}); });
}, },
replyToDiscussion(context, data) { updateNote(context, data) {
const { endpoint, reply } = data; const { endpoint, note } = data;
return service return service
.replyToDiscussion(endpoint, reply) .updateNote(endpoint, note)
.then(res => res.json()) .then(res => res.json())
.then((res) => { .then((res) => {
context.commit('addNewReplyToDiscussion', res); context.commit('updateNote', res);
}); });
}, },
updateNote(context, data) { replyToDiscussion(context, noteData) {
const { endpoint, note } = data; const { endpoint, data } = noteData;
return service return service
.updateNote(endpoint, note) .replyToDiscussion(endpoint, data)
.then(res => res.json()) .then(res => res.json())
.then((res) => { .then((res) => {
context.commit('updateNote', res); context.commit('addNewReplyToDiscussion', res);
}); });
}, },
createNewNote(context, data) { createNewNote(context, noteData) {
const { endpoint, noteData } = data; const { endpoint, data } = noteData;
return service return service
.createNewNote(endpoint, noteData) .createNewNote(endpoint, data)
.then(res => res.json()) .then(res => res.json())
.then((res) => { .then((res) => {
if (!res.errors) { if (!res.errors) {
...@@ -180,6 +181,49 @@ const actions = { ...@@ -180,6 +181,49 @@ const actions = {
return res; return res;
}); });
}, },
saveNote(context, noteData) {
const { note } = noteData.data.note;
let placeholderText = note;
const hasQuickActions = utils.hasQuickActions(placeholderText);
if (hasQuickActions) {
placeholderText = utils.stripQuickActions(placeholderText);
}
if (placeholderText.length) {
context.commit('showPlaceholderNote', {
noteBody: placeholderText,
});
}
if (hasQuickActions) {
context.commit('showPlaceholderNote', {
isSystemNote: true,
noteBody: utils.getQuickActionText(note),
});
}
const hasReplyId = noteData.data.in_reply_to_discussion_id;
const methodToDispatch = hasReplyId ? 'replyToDiscussion' : 'createNewNote';
return context.dispatch(methodToDispatch, noteData)
.then((res) => {
const { errors } = res;
if (hasQuickActions) {
context.dispatch('poll');
$('.js-gfm-input').trigger('clear-commands-cache.atwho');
new Flash('Commands applied', 'notice', $(noteData.flashContainer)); // eslint-disable-line
}
if (errors && errors.commands_only) {
new Flash(errors.commands_only, 'notice', $(noteData.flashContainer)); // eslint-disable-line
}
context.commit('removePlaceholderNotes');
return res;
});
},
poll(context) { poll(context) {
const { notesPath } = $('.js-notes-wrapper')[0].dataset; const { notesPath } = $('.js-notes-wrapper')[0].dataset;
......
import AjaxCache from '~/lib/utils/ajax_cache';
const REGEX_QUICK_ACTIONS = /^\/\w+.*$/gm;
export default {
getQuickActionText(note) {
let text = 'Applying command';
const quickActions = AjaxCache.get(gl.GfmAutoComplete.dataSources.commands);
const executedCommands = quickActions.filter((command) => {
const commandRegex = new RegExp(`/${command.name}`);
return commandRegex.test(note);
});
if (executedCommands && executedCommands.length) {
if (executedCommands.length > 1) {
text = 'Applying multiple commands';
} else {
const commandDescription = executedCommands[0].description.toLowerCase();
text = `Applying command to ${commandDescription}`;
}
}
return text;
},
hasQuickActions(note) {
return REGEX_QUICK_ACTIONS.test(note);
},
stripQuickActions(note) {
return note.replace(REGEX_QUICK_ACTIONS, '').trim();
},
}
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