Commit be0cc321 authored by Tom Quirk's avatar Tom Quirk Committed by Natalia Tepluhina

Adressing reviewer feedback

- explicitly pass no args to `mutate()`
- remove redundant `error` arg in scoped slot
- remove unnecessary double-incr. of `notesCount`
parent 2fdeab45
<script> <script>
import { ApolloMutation } from 'vue-apollo';
import Mousetrap from 'mousetrap'; import Mousetrap from 'mousetrap';
import createFlash from '~/flash'; import createFlash from '~/flash';
import { s__ } from '~/locale'; import { s__ } from '~/locale';
...@@ -14,10 +15,12 @@ import getDesignQuery from '../../graphql/queries/getDesign.query.graphql'; ...@@ -14,10 +15,12 @@ import getDesignQuery from '../../graphql/queries/getDesign.query.graphql';
import appDataQuery from '../../graphql/queries/appData.query.graphql'; import appDataQuery from '../../graphql/queries/appData.query.graphql';
import createImageDiffNoteMutation from '../../graphql/mutations/createImageDiffNote.mutation.graphql'; import createImageDiffNoteMutation from '../../graphql/mutations/createImageDiffNote.mutation.graphql';
import { extractDiscussions, extractDesign } from '../../utils/design_management_utils'; import { extractDiscussions, extractDesign } from '../../utils/design_management_utils';
import { updateStoreAfterAddImageDiffNote } from '../../utils/cache_update';
import { ADD_DISCUSSION_COMMENT_ERROR } from '../../utils/error_messages'; import { ADD_DISCUSSION_COMMENT_ERROR } from '../../utils/error_messages';
export default { export default {
components: { components: {
ApolloMutation,
DesignImage, DesignImage,
DesignOverlay, DesignOverlay,
DesignDiscussion, DesignDiscussion,
...@@ -44,7 +47,6 @@ export default { ...@@ -44,7 +47,6 @@ export default {
}, },
projectPath: '', projectPath: '',
issueId: '', issueId: '',
isNoteSaving: false,
}; };
}, },
apollo: { apollo: {
...@@ -102,6 +104,25 @@ export default { ...@@ -102,6 +104,25 @@ export default {
atVersion: this.designsVersion, atVersion: this.designsVersion,
}; };
}, },
mutationPayload() {
const { x, y, width, height } = this.annotationCoordinates;
return {
noteableId: this.design.id,
body: this.comment,
position: {
headSha: this.design.diffRefs.headSha,
baseSha: this.design.diffRefs.baseSha,
startSha: this.design.diffRefs.startSha,
x,
y,
width,
height,
paths: {
newPath: this.design.fullPath,
},
},
};
},
}, },
mounted() { mounted() {
Mousetrap.bind('esc', this.closeDesign); Mousetrap.bind('esc', this.closeDesign);
...@@ -110,80 +131,22 @@ export default { ...@@ -110,80 +131,22 @@ export default {
Mousetrap.unbind('esc', this.closeDesign); Mousetrap.unbind('esc', this.closeDesign);
}, },
methods: { methods: {
addImageDiffNote() { addImageDiffNoteToStore(
const { x, y, width, height } = this.annotationCoordinates; store,
this.isNoteSaving = true; {
return this.$apollo data: { createImageDiffNote },
.mutate({ },
mutation: createImageDiffNoteMutation, ) {
variables: { updateStoreAfterAddImageDiffNote(
input: { store,
noteableId: this.design.id, createImageDiffNote,
body: this.comment, getDesignQuery,
position: { this.designVariables,
headSha: this.design.diffRefs.headSha, );
baseSha: this.design.diffRefs.baseSha, },
startSha: this.design.diffRefs.startSha, onMutationError(e) {
x, createFlash(ADD_DISCUSSION_COMMENT_ERROR);
y, throw e;
width,
height,
paths: {
newPath: this.design.fullPath,
},
},
},
},
update: (store, { data: { createImageDiffNote } }) => {
const data = store.readQuery({
query: getDesignQuery,
variables: this.designVariables,
});
const newDiscussion = {
__typename: 'DiscussionEdge',
node: {
// False positive i18n lint: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/26
// eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings
__typename: 'Discussion',
id: createImageDiffNote.note.discussion.id,
replyId: createImageDiffNote.note.discussion.replyId,
notes: {
__typename: 'NoteConnection',
edges: [
{
__typename: 'NoteEdge',
node: createImageDiffNote.note,
},
],
},
},
};
const design = extractDesign(data);
design.discussions.edges = [...design.discussions.edges, newDiscussion];
design.notesCount += 1;
store.writeQuery({
query: getDesignQuery,
variables: this.designVariables,
data: {
...data,
design: {
...design,
notesCount: design.notesCount + 1,
},
},
});
},
})
.then(() => {
this.closeCommentForm();
})
.catch(e => {
createFlash(ADD_DISCUSSION_COMMENT_ERROR);
throw new Error(e);
})
.finally(() => {
this.isNoteSaving = false;
});
}, },
openCommentForm(position) { openCommentForm(position) {
const { x, y } = position; const { x, y } = position;
...@@ -215,6 +178,7 @@ export default { ...@@ -215,6 +178,7 @@ export default {
this.closeCommentForm(); this.closeCommentForm();
next(); next();
}, },
createImageDiffNoteMutation,
}; };
</script> </script>
...@@ -269,14 +233,25 @@ export default { ...@@ -269,14 +233,25 @@ export default {
:discussion-index="index + 1" :discussion-index="index + 1"
:markdown-preview-path="markdownPreviewPath" :markdown-preview-path="markdownPreviewPath"
/> />
<design-reply-form <apollo-mutation
v-if="annotationCoordinates" v-if="annotationCoordinates"
v-model="comment" v-slot="{ mutate, loading }"
:is-saving="isNoteSaving" :mutation="$options.createImageDiffNoteMutation"
:markdown-preview-path="markdownPreviewPath" :variables="{
@submitForm="addImageDiffNote" input: mutationPayload,
@cancelForm="closeCommentForm" }"
/> :update="addImageDiffNoteToStore"
@done="closeCommentForm"
@error="onMutationError"
>
<design-reply-form
v-model="comment"
:is-saving="loading"
:markdown-preview-path="markdownPreviewPath"
@submitForm="mutate()"
@cancelForm="closeCommentForm"
/>
</apollo-mutation>
</template> </template>
<h2 v-else class="new-discussion-disclaimer m-0"> <h2 v-else class="new-discussion-disclaimer m-0">
{{ __("Click the image where you'd like to start a new discussion") }} {{ __("Click the image where you'd like to start a new discussion") }}
......
...@@ -74,13 +74,10 @@ const addDiscussionCommentToStore = (store, createNote, query, queryVariables) = ...@@ -74,13 +74,10 @@ const addDiscussionCommentToStore = (store, createNote, query, queryVariables) =
}); });
}; };
const addImageDiffNoteToStore = (store, createImageDiffNote, query) => { const addImageDiffNoteToStore = (store, createImageDiffNote, query, variables) => {
const data = store.readQuery({ const data = store.readQuery({
query, query,
variables: { variables,
id: this.id,
version: this.designsVersion,
},
}); });
const newDiscussion = { const newDiscussion = {
__typename: 'DiscussionEdge', __typename: 'DiscussionEdge',
...@@ -101,9 +98,20 @@ const addImageDiffNoteToStore = (store, createImageDiffNote, query) => { ...@@ -101,9 +98,20 @@ const addImageDiffNoteToStore = (store, createImageDiffNote, query) => {
}, },
}, },
}; };
data.design.discussions.edges.push(newDiscussion); const design = extractDesign(data);
data.design.notesCount += 1; const notesCount = design.notesCount + 1;
store.writeQuery({ query, data }); design.discussions.edges = [...design.discussions.edges, newDiscussion];
store.writeQuery({
query,
variables,
data: {
...data,
design: {
...design,
notesCount,
},
},
});
}; };
const addNewDesignToStore = (store, designManagementUpload, query) => { const addNewDesignToStore = (store, designManagementUpload, query) => {
...@@ -186,11 +194,11 @@ export const updateStoreAfterAddDiscussionComment = (store, data, query, queryVa ...@@ -186,11 +194,11 @@ export const updateStoreAfterAddDiscussionComment = (store, data, query, queryVa
} }
}; };
export const updateStoreAfterAddImageDiffNote = (store, data, query) => { export const updateStoreAfterAddImageDiffNote = (store, data, query, queryVariables) => {
if (data.errors) { if (data.errors) {
onError(data, ADD_IMAGE_DIFF_NOTE_ERROR); onError(data, ADD_IMAGE_DIFF_NOTE_ERROR);
} else { } else {
addImageDiffNoteToStore(store, data, query); addImageDiffNoteToStore(store, data, query, queryVariables);
} }
}; };
......
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { ApolloMutation } from 'vue-apollo';
import DesignIndex from 'ee/design_management/pages/design/index.vue'; import DesignIndex from 'ee/design_management/pages/design/index.vue';
import DesignDiscussion from 'ee/design_management/components/design_notes/design_discussion.vue'; import DesignDiscussion from 'ee/design_management/components/design_notes/design_discussion.vue';
import DesignReplyForm from 'ee/design_management/components/design_notes/design_reply_form.vue'; import DesignReplyForm from 'ee/design_management/components/design_notes/design_reply_form.vue';
...@@ -57,6 +58,9 @@ describe('Design management design index page', () => { ...@@ -57,6 +58,9 @@ describe('Design management design index page', () => {
sync: false, sync: false,
propsData: { id: '1' }, propsData: { id: '1' },
mocks: { $apollo }, mocks: { $apollo },
stubs: {
ApolloMutation,
},
}); });
wrapper.setData({ wrapper.setData({
...@@ -122,7 +126,7 @@ describe('Design management design index page', () => { ...@@ -122,7 +126,7 @@ describe('Design management design index page', () => {
}); });
it('renders correct amount of discussions', () => { it('renders correct amount of discussions', () => {
expect(wrapper.findAll(DesignDiscussion).length).toBe(1); expect(findDiscussions().length).toBe(1);
}); });
}); });
...@@ -165,7 +169,7 @@ describe('Design management design index page', () => { ...@@ -165,7 +169,7 @@ describe('Design management design index page', () => {
findDiscussionForm().vm.$emit('submitForm'); findDiscussionForm().vm.$emit('submitForm');
expect(mutate).toHaveBeenCalledWith(mutationVariables); expect(mutate).toHaveBeenCalledWith(mutationVariables);
return wrapper.vm.addImageDiffNote(); return mutate({ variables: mutationVariables });
}) })
.then(() => { .then(() => {
expect(findDiscussionForm().exists()).toBe(false); expect(findDiscussionForm().exists()).toBe(false);
......
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