@@ -16,6 +16,11 @@ import { globalModels as models } from './global-models'
16
16
import _omit from 'lodash/omit'
17
17
import _get from 'lodash/get'
18
18
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' >
19
24
20
25
export default function makeServiceMutations ( ) {
21
26
function addItems ( state , items ) {
@@ -374,24 +379,46 @@ export default function makeServiceMutations() {
374
379
Vue . set ( state . pagination , qid , newState )
375
380
} ,
376
381
377
- setPending ( state , method : string ) : void {
382
+ setPending ( state , method : PendingServiceMethodName ) : void {
378
383
const uppercaseMethod = method . charAt ( 0 ) . toUpperCase ( ) + method . slice ( 1 )
379
384
state [ `is${ uppercaseMethod } Pending` ] = true
380
385
} ,
381
- unsetPending ( state , method : string ) : void {
386
+ unsetPending ( state , method : PendingServiceMethodName ) : void {
382
387
const uppercaseMethod = method . charAt ( 0 ) . toUpperCase ( ) + method . slice ( 1 )
383
388
state [ `is${ uppercaseMethod } Pending` ] = false
384
389
} ,
385
390
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 {
387
414
const { method, error } = payload
388
415
const uppercaseMethod = method . charAt ( 0 ) . toUpperCase ( ) + method . slice ( 1 )
389
416
state [ `errorOn${ uppercaseMethod } ` ] = Object . assign (
390
417
{ } ,
391
418
serializeError ( error )
392
419
)
393
420
} ,
394
- clearError ( state , method : string ) : void {
421
+ clearError ( state , method : PendingServiceMethodName ) : void {
395
422
const uppercaseMethod = method . charAt ( 0 ) . toUpperCase ( ) + method . slice ( 1 )
396
423
state [ `errorOn${ uppercaseMethod } ` ] = null
397
424
}
0 commit comments