Add confirmation dialog to content block deletion

This commit is contained in:
Ramon Wenger 2022-03-01 11:02:26 +01:00
parent a993eeacfc
commit 1f268c62e1
1 changed files with 38 additions and 31 deletions

View File

@ -86,6 +86,7 @@
import PopoverLink from '@/components/ui/PopoverLink'; import PopoverLink from '@/components/ui/PopoverLink';
import {removeAtIndex} from '@/graphql/immutable-operations'; import {removeAtIndex} from '@/graphql/immutable-operations';
import {EDIT_CONTENT_BLOCK_PAGE} from '@/router/module.names'; import {EDIT_CONTENT_BLOCK_PAGE} from '@/router/module.names';
const ContentComponent = () => import(/* webpackChunkName: "content-components" */'@/components/content-blocks/ContentComponent'); const ContentComponent = () => import(/* webpackChunkName: "content-components" */'@/components/content-blocks/ContentComponent');
const instruments = { const instruments = {
@ -100,16 +101,16 @@
props: { props: {
contentBlock: { contentBlock: {
type: Object, type: Object,
default: () => ({}) default: () => ({}),
}, },
parent: { parent: {
type: Object, type: Object,
default: () => ({}) default: () => ({}),
}, },
editMode: { editMode: {
type: Boolean, type: Boolean,
default: true default: true,
} },
}, },
mixins: [me], mixins: [me],
@ -120,7 +121,7 @@
AddContentButton, AddContentButton,
VisibilityAction, VisibilityAction,
MoreOptionsWidget, MoreOptionsWidget,
UserWidget UserWidget,
}, },
computed: { computed: {
@ -178,46 +179,52 @@
} }
}, []); }, []);
return Object.assign({}, this.contentBlock, { return Object.assign({}, this.contentBlock, {
contents: newContent contents: newContent,
}); });
}, },
hidden() { hidden() {
return hidden({ return hidden({
block: this.contentBlock, block: this.contentBlock,
schoolClass: this.schoolClass, schoolClass: this.schoolClass,
type: CONTENT_TYPE type: CONTENT_TYPE,
}); });
}, },
root() { root() {
// we need the root content block id, not the generated content block if inside a content list block // we need the root content block id, not the generated content block if inside a content list block
return this.contentBlock.root ? this.contentBlock.root : this.contentBlock.id; return this.contentBlock.root ? this.contentBlock.root : this.contentBlock.id;
} },
}, },
methods: { methods: {
editContentBlock(contentBlock) { editContentBlock(contentBlock) {
const route = { const route = {
name: EDIT_CONTENT_BLOCK_PAGE, name: EDIT_CONTENT_BLOCK_PAGE,
params: { params: {
id: contentBlock.id id: contentBlock.id,
} },
}; };
this.$router.push(route); this.$router.push(route);
}, },
deleteContentBlock(contentBlock) { deleteContentBlock(contentBlock) {
this.$modal.open('confirm').then(() => {
this.doDeleteContentBlock(contentBlock);
})
.catch();
},
doDeleteContentBlock(contentBlock) {
const parent = this.parent; const parent = this.parent;
const id = contentBlock.id; const id = contentBlock.id;
this.$apollo.mutate({ this.$apollo.mutate({
mutation: DELETE_CONTENT_BLOCK_MUTATION, mutation: DELETE_CONTENT_BLOCK_MUTATION,
variables: { variables: {
input: { input: {
id id,
} },
}, },
update(store, {data: {deleteContentBlock: {success}}}) { update(store, {data: {deleteContentBlock: {success}}}) {
if (success) { if (success) {
const query = CHAPTER_QUERY; const query = CHAPTER_QUERY;
const variables = { const variables = {
id: parent.id id: parent.id,
}; };
const {chapter} = store.readQuery({query, variables}); const {chapter} = store.readQuery({query, variables});
const index = chapter.contentBlocks.findIndex(contentBlock => contentBlock.id === id); const index = chapter.contentBlocks.findIndex(contentBlock => contentBlock.id === id);
@ -225,22 +232,22 @@
const data = { const data = {
chapter: { chapter: {
...chapter, ...chapter,
contentBlocks contentBlocks,
} },
}; };
store.writeQuery({query, variables, data}); store.writeQuery({query, variables, data});
} }
} },
}); });
}, },
createContentListOrBlocks(contentList) { createContentListOrBlocks(contentList) {
return [{ return [{
type: 'content_list', type: 'content_list',
contents: contentList, contents: contentList,
id: contentList[0].id id: contentList[0].id,
}]; }];
}, },
} },
}; };
</script> </script>