Add duplicate action to frontend

This commit is contained in:
Ramon Wenger 2022-09-21 16:43:51 +02:00
parent 6be6ab8092
commit 4693d2c01a
5 changed files with 86 additions and 6 deletions

View File

@ -61,6 +61,14 @@ describe('Custom Content Block', () => {
cy.log('Opening More Menu');
cy.getByDataCy('more-options-link').click();
cy.log('Duplicating Content Block');
cy.getByDataCy('duplicate-content-block-link').click();
cy.get('.content-block').should('have.length', 2);
cy.log('Opening More Menu');
cy.getByDataCy('more-options-link').click();
// check if content block is still there
cy.log('Deleting Content Block');
cy.getByDataCy('delete-content-block-link').click();

View File

@ -11,14 +11,27 @@
>
<div
class="block-actions"
v-if="canEditContentBlock && editMode"
v-if="canEditModule"
>
<user-widget
v-bind="me"
class="block-actions__user-widget content-block__user-widget"
v-if="isMine"
/>
<more-options-widget>
<li class="popover-links__link">
<li
class="popover-links__link"
>
<popover-link
data-cy="duplicate-content-block-link"
text="Duplizieren"
@link-action="duplicateContentBlock(contentBlock)"
/>
</li>
<li
class="popover-links__link"
v-if="isMine"
>
<popover-link
data-cy="delete-content-block-link"
text="Löschen"
@ -26,7 +39,10 @@
/>
</li>
<li class="popover-links__link">
<li
class="popover-links__link"
v-if="isMine"
>
<popover-link
text="Bearbeiten"
@link-action="editContentBlock(contentBlock)"
@ -83,13 +99,14 @@
import CHAPTER_QUERY from '@/graphql/gql/queries/chapterQuery.gql';
import DELETE_CONTENT_BLOCK_MUTATION from '@/graphql/gql/mutations/deleteContentBlock.gql';
import DUPLICATE_CONTENT_BLOCK_MUTATION from '@/graphql/gql/mutations/duplicateContentBlock.gql';
import me from '@/mixins/me';
import {hidden} from '@/helpers/visibility';
import {CONTENT_TYPE} from '@/consts/types';
import PopoverLink from '@/components/ui/PopoverLink';
import {removeAtIndex} from '@/graphql/immutable-operations';
import {insertAtIndex, removeAtIndex} from '@/graphql/immutable-operations';
import {EDIT_CONTENT_BLOCK_PAGE} from '@/router/module.names';
import {instrumentCategory} from '@/helpers/instrumentType';
@ -163,7 +180,10 @@
return {};
},
canEditContentBlock() {
return this.contentBlock.mine && !this.contentBlock.indent;
return this.isMine && !this.contentBlock.indent;
},
isMine() {
return this.contentBlock.mine;
},
contentBlocksWithContentLists() {
/*
@ -218,6 +238,36 @@
},
},
methods: {
duplicateContentBlock({id}) {
const parent = this.parent;
this.$apollo.mutate({
mutation: DUPLICATE_CONTENT_BLOCK_MUTATION,
variables: {
input: {
id,
},
},
update(store, {data: {duplicateContentBlock: {contentBlock}}}) {
if (contentBlock) {
const query = CHAPTER_QUERY;
const variables = {
id: parent.id,
};
const {chapter} = store.readQuery({query, variables});
const index = chapter.contentBlocks.findIndex(contentBlock => contentBlock.id === id);
const contentBlocks = insertAtIndex(chapter.contentBlocks, index, contentBlock);
const data = {
chapter: {
...chapter,
contentBlocks,
},
};
store.writeQuery({query, variables, data});
}
},
});
},
editContentBlock(contentBlock) {
const route = {
name: EDIT_CONTENT_BLOCK_PAGE,

View File

@ -0,0 +1,11 @@
#import "../fragments/contentBlockInterfaceParts.gql"
#import "../fragments/contentBlockParts.gql"
mutation DuplicateContentBlock($input: DuplicateContentBlockInput!) {
duplicateContentBlock(input: $input) {
contentBlock {
...ContentBlockInterfaceParts
...ContentBlockParts
}
}
}

View File

@ -15,7 +15,7 @@ class ContentBlockManager(PageManager):
user_created=True,
owner=user,
contents=content_block.contents,
title=content_block.title,
title=f'{content_block.title} (Kopie)',
type=content_block.type,
)
content_block.add_sibling(instance=new_content_block, pos='right')

View File

@ -478,6 +478,16 @@ type DjangoDebugSQL {
encoding: String
}
input DuplicateContentBlockInput {
id: ID!
clientMutationId: String
}
type DuplicateContentBlockPayload {
contentBlock: ContentBlockNode
clientMutationId: String
}
type DuplicateName {
reason: String
}
@ -694,6 +704,7 @@ type Mutation {
mutateContentBlock(input: MutateContentBlockInput!): MutateContentBlockPayload
addContentBlock(input: AddContentBlockInput!): AddContentBlockPayload
deleteContentBlock(input: DeleteContentBlockInput!): DeleteContentBlockPayload
duplicateContentBlock(input: DuplicateContentBlockInput!): DuplicateContentBlockPayload
updateSolutionVisibility(input: UpdateSolutionVisibilityInput!): UpdateSolutionVisibilityPayload
updateLastModule(input: UpdateLastModuleInput!): UpdateLastModulePayload
updateLastTopic(input: UpdateLastTopicInput!): UpdateLastTopicPayload