Update bookmark in module

This commit is contained in:
Ramon Wenger 2022-01-30 00:14:45 +01:00
parent bf89fba212
commit 12f16c3971
9 changed files with 114 additions and 35 deletions

View File

@ -82,7 +82,7 @@ export default {
}), }),
ModuleNode: () => ({ ModuleNode: () => ({
title: 'Module Title', title: 'Module Title',
slug: 'some slug', slug: 'some-slug',
metaTitle: 'Meta Title', metaTitle: 'Meta Title',
heroImage: '', heroImage: '',
teaser: '', teaser: '',
@ -90,6 +90,7 @@ export default {
assignments: [], assignments: [],
objectiveGroups: [], objectiveGroups: [],
id: getModuleId(), id: getModuleId(),
bookmark: null
}), }),
TopicNode: () => ({ TopicNode: () => ({
modules: [], modules: [],

View File

@ -15,6 +15,7 @@ describe('Bookmarks', () => {
ModuleDetailsQuery: { ModuleDetailsQuery: {
module: { module: {
...minimalModule, ...minimalModule,
slug: 'my-module-slug',
chapters: [ chapters: [
{ {
title: 'My super Chapter', title: 'My super Chapter',
@ -41,6 +42,11 @@ describe('Bookmarks', () => {
success: true success: true
} }
}, },
UpdateModuleBookmark: {
updateModuleBookmark: {
success: true
}
},
UpdateChapterBookmark: { UpdateChapterBookmark: {
updateChapterBookmark: { updateChapterBookmark: {
success: true success: true
@ -65,7 +71,29 @@ describe('Bookmarks', () => {
}); });
it('should bookmark module', () => { it('should bookmark module', () => {
cy.visit(); cy.visit('/module/lohn-und-budget/');
cy.getByDataCy('module-bookmark-actions').as('moduleBookmark');
cy.get('@moduleBookmark').within(() => {
cy.getByDataCy('bookmark-action').click();
cy.getByDataCy('add-note-action').click();
});
cy.get('[data-cy=bookmark-note]').within(() => {
cy.get('.skillbox-input').type('Hallo Velo');
});
cy.get('[data-cy=modal-save-button]').click();
cy.get('@moduleBookmark').within(() => {
cy.getByDataCy('edit-note-action').click();
});
cy.get('[data-cy=bookmark-note]').within(() => {
cy.get('.skillbox-input').clear().type('Hello Bike');
});
cy.get('[data-cy=modal-save-button]').click();
}); });
it('should bookmark chapter', () => { it('should bookmark chapter', () => {

View File

@ -27,6 +27,7 @@
:bookmarked="module.bookmark" :bookmarked="module.bookmark"
:note="note" :note="note"
class="module__bookmark-actions" class="module__bookmark-actions"
data-cy="module-bookmark-actions"
@add-note="$emit('addNote')" @add-note="$emit('addNote')"
@edit-note="$emit('editNote')" @edit-note="$emit('editNote')"
@bookmark="$emit('bookmark', !module.bookmark)" @bookmark="$emit('bookmark', !module.bookmark)"

View File

@ -52,8 +52,7 @@
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
@import "@/styles/_variables.scss"; @import "~styles/helpers";
@import "@/styles/_mixins.scss";
.bookmark-actions { .bookmark-actions {
height: 100%; height: 100%;

View File

@ -40,7 +40,6 @@
content, content,
block, block,
text, text,
type
}; };
} else { } else {
note = { note = {
@ -48,6 +47,9 @@
text text
}; };
} }
if(type){
note.type = type;
}
this.$apollo this.$apollo
.mutate(constructNoteMutation(note)) .mutate(constructNoteMutation(note))

View File

@ -3,8 +3,9 @@ import CONTENT_BLOCK_QUERY from '@/graphql/gql/queries/contentBlockQuery.gql';
import CHAPTER_QUERY from '@/graphql/gql/queries/chapterQuery.gql'; import CHAPTER_QUERY from '@/graphql/gql/queries/chapterQuery.gql';
import MODULE_QUERY from '@/graphql/gql/queries/modules/moduleDetailsQuery.gql'; import MODULE_QUERY from '@/graphql/gql/queries/modules/moduleDetailsQuery.gql';
import INSTRUMENT_FRAGMENT from '@/graphql/gql/fragments/instrumentParts.gql'; import INSTRUMENT_FRAGMENT from '@/graphql/gql/fragments/instrumentParts.gql';
import gql from 'graphql-tag';
import MODULE_FRAGMENT from '@/graphql/gql/fragments/moduleParts.gql';
const getBlockType = id => atob(id).split(':')[0];
const compareUuid = note => element => element.uuid === note.content; const compareUuid = note => element => element.uuid === note.content;
export const constructNoteMutation = (n) => { export const constructNoteMutation = (n) => {
@ -80,32 +81,64 @@ export const constructNoteMutation = (n) => {
}; };
} else { // it's a chapter bookmark or a module bookmark } else { // it's a chapter bookmark or a module bookmark
update = (store, {data: {addNote: {note}}}) => { update = (store, {data: {addNote: {note}}}) => {
const type = getBlockType(n.parent) === 'ChapterNode' ? 'chapter' : 'module'; const type = n.type === 'module' ? 'module' : 'chapter';
const query = type === 'chapter' ? CHAPTER_QUERY : MODULE_QUERY; const isChapter = type === 'chapter';
const variables = {id: n.parent}; let query;
const fromStore = store.readQuery({ let variables;
query, let fromStore;
variables if(isChapter) {
}); variables = {id: n.parent};
query = CHAPTER_QUERY;
fromStore = store.readQuery({
query,
variables
});
const entity = fromStore[type]; console.log('fromStore', fromStore);
const entity = fromStore[type];
let bookmark = {
...entity.bookmark,
note
};
const data = {
[type]: {
...entity,
bookmark
}
};
store.writeQuery({
data,
query,
variables
});
} else {
let fragment = MODULE_FRAGMENT;
let id = store.identify({
__typename: 'ModuleNode',
slug: n.parent
});
const module = store.readFragment({
fragment,
id
});
let bookmark = { let bookmark = {
...entity.bookmark, ...module.bookmark,
note note
}; };
let data = {
const data = { ...module,
[type]: { bookmark
...entity,
bookmark
}
}; };
store.writeFragment({
store.writeQuery({ fragment,
data, id,
query, data
variables
}); });
}
}; };
} }

View File

@ -137,21 +137,32 @@
}, },
update: (store) => { update: (store) => {
const fragment = MODULE_FRAGMENT; const fragment = MODULE_FRAGMENT;
const id = `ModuleNode:${slug}`; const id = store.identify({
const data = store.readFragment({ __typename: 'ModuleNode',
slug: slug
});
const module = store.readFragment({
fragment, fragment,
id, id,
}); });
let bookmark;
if (bookmarked) { if (bookmarked) {
data.bookmark = { bookmark = {
__typename: 'ModuleBookmarkNode', __typename: 'ModuleBookmarkNode',
note: null, note: null,
}; };
} else { } else {
data.bookmark = null; bookmark = null;
} }
const data = {
...module,
bookmark
};
store.writeFragment({ store.writeFragment({
data, data,
fragment, fragment,
@ -170,7 +181,8 @@
addNote(id) { addNote(id) {
this.$store.dispatch('addNote', { this.$store.dispatch('addNote', {
content: id, content: id,
parent: this.module.id, parent: this.module.slug,
type: 'module'
}); });
}, },
editNote() { editNote() {

View File

@ -140,11 +140,13 @@ export default new Vuex.Store({
addNote({commit, dispatch}, payload) { addNote({commit, dispatch}, payload) {
if (payload.block) { if (payload.block) {
commit('setCurrentNoteBlock', payload.block); commit('setCurrentNoteBlock', payload.block);
commit('setNoteType', payload.type);
commit('setCurrentContent', payload.content); commit('setCurrentContent', payload.content);
} else { } else {
commit('setCurrentNoteParent', payload.parent); commit('setCurrentNoteParent', payload.parent);
} }
if(payload.type) {
commit('setNoteType', payload.type);
}
dispatch('showModal', 'new-note-wizard'); dispatch('showModal', 'new-note-wizard');
}, },
editNote({commit, dispatch}, payload) { editNote({commit, dispatch}, payload) {

View File

@ -87,13 +87,14 @@ class AddNote(relay.ClientIDMutation):
user=user user=user
) )
else: else:
type, id = from_global_id(parent) type = note.get('type', 'chapter')
if type == 'ModuleNode': if type == 'module':
bookmark = ModuleBookmark.objects.get( bookmark = ModuleBookmark.objects.get(
module__id=id, module__slug=parent,
user=user user=user
) )
else: else:
_, id = from_global_id(parent)
bookmark = ChapterBookmark.objects.get( bookmark = ChapterBookmark.objects.get(
chapter__id=id, chapter__id=id,
user=user user=user