Update last module in a mutation instead of the query
This commit is contained in:
parent
09d20e346d
commit
6853cc8734
|
|
@ -7,7 +7,7 @@
|
||||||
data-cy="toggle-enable-solutions"></toggle-solutions-for-module>
|
data-cy="toggle-enable-solutions"></toggle-solutions-for-module>
|
||||||
|
|
||||||
<h2 class="module__meta-title" id="meta-title">{{module.metaTitle}}</h2>
|
<h2 class="module__meta-title" id="meta-title">{{module.metaTitle}}</h2>
|
||||||
<h1 class="module__title">{{module.title}}</h1>
|
<h1 class="module__title" data-cy="module-title">{{module.title}}</h1>
|
||||||
<img
|
<img
|
||||||
:src="module.heroImage"
|
:src="module.heroImage"
|
||||||
alt="" class="module__hero">
|
alt="" class="module__hero">
|
||||||
|
|
@ -40,6 +40,7 @@
|
||||||
import ToggleSolutionsForModule from '@/components/ToggleSolutionsForModule.vue';
|
import ToggleSolutionsForModule from '@/components/ToggleSolutionsForModule.vue';
|
||||||
|
|
||||||
import UPDATE_OBJECTIVE_PROGRESS_MUTATION from '@/graphql/gql/mutations/updateObjectiveProgress.gql';
|
import UPDATE_OBJECTIVE_PROGRESS_MUTATION from '@/graphql/gql/mutations/updateObjectiveProgress.gql';
|
||||||
|
import UPDATE_LAST_MODULE_MUTATION from '@/graphql/gql/mutations/updateLastModule.gql';
|
||||||
import OBJECTIVE_QUERY from '@/graphql/gql/objectiveQuery.gql';
|
import OBJECTIVE_QUERY from '@/graphql/gql/objectiveQuery.gql';
|
||||||
import ME_QUERY from '@/graphql/gql/meQuery.gql';
|
import ME_QUERY from '@/graphql/gql/meQuery.gql';
|
||||||
|
|
||||||
|
|
@ -62,6 +63,7 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
created() {
|
created() {
|
||||||
|
this.updateLastVisitedModule(this.module.id)
|
||||||
},
|
},
|
||||||
|
|
||||||
computed: {
|
computed: {
|
||||||
|
|
@ -81,6 +83,26 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
|
updateLastVisitedModule(moduleId) {
|
||||||
|
this.$apollo.mutate({
|
||||||
|
mutation: UPDATE_LAST_MODULE_MUTATION,
|
||||||
|
variables: {
|
||||||
|
input: {
|
||||||
|
id: moduleId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
update(store, {data: {updateLastModule: {module}}}) {
|
||||||
|
if (module) {
|
||||||
|
const data = store.readQuery({query: ME_QUERY});
|
||||||
|
if (data) {
|
||||||
|
console.log(data);
|
||||||
|
data.me.lastModule = module;
|
||||||
|
store.writeQuery({query: ME_QUERY, data});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
updateObjectiveProgress(done, objectiveId) {
|
updateObjectiveProgress(done, objectiveId) {
|
||||||
this.$apollo.mutate({
|
this.$apollo.mutate({
|
||||||
mutation: UPDATE_OBJECTIVE_PROGRESS_MUTATION,
|
mutation: UPDATE_OBJECTIVE_PROGRESS_MUTATION,
|
||||||
|
|
@ -91,18 +113,14 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
update(store, {data: {updateObjectiveProgress: {objective}}}) {
|
update(store, {data: {updateObjectiveProgress: {objective}}}) {
|
||||||
try {
|
if (objective) {
|
||||||
if (objective) {
|
const variables = {id: objectiveId};
|
||||||
const variables = {id: objectiveId};
|
const query = OBJECTIVE_QUERY;
|
||||||
const query = OBJECTIVE_QUERY;
|
const data = store.readQuery({query, variables});
|
||||||
const data = store.readQuery({query, variables});
|
if (data && data.objective.objectiveProgress.edges.length > 0) {
|
||||||
if (data.objective.objectiveProgress.edges.length > 0) {
|
data.objective.objectiveProgress.edges[0].node.done = done;
|
||||||
data.objective.objectiveProgress.edges[0].node.done = done;
|
|
||||||
}
|
|
||||||
store.writeQuery({query: OBJECTIVE_QUERY, data, variables});
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
store.writeQuery({query: OBJECTIVE_QUERY, data, variables});
|
||||||
// Query did not exist in the cache, and apollo throws a generic Error. Do nothing
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
#import "../fragments/moduleParts.gql"
|
||||||
|
mutation UpdateLastModule($input: UpdateLastModuleInput!) {
|
||||||
|
updateLastModule(input: $input) {
|
||||||
|
module {
|
||||||
|
...ModuleParts
|
||||||
|
}
|
||||||
|
errors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
mutation UpdateObjectiveProgressInput($input: UpdateObjectiveProgressInput!) {
|
mutation UpdateObjectiveProgress($input: UpdateObjectiveProgressInput!) {
|
||||||
updateObjectiveProgress(input: $input){
|
updateObjectiveProgress(input: $input){
|
||||||
objective {
|
objective {
|
||||||
id
|
id
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
from books.schema.mutations.contentblock import MutateContentBlock, AddContentBlock, DeleteContentBlock
|
from books.schema.mutations.contentblock import MutateContentBlock, AddContentBlock, DeleteContentBlock
|
||||||
from books.schema.mutations.module import UpdateSolutionVisibility
|
from books.schema.mutations.module import UpdateSolutionVisibility, UpdateLastModule
|
||||||
|
|
||||||
|
|
||||||
class BookMutations(object):
|
class BookMutations(object):
|
||||||
|
|
@ -7,3 +7,4 @@ class BookMutations(object):
|
||||||
add_content_block = AddContentBlock.Field()
|
add_content_block = AddContentBlock.Field()
|
||||||
delete_content_block = DeleteContentBlock.Field()
|
delete_content_block = DeleteContentBlock.Field()
|
||||||
update_solution_visibility = UpdateSolutionVisibility.Field()
|
update_solution_visibility = UpdateSolutionVisibility.Field()
|
||||||
|
update_last_module = UpdateLastModule.Field()
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ from graphene import relay
|
||||||
|
|
||||||
from api.utils import get_errors, get_object
|
from api.utils import get_errors, get_object
|
||||||
from books.models import Module
|
from books.models import Module
|
||||||
|
from books.schema.queries import ModuleNode
|
||||||
|
|
||||||
|
|
||||||
class UpdateSolutionVisibility(relay.ClientIDMutation):
|
class UpdateSolutionVisibility(relay.ClientIDMutation):
|
||||||
|
|
@ -38,3 +39,33 @@ class UpdateSolutionVisibility(relay.ClientIDMutation):
|
||||||
errors = ['Error: {}'.format(e)]
|
errors = ['Error: {}'.format(e)]
|
||||||
|
|
||||||
return cls(success=False, solutions_enabled=None, errors=errors)
|
return cls(success=False, solutions_enabled=None, errors=errors)
|
||||||
|
|
||||||
|
|
||||||
|
class UpdateLastModule(relay.ClientIDMutation):
|
||||||
|
class Input:
|
||||||
|
id = graphene.ID()
|
||||||
|
|
||||||
|
module = graphene.Field(ModuleNode)
|
||||||
|
errors = graphene.List(graphene.String)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def mutate_and_get_payload(cls, root, info, **args):
|
||||||
|
try:
|
||||||
|
user = info.context.user
|
||||||
|
id = args.get('id')
|
||||||
|
|
||||||
|
module = get_object(Module, id)
|
||||||
|
if not module:
|
||||||
|
raise Module.DoesNotExist
|
||||||
|
|
||||||
|
user.last_module = module
|
||||||
|
user.save()
|
||||||
|
|
||||||
|
return cls(module=module)
|
||||||
|
|
||||||
|
except Module.DoesNotExist:
|
||||||
|
return cls(errors=['Module not found'])
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
errors = ['Error: {}'.format(e)]
|
||||||
|
return cls(errors=errors)
|
||||||
|
|
|
||||||
|
|
@ -199,8 +199,6 @@ class BookQuery(object):
|
||||||
def resolve_module(self, info, **kwargs):
|
def resolve_module(self, info, **kwargs):
|
||||||
slug = kwargs.get('slug')
|
slug = kwargs.get('slug')
|
||||||
id = kwargs.get('id')
|
id = kwargs.get('id')
|
||||||
# save last visited module in user
|
|
||||||
user = info.context.user
|
|
||||||
module = None
|
module = None
|
||||||
|
|
||||||
if id is not None:
|
if id is not None:
|
||||||
|
|
@ -209,10 +207,6 @@ class BookQuery(object):
|
||||||
elif slug is not None:
|
elif slug is not None:
|
||||||
module = Module.objects.get(slug=slug)
|
module = Module.objects.get(slug=slug)
|
||||||
|
|
||||||
if module is not None:
|
|
||||||
user.last_module = module
|
|
||||||
user.save()
|
|
||||||
|
|
||||||
return module
|
return module
|
||||||
|
|
||||||
def resolve_topic(self, info, **kwargs):
|
def resolve_topic(self, info, **kwargs):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue