@@ -11,7 +11,9 @@ var vuexEasyAccess = require('vuex-easy-access');
11
11
var isWhat = require ( 'is-what' ) ;
12
12
var copy = _interopDefault ( require ( 'copy-anything' ) ) ;
13
13
var mergeAnything = require ( 'merge-anything' ) ;
14
- var flatten = _interopDefault ( require ( 'flatten-anything' ) ) ;
14
+ var flatten = require ( 'flatten-anything' ) ;
15
+ var flatten__default = _interopDefault ( flatten ) ;
16
+ var pathToProp = _interopDefault ( require ( 'path-to-prop' ) ) ;
15
17
var compareAnything = require ( 'compare-anything' ) ;
16
18
var findAndReplaceAnything = require ( 'find-and-replace-anything' ) ;
17
19
var filter = _interopDefault ( require ( 'filter-anything' ) ) ;
@@ -329,6 +331,37 @@ function isIncrementHelper(payload) {
329
331
payload . isIncrementHelper === true ) ;
330
332
}
331
333
334
+ function convertHelpers ( originVal , newVal ) {
335
+ if ( isWhat . isArray ( originVal ) && isArrayHelper ( newVal ) ) {
336
+ newVal = newVal . executeOn ( originVal ) ;
337
+ }
338
+ if ( isWhat . isNumber ( originVal ) && isIncrementHelper ( newVal ) ) {
339
+ newVal = newVal . executeOn ( originVal ) ;
340
+ }
341
+ return newVal ; // always return newVal as fallback!!
342
+ }
343
+ /**
344
+ * Creates the params needed to $set a target based on a nested.path
345
+ *
346
+ * @param {object } target
347
+ * @param {string } path
348
+ * @param {* } value
349
+ * @returns {[object, string, any] }
350
+ */
351
+ function getSetParams ( target , path , value ) {
352
+ var _a ;
353
+ var pathParts = path . split ( '.' ) ;
354
+ var prop = pathParts . pop ( ) ;
355
+ var pathParent = pathParts . join ( '.' ) ;
356
+ var targetForNestedProp = pathToProp ( target , pathParent ) ;
357
+ if ( targetForNestedProp === undefined ) {
358
+ // the target doesn't have an object ready at this level to set the value to
359
+ // so we need to step down a level and try again
360
+ return getSetParams ( target , pathParent , ( _a = { } , _a [ prop ] = value , _a ) ) ;
361
+ }
362
+ var valueToSet = value ;
363
+ return [ targetForNestedProp , prop , valueToSet ] ;
364
+ }
332
365
/**
333
366
* a function returning the mutations object
334
367
*
@@ -402,29 +435,27 @@ function pluginMutations (userState) {
402
435
}
403
436
} ,
404
437
PATCH_DOC : function ( state , patches ) {
405
- var _this = this ;
438
+ var _a ;
406
439
// Get the state prop ref
407
440
var ref = state . _conf . statePropName ? state [ state . _conf . statePropName ] : state ;
408
441
if ( state . _conf . firestoreRefType . toLowerCase ( ) === 'collection' ) {
409
442
ref = ref [ patches . id ] ;
410
443
}
411
444
if ( ! ref )
412
445
return error ( 'patch-no-ref' ) ;
413
- return Object . keys ( patches ) . forEach ( function ( key ) {
414
- var newVal = patches [ key ] ;
415
- // Merge if exists
416
- function helpers ( originVal , newVal ) {
417
- if ( isWhat . isArray ( originVal ) && isArrayHelper ( newVal ) ) {
418
- newVal = newVal . executeOn ( originVal ) ;
419
- }
420
- if ( isWhat . isNumber ( originVal ) && isIncrementHelper ( newVal ) ) {
421
- newVal = newVal . executeOn ( originVal ) ;
422
- }
423
- return newVal ; // always return newVal as fallback!!
424
- }
425
- newVal = mergeAnything . merge ( { extensions : [ helpers ] } , ref [ key ] , patches [ key ] ) ;
426
- _this . _vm . $set ( ref , key , newVal ) ;
427
- } ) ;
446
+ var patchesFlat = flatten . flattenObject ( patches ) ;
447
+ for ( var _i = 0 , _b = Object . entries ( patchesFlat ) ; _i < _b . length ; _i ++ ) {
448
+ var _c = _b [ _i ] , path = _c [ 0 ] , value = _c [ 1 ] ;
449
+ var targetVal = pathToProp ( ref , path ) ;
450
+ var newVal = convertHelpers ( targetVal , value ) ;
451
+ // do not update anything if the values are the same
452
+ // this is technically not required, because vue takes care of this as well:
453
+ if ( targetVal === newVal )
454
+ continue ;
455
+ // update just the nested value
456
+ var setParams = getSetParams ( ref , path , newVal ) ;
457
+ ( _a = this . _vm ) . $set . apply ( _a , setParams ) ;
458
+ }
428
459
} ,
429
460
DELETE_DOC : function ( state , id ) {
430
461
if ( state . _conf . firestoreRefType . toLowerCase ( ) !== 'collection' )
@@ -1286,9 +1317,9 @@ function pluginActions (Firebase) {
1286
1317
var getters = _a . getters , commit = _a . commit ;
1287
1318
var defaultValues = getters . defaultValues ;
1288
1319
var searchTarget = getters . collectionMode ? getters . storeRef [ doc . id ] : getters . storeRef ;
1289
- var compareInfo = compareAnything . compareObjectProps ( flatten ( doc ) , // presentIn 0
1290
- flatten ( defaultValues ) , // presentIn 1
1291
- flatten ( searchTarget ) // presentIn 2
1320
+ var compareInfo = compareAnything . compareObjectProps ( flatten__default ( doc ) , // presentIn 0
1321
+ flatten__default ( defaultValues ) , // presentIn 1
1322
+ flatten__default ( searchTarget ) // presentIn 2
1292
1323
) ;
1293
1324
Object . keys ( compareInfo . presentIn ) . forEach ( function ( prop ) {
1294
1325
// don't worry about props not in fillables
@@ -1918,7 +1949,7 @@ function pluginGetters (Firebase) {
1918
1949
patchData . updated_by = state . _sync . userId ;
1919
1950
// clean up item
1920
1951
var cleanedPatchData = filter ( patchData , getters . fillables , getters . guard ) ;
1921
- var itemToUpdate = flatten ( cleanedPatchData ) ;
1952
+ var itemToUpdate = flatten__default ( cleanedPatchData ) ;
1922
1953
// add id (required to get ref later at apiHelpers.ts)
1923
1954
// @ts -ignore
1924
1955
itemToUpdate . id = id ;
0 commit comments