Skip to content

Commit 5f7ac40

Browse files
committed
chore: change mutations for copy-handling
- removeItem removes copy - removeItems removes copy - clearAll also clears copiesById - make clearCopy-mutation cleaner
1 parent 10088ee commit 5f7ac40

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/service-module/service-module.mutations.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,17 @@ export default function makeServiceMutations() {
173173
const isIdOk = idToBeRemoved !== null && idToBeRemoved !== undefined
174174
const index = state.ids.findIndex(i => i === idToBeRemoved)
175175

176+
const Model = _get(models, `[${state.serverAlias}][${state.modelName}]`)
177+
const copiesById = state.keepCopiesInStore
178+
? state.copiesById
179+
: Model.copiesById
180+
176181
if (isIdOk && index !== null && index !== undefined) {
177182
Vue.delete(state.ids, index)
178183
Vue.delete(state.keyedById, idToBeRemoved)
184+
if (copiesById.hasOwnProperty(idToBeRemoved)) {
185+
Vue.delete(copiesById, idToBeRemoved)
186+
}
179187
}
180188
},
181189

@@ -211,8 +219,17 @@ export default function makeServiceMutations() {
211219
map[id] = true
212220
return map
213221
}, {})
222+
223+
const Model = _get(models, `[${state.serverAlias}][${state.modelName}]`)
224+
const copiesById = state.keepCopiesInStore
225+
? state.copiesById
226+
: Model.copiesById
227+
214228
idsToRemove.forEach(id => {
215229
Vue.delete(state.keyedById, id)
230+
if (copiesById.hasOwnProperty(id)) {
231+
Vue.delete(copiesById, id)
232+
}
216233
})
217234

218235
// Get indexes to remove from the ids array.
@@ -242,6 +259,18 @@ export default function makeServiceMutations() {
242259
clearAll(state) {
243260
state.ids = []
244261
state.keyedById = {}
262+
263+
if (state.keepCopiesInStore) {
264+
state.copiesById = {}
265+
} else {
266+
const Model = _get(
267+
models,
268+
`[${state.serverAlias}].byServicePath[${state.servicePath}]`
269+
)
270+
Object.keys(Model.copiesById).forEach(k =>
271+
Vue.delete(Model.copiesById, k)
272+
)
273+
}
245274
},
246275

247276
// Creates a copy of the record with the passed-in id, stores it in copiesById
@@ -322,15 +351,15 @@ export default function makeServiceMutations() {
322351
// Removes the copy from copiesById
323352
clearCopy(state, id) {
324353
const { keepCopiesInStore } = state
325-
if (keepCopiesInStore) {
326-
Vue.delete(state.copiesById, id)
327-
} else {
328-
const { serverAlias, servicePath } = state
329-
const Model = _get(
330-
models,
331-
`[${serverAlias}].byServicePath[${servicePath}]`
332-
)
333-
Vue.delete(Model.copiesById, id)
354+
const Model = _get(
355+
models,
356+
`[${state.serverAlias}].byServicePath[${state.servicePath}]`
357+
)
358+
359+
const copiesById = keepCopiesInStore ? state.copiesById : Model.copiesById
360+
361+
if (copiesById[id]) {
362+
Vue.delete(copiesById, id)
334363
}
335364
},
336365

0 commit comments

Comments
 (0)