Fix delete mutation update

This commit is contained in:
Ramon Wenger 2022-02-02 16:53:23 +01:00
parent 63d79894ae
commit 6d1eb48fe5
1 changed files with 13 additions and 9 deletions

View File

@ -84,6 +84,7 @@
import {hidden} from '@/helpers/visibility';
import {CONTENT_TYPE} from '@/consts/types';
import PopoverLink from '@/components/ui/PopoverLink';
import {removeAtIndex} from '@/graphql/immutable-operations';
const ContentComponent = () => import(/* webpackChunkName: "content-components" */'@/components/content-blocks/ContentComponent');
const instruments = {
@ -101,8 +102,8 @@
default: () => ({})
},
parent: {
type: String,
default: ''
type: Object,
default: () => ({})
},
editMode: {
type: Boolean,
@ -219,19 +220,22 @@
}
},
update(store, {data: {deleteContentBlock: {success}}}) {
try {
if (success) {
const query = CHAPTER_QUERY;
const variables = {
id: parent
id: parent.id
};
const {chapter} = store.readQuery({query, variables});
const index = chapter.contentBlocks.findIndex(contentBlock => contentBlock.id === id);
const contentBlocks = removeAtIndex(chapter.contentBlocks, index);
const data = {
chapter: {
...chapter,
contentBlocks
}
};
const data = store.readQuery({query, variables});
data.chapter.contentBlocks.splice(data.chapter.contentBlocks.findIndex(contentBlock => contentBlock.id === id), 1);
store.writeQuery({query, variables, data});
}
} catch (e) {
// Query did not exist in the cache, and apollo throws a generic Error. Do nothing
}
}
});
},