diff --git a/src/store/modules/lists.js b/src/store/modules/lists.js index abce934a..0db79750 100644 --- a/src/store/modules/lists.js +++ b/src/store/modules/lists.js @@ -8,10 +8,10 @@ export default { // The state is an object which has the list ids as keys. state: () => ({}), mutations: { - addList(state, list) { + setList(state, list) { Vue.set(state, list.id, list) }, - addLists(state, lists) { + setLists(state, lists) { lists.forEach(l => { Vue.set(state, l.id, l) }) @@ -28,16 +28,34 @@ export default { actions: { toggleListFavorite(ctx, list) { list.isFavorite = !list.isFavorite + + return ctx.dispatch('updateList', list) + }, + createList(ctx, list) { + const listService = new ListService() + + return listService.create(list) + .then(r => { + r.namespaceId = list.namespaceId + ctx.commit('namespaces/addListToNamespace', r, {root: true}) + ctx.commit('setList', r) + return Promise.resolve(r) + }) + .catch(e => { + return Promise.reject(e) + }) + }, + updateList(ctx, list) { const listService = new ListService() return listService.update(list) .then(r => { + ctx.commit('setList', r) + ctx.commit('namespaces/setListInNamespaceById', r, {root: true}) if (r.isFavorite) { - ctx.commit('addList', r) r.namespaceId = FavoriteListsNamespace ctx.commit('namespaces/addListToNamespace', r, {root: true}) } else { - ctx.commit('namespaces/setListInNamespaceById', r, {root: true}) r.namespaceId = FavoriteListsNamespace ctx.commit('namespaces/removeListFromNamespaceById', r, {root: true}) } @@ -48,23 +66,9 @@ export default { .catch(e => { // Reset the list state to the initial one to avoid confusion for the user list.isFavorite = !list.isFavorite - ctx.commit('addList', list) + ctx.commit('setList', list) return Promise.reject(e) }) - }, - createList(ctx, list) { - const listService = new ListService() - - return listService.create(list) - .then(r => { - r.namespaceId = list.namespaceId - ctx.commit('namespaces/addListToNamespace', r, {root: true}) - ctx.commit('addList', r) - return Promise.resolve(r) - }) - .catch(e => { - return Promise.reject(e) - }) - }, + } }, } \ No newline at end of file diff --git a/src/store/modules/namespaces.js b/src/store/modules/namespaces.js index 04d763cd..87ba1301 100644 --- a/src/store/modules/namespaces.js +++ b/src/store/modules/namespaces.js @@ -112,7 +112,7 @@ export default { }) }) - ctx.commit('lists/addLists', lists, {root: true}) + ctx.commit('lists/setLists', lists, {root: true}) return Promise.resolve(r) }) diff --git a/src/views/list/settings/duplicate.vue b/src/views/list/settings/duplicate.vue index 9bdd5899..d15cb6bc 100644 --- a/src/views/list/settings/duplicate.vue +++ b/src/views/list/settings/duplicate.vue @@ -45,7 +45,7 @@ export default { this.listDuplicateService.create(listDuplicate) .then(r => { this.$store.commit('namespaces/addListToNamespace', r.list) - this.$store.commit('lists/addList', r.list) + this.$store.commit('lists/setList', r.list) this.success({message: 'The list was successfully duplicated.'}, this) this.$router.push({name: 'list.index', params: {listId: r.list.id}}) }) diff --git a/src/views/list/settings/edit.vue b/src/views/list/settings/edit.vue index cea26d5b..ffc29c85 100644 --- a/src/views/list/settings/edit.vue +++ b/src/views/list/settings/edit.vue @@ -113,9 +113,8 @@ export default { }) }, save() { - this.listService.update(this.list) - .then(r => { - this.$store.commit('namespaces/setListInNamespaceById', r) + this.$store.dispatch('lists/updateList', this.list) + .then(() => { this.success({message: 'The list was successfully updated.'}, this) this.$router.back() })