Add optimistic update for the new note wizard

This commit is contained in:
Ramon Wenger 2019-10-28 15:21:06 +01:00
parent c346cfef3f
commit 9b1c54cad2
3 changed files with 52 additions and 13 deletions

View File

@ -124,9 +124,11 @@
}); });
}, },
optimisticResponse: { optimisticResponse: {
note: null, __typename: 'Mutation',
uuid: uuid, updateContentBookmark: {
__typename: 'ContentBlockBookmarkNode' __typename: 'UpdateContentBookmarkPayload',
success: true
}
} }
}); });
}, },

View File

@ -6,7 +6,7 @@
import NoteForm from '@/components/notes/NoteForm'; import NoteForm from '@/components/notes/NoteForm';
import ADD_NOTE_MUTATION from '@/graphql/gql/mutations/addNote.gql'; import ADD_NOTE_MUTATION from '@/graphql/gql/mutations/addNote.gql';
import MODULE_DETAILS_QUERY from '@/graphql/gql/moduleDetailsQuery.gql'; import CONTENT_BLOCK_QUERY from '@/graphql/gql/contentBlockQuery.gql';
import {mapGetters} from 'vuex'; import {mapGetters} from 'vuex';
@ -27,23 +27,60 @@
methods: { methods: {
addNote(note) { addNote(note) {
const content = this.currentContent;
const contentBlock = this.currentContentBlock;
const text = note.text;
this.$apollo.mutate({ this.$apollo.mutate({
mutation: ADD_NOTE_MUTATION, mutation: ADD_NOTE_MUTATION,
variables: { variables: {
input: { input: {
note: { note: {
content: this.currentContent, content,
contentBlock: this.currentContentBlock, contentBlock,
text: note.text text
} }
} }
}, },
refetchQueries: [{ update: (store, {data: {addNote: {note}}}) => {
query: MODULE_DETAILS_QUERY, const query = CONTENT_BLOCK_QUERY;
variables: { const variables = {id: contentBlock};
slug: this.$route.params.slug const data = store.readQuery({
query,
variables
});
const bookmarks = data.contentBlock.bookmarks;
let index = bookmarks.findIndex(element => {
return element.uuid === content;
});
if (index > -1) {
let el = bookmarks[index];
el.note = note;
bookmarks.splice(index, 1, el);
} }
}]
data.contentBlock.bookmarks = bookmarks;
store.writeQuery({
data,
query,
variables
});
},
optimisticResponse: {
__typename: 'Mutation',
addNote: {
__typename: 'AddNotePayload',
note: {
__typename: 'NoteNode',
id: -1,
text: text
}
}
}
}).then(() => { }).then(() => {
this.$store.dispatch('hideModal'); this.$store.dispatch('hideModal');
}); });

View File

@ -7,7 +7,7 @@
<div slot="footer"> <div slot="footer">
<a class="button button--primary" data-cy="modal-save-button" <a class="button button--primary" data-cy="modal-save-button"
@click="$emit('save', localNote)">Speichern</a> @click="$emit('save', localNote)">Speichern</a>
<a class="button" @click="$emit('hide')">Löschen</a> <a class="button" @click="$emit('hide')">Abbrechen</a>
</div> </div>
</modal> </modal>