Add bookmark actions to instrument, implement instrument bookmark
This commit is contained in:
parent
c8a09badad
commit
2b40f81764
|
|
@ -39,8 +39,7 @@
|
|||
import Solution from '@/components/content-blocks/Solution';
|
||||
import BookmarkActions from '@/components/notes/BookmarkActions';
|
||||
|
||||
import UPDATE_CONTENT_BOOKMARK from '@/graphql/gql/mutations/updateContentBookmark.gql';
|
||||
import CONTENT_BLOCK_QUERY from '@/graphql/gql/contentBlockQuery.gql';
|
||||
import {constructContentComponentBookmarkMutation} from '@/helpers/update-content-bookmark-mutation';
|
||||
|
||||
export default {
|
||||
props: ['component', 'parent', 'bookmarks', 'notes', 'root'],
|
||||
|
|
@ -91,56 +90,7 @@
|
|||
this.$store.dispatch('editNote', this.note);
|
||||
},
|
||||
bookmarkContent(uuid, bookmarked) {
|
||||
this.$apollo.mutate({
|
||||
mutation: UPDATE_CONTENT_BOOKMARK,
|
||||
variables: {
|
||||
input: {
|
||||
uuid,
|
||||
contentBlock: this.root,
|
||||
bookmarked
|
||||
}
|
||||
},
|
||||
update: (store, response) => {
|
||||
const query = CONTENT_BLOCK_QUERY;
|
||||
const variables = {id: this.root};
|
||||
const data = store.readQuery({
|
||||
query,
|
||||
variables
|
||||
});
|
||||
|
||||
const bookmarks = data.contentBlock.bookmarks;
|
||||
|
||||
if (bookmarked) {
|
||||
bookmarks.push({
|
||||
note: null,
|
||||
uuid: uuid,
|
||||
__typename: 'ContentBlockBookmarkNode'
|
||||
});
|
||||
} else {
|
||||
let index = bookmarks.findIndex(element => {
|
||||
return element.uuid === uuid;
|
||||
});
|
||||
if (index > -1) {
|
||||
bookmarks.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
data.contentBlock.bookmarks = bookmarks;
|
||||
|
||||
store.writeQuery({
|
||||
data,
|
||||
query,
|
||||
variables
|
||||
});
|
||||
},
|
||||
optimisticResponse: {
|
||||
__typename: 'Mutation',
|
||||
updateContentBookmark: {
|
||||
__typename: 'UpdateContentBookmarkPayload',
|
||||
success: true
|
||||
}
|
||||
}
|
||||
});
|
||||
this.$apollo.mutate(constructContentComponentBookmarkMutation(uuid, bookmarked, this.parent, this.root));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
mutation UpdateInstrumentBookmark($input: UpdateInstrumentBookmarkInput!) {
|
||||
updateInstrumentBookmark(input: $input) {
|
||||
success
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,113 @@
|
|||
import UPDATE_CONTENT_BOOKMARK from '@/graphql/gql/mutations/updateContentBookmark.gql';
|
||||
import UPDATE_INSTRUMENT_BOOKMARK from '@/graphql/gql/mutations/updateInstrumentBookmark.gql';
|
||||
import CONTENT_BLOCK_QUERY from '@/graphql/gql/contentBlockQuery.gql';
|
||||
import INSTRUMENT_QUERY from '@/graphql/gql/instrumentQueryById.gql';
|
||||
|
||||
export const constructContentComponentBookmarkMutation = (uuid, bookmarked, parent, root) => {
|
||||
let mutation = {};
|
||||
console.log(uuid, bookmarked, parent, root);
|
||||
if (parent.__typename === 'ContentBlockNode') {
|
||||
mutation = {
|
||||
mutation: UPDATE_CONTENT_BOOKMARK,
|
||||
variables: {
|
||||
input: {
|
||||
uuid,
|
||||
contentBlock: root,
|
||||
bookmarked
|
||||
}
|
||||
},
|
||||
update: (store, response) => {
|
||||
const query = CONTENT_BLOCK_QUERY;
|
||||
const variables = {id: root};
|
||||
const data = store.readQuery({
|
||||
query,
|
||||
variables
|
||||
});
|
||||
|
||||
const bookmarks = data.contentBlock.bookmarks;
|
||||
|
||||
if (bookmarked) {
|
||||
bookmarks.push({
|
||||
note: null,
|
||||
uuid,
|
||||
__typename: 'ContentBlockBookmarkNode'
|
||||
});
|
||||
} else {
|
||||
let index = bookmarks.findIndex(element => {
|
||||
return element.uuid === uuid;
|
||||
});
|
||||
if (index > -1) {
|
||||
bookmarks.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
data.contentBlock.bookmarks = bookmarks;
|
||||
|
||||
store.writeQuery({
|
||||
data,
|
||||
query,
|
||||
variables
|
||||
});
|
||||
},
|
||||
optimisticResponse: {
|
||||
__typename: 'Mutation',
|
||||
updateContentBookmark: {
|
||||
__typename: 'UpdateContentBookmarkPayload',
|
||||
success: true
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mutation = {
|
||||
mutation: UPDATE_INSTRUMENT_BOOKMARK,
|
||||
variables: {
|
||||
input: {
|
||||
uuid,
|
||||
instrument: root,
|
||||
bookmarked
|
||||
}
|
||||
},
|
||||
update: (store, response) => {
|
||||
const query = INSTRUMENT_QUERY;
|
||||
const variables = {id: root};
|
||||
const data = store.readQuery({
|
||||
query,
|
||||
variables
|
||||
});
|
||||
|
||||
const bookmarks = data.instrument.bookmarks;
|
||||
|
||||
if (bookmarked) {
|
||||
bookmarks.push({
|
||||
note: null,
|
||||
uuid,
|
||||
__typename: 'InstrumentBookmarkNode'
|
||||
})
|
||||
} else {
|
||||
let index = bookmarks.findIndex(element => {
|
||||
return element.uuid === uuid;
|
||||
});
|
||||
if (index > -1) {
|
||||
bookmarks.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
data.instrument.bookmarks = bookmarks;
|
||||
|
||||
store.writeQuery({
|
||||
data,
|
||||
query,
|
||||
variables
|
||||
});
|
||||
},
|
||||
optimisticResponse: {
|
||||
__typename: 'Mutation',
|
||||
updateInstrumentBookmark: {
|
||||
__typename: 'UpdateInstrumentBookmarkPayload',
|
||||
success: true
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return mutation;
|
||||
};
|
||||
|
|
@ -2,11 +2,15 @@
|
|||
<div class="instrument">
|
||||
<h1 class="instrument__title">{{instrument.title}}</h1>
|
||||
|
||||
<component v-for="component in instrument.contents"
|
||||
:key="component.id"
|
||||
:is="component.type"
|
||||
v-bind="component">
|
||||
</component>
|
||||
<content-component v-for="component in instrument.contents"
|
||||
:key="component.id"
|
||||
:component="component"
|
||||
:root="instrument.id"
|
||||
:parent="instrument"
|
||||
:bookmarks="instrument.bookmarks"
|
||||
:notes="instrument.notes"
|
||||
>
|
||||
</content-component>
|
||||
|
||||
</div>
|
||||
</template>
|
||||
|
|
@ -14,17 +18,7 @@
|
|||
<script>
|
||||
import INSTRUMENT_QUERY from '@/graphql/gql/instrumentQuery.gql';
|
||||
|
||||
import TextBlock from '@/components/content-blocks/TextBlock';
|
||||
import InstrumentWidget from '@/components/content-blocks/InstrumentWidget';
|
||||
import ImageBlock from '@/components/content-blocks/ImageBlock';
|
||||
import ImageUrlBlock from '@/components/content-blocks/ImageUrlBlock';
|
||||
import VideoBlock from '@/components/content-blocks/VideoBlock';
|
||||
import LinkBlock from '@/components/content-blocks/LinkBlock';
|
||||
import DocumentBlock from '@/components/content-blocks/DocumentBlock';
|
||||
import SectionTitleBlock from '@/components/content-blocks/SectionTitleBlock';
|
||||
import SubtitleBlock from '@/components/content-blocks/SubtitleBlock';
|
||||
import GeniallyBlock from '@/components/content-blocks/GeniallyBlock';
|
||||
import ThinglinkBlock from '@/components/content-blocks/ThinglinkBlock';
|
||||
import ContentComponent from '@/components/content-blocks/ContentComponent';
|
||||
|
||||
export default {
|
||||
apollo: {
|
||||
|
|
@ -39,18 +33,7 @@
|
|||
},
|
||||
|
||||
components: {
|
||||
'text_block': TextBlock,
|
||||
'basic_knowledge': InstrumentWidget, // for legacy
|
||||
'instrument': InstrumentWidget,
|
||||
'image_block': ImageBlock,
|
||||
'image_url_block': ImageUrlBlock,
|
||||
'video_block': VideoBlock,
|
||||
'link_block': LinkBlock,
|
||||
'document_block': DocumentBlock,
|
||||
'section_title': SectionTitleBlock,
|
||||
'subtitle': SubtitleBlock,
|
||||
'genially_block': GeniallyBlock,
|
||||
'thinglink_block': ThinglinkBlock
|
||||
ContentComponent
|
||||
},
|
||||
|
||||
data() {
|
||||
|
|
|
|||
|
|
@ -6,9 +6,10 @@ from graphene import relay
|
|||
from graphql_relay import from_global_id
|
||||
|
||||
from api.utils import get_object
|
||||
from basicknowledge.models import BasicKnowledge
|
||||
from books.models import ContentBlock, Chapter, Module
|
||||
from notes.inputs import AddNoteArgument, UpdateNoteArgument
|
||||
from notes.models import ContentBlockBookmark, Note, ChapterBookmark, ModuleBookmark
|
||||
from notes.models import ContentBlockBookmark, Note, ChapterBookmark, ModuleBookmark, InstrumentBookmark
|
||||
from notes.schema import NoteNode
|
||||
|
||||
|
||||
|
|
@ -172,9 +173,43 @@ class UpdateModuleBookmark(relay.ClientIDMutation):
|
|||
return cls(success=True)
|
||||
|
||||
|
||||
class UpdateInstrumentBookmark(relay.ClientIDMutation):
|
||||
class Input:
|
||||
uuid = graphene.UUID(required=True)
|
||||
instrument = graphene.ID(required=True)
|
||||
bookmarked = graphene.Boolean(required=True)
|
||||
|
||||
success = graphene.Boolean()
|
||||
|
||||
@classmethod
|
||||
def mutate_and_get_payload(cls, root, info, **kwargs):
|
||||
user = info.context.user
|
||||
instrument_id = kwargs.get('instrument')
|
||||
uuid = kwargs.get('uuid')
|
||||
bookmarked = kwargs.get('bookmarked')
|
||||
|
||||
instrument = get_object(BasicKnowledge, instrument_id)
|
||||
|
||||
if bookmarked:
|
||||
InstrumentBookmark.objects.create(
|
||||
instrument=instrument,
|
||||
uuid=uuid,
|
||||
user=user
|
||||
)
|
||||
else:
|
||||
InstrumentBookmark.objects.get(
|
||||
instrument=instrument,
|
||||
uuid=uuid,
|
||||
user=user
|
||||
).delete()
|
||||
|
||||
return cls(success=True)
|
||||
|
||||
|
||||
class NoteMutations:
|
||||
add_note = AddNote.Field()
|
||||
update_note = UpdateNote.Field()
|
||||
update_content_bookmark = UpdateContentBookmark.Field()
|
||||
update_chapter_bookmark = UpdateChapterBookmark.Field()
|
||||
update_module_bookmark = UpdateModuleBookmark.Field()
|
||||
update_instrument_bookmark = UpdateInstrumentBookmark.Field()
|
||||
|
|
|
|||
Loading…
Reference in New Issue