Skip to content

Commit 9d97f2b

Browse files
committed
add mutations for manipulating isIdPending state
1 parent 4b37976 commit 9d97f2b

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import { globalModels as models } from './global-models'
1616
import _omit from 'lodash/omit'
1717
import _get from 'lodash/get'
1818
import _isObject from 'lodash/isObject'
19+
import { Id } from '@feathersjs/feathers'
20+
import { ServiceState } from '..'
21+
22+
type PendingServiceMethodName = 'find' | 'get' | 'create' | 'update' | 'patch' | 'remove'
23+
type PendingIdServiceMethodName = Exclude<PendingServiceMethodName, 'find' | 'get'>
1924

2025
export default function makeServiceMutations() {
2126
function addItems(state, items) {
@@ -374,24 +379,46 @@ export default function makeServiceMutations() {
374379
Vue.set(state.pagination, qid, newState)
375380
},
376381

377-
setPending(state, method: string): void {
382+
setPending(state, method: PendingServiceMethodName): void {
378383
const uppercaseMethod = method.charAt(0).toUpperCase() + method.slice(1)
379384
state[`is${uppercaseMethod}Pending`] = true
380385
},
381-
unsetPending(state, method: string): void {
386+
unsetPending(state, method: PendingServiceMethodName): void {
382387
const uppercaseMethod = method.charAt(0).toUpperCase() + method.slice(1)
383388
state[`is${uppercaseMethod}Pending`] = false
384389
},
385390

386-
setError(state, payload: { method: string; error: Error }): void {
391+
setIdPending(state, payload: { method: PendingIdServiceMethodName, id: Id | Id[] }): void {
392+
const { method, id } = payload
393+
const uppercaseMethod = method.charAt(0).toUpperCase() + method.slice(1)
394+
const isIdMethodPending = state[`isId${uppercaseMethod}Pending`] as ServiceState['isIdCreatePending']
395+
// if `id` is an array, ensure it doesn't have duplicates
396+
const ids = Array.isArray(id) ? [...new Set(id)] : [id]
397+
ids.forEach(id => isIdMethodPending.push(id))
398+
},
399+
unsetIdPending(state, payload: { method: PendingIdServiceMethodName, id: Id | Id[] }): void {
400+
const { method, id } = payload
401+
const uppercaseMethod = method.charAt(0).toUpperCase() + method.slice(1)
402+
const isIdMethodPending = state[`isId${uppercaseMethod}Pending`] as ServiceState['isIdCreatePending']
403+
// if `id` is an array, ensure it doesn't have duplicates
404+
const ids = Array.isArray(id) ? [...new Set(id)] : [id]
405+
ids.forEach(id => {
406+
const idx = isIdMethodPending.indexOf(id);
407+
if (idx >= 0) {
408+
Vue.delete(isIdMethodPending, idx);
409+
}
410+
})
411+
},
412+
413+
setError(state, payload: { method: PendingServiceMethodName; error: Error }): void {
387414
const { method, error } = payload
388415
const uppercaseMethod = method.charAt(0).toUpperCase() + method.slice(1)
389416
state[`errorOn${uppercaseMethod}`] = Object.assign(
390417
{},
391418
serializeError(error)
392419
)
393420
},
394-
clearError(state, method: string): void {
421+
clearError(state, method: PendingServiceMethodName): void {
395422
const uppercaseMethod = method.charAt(0).toUpperCase() + method.slice(1)
396423
state[`errorOn${uppercaseMethod}`] = null
397424
}

0 commit comments

Comments
 (0)