Skip to content

Commit a5cae0c

Browse files
authored
fix: update initial after save (#309)
* use set intead of setItem after a save request After a save request, the initial store should be updated too. * fix setAllMutation in some cases the method is called with just one item as payload. In this case we just have to update that item within the collection. * add changelog entry * linting
1 parent 1eb366f commit a5cae0c

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# next version
22

33
- fix listRelationships for "to one"-relationships
4+
- fix storing data on save in collections
45

56
# v0.0.35
67

src/module/actions/saveAction.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ export function saveAction (api, isCollection, moduleName, defaultQuery = {}) {
7070
const { data, status } = response
7171

7272
if (status === 204) {
73-
vuexFns.commit('setItem', getExpectedResponse(currentItemState))
73+
vuexFns.commit('set', getExpectedResponse(currentItemState))
7474
}
7575

7676
if (status === 200) {
7777
const isEmptyArray = Array.isArray(data) === true && data.length === 0
7878
const isEmptyObject = typeof data === 'object' && data !== null && Object.keys(data).length === 0
7979
const isNull = data === null
8080
if (isEmptyArray || isEmptyObject || isNull) {
81-
vuexFns.commit('setItem', getExpectedResponse(currentItemState))
81+
vuexFns.commit('set', getExpectedResponse(currentItemState))
8282
}
8383
}
8484

src/module/mutations/setAllMutation.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { hasOwn } from '../../shared/utils'
2+
import Vue from 'vue'
3+
14
/**
25
* Proxy for setting Resource Objects on a collection module
36
*
@@ -12,8 +15,13 @@ export function setAllMutation (resourceBuilder) {
1215
apply (target, thisArg, [state, payload]) {
1316
const settablePayload = resourceBuilder.build(payload)
1417

15-
state.items = { ...state.items, ...settablePayload }
16-
state.initial = { ...state.initial, ...settablePayload }
18+
if (hasOwn(settablePayload, 'id')) {
19+
Vue.set(state.items, settablePayload.id, settablePayload)
20+
Vue.set(state.initial, settablePayload.id, settablePayload)
21+
} else {
22+
state.items = { ...state.items, ...settablePayload }
23+
state.initial = { ...state.initial, ...settablePayload }
24+
}
1725
}
1826
})
1927
}

0 commit comments

Comments
 (0)