@@ -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,28 @@ function isIncrementHelper(payload) {
329
331
payload . isIncrementHelper === true ) ;
330
332
}
331
333
334
+ /**
335
+ * Creates the params needed to $set a target based on a nested.path
336
+ *
337
+ * @param {object } target
338
+ * @param {string } path
339
+ * @param {* } value
340
+ * @returns {[object, string, any] }
341
+ */
342
+ function getSetParams ( target , path , value ) {
343
+ var _a ;
344
+ var pathParts = path . split ( '.' ) ;
345
+ var prop = pathParts . pop ( ) ;
346
+ var pathParent = pathParts . join ( '.' ) ;
347
+ var targetForNestedProp = pathToProp ( target , pathParent ) ;
348
+ if ( targetForNestedProp === undefined ) {
349
+ // the target doesn't have an object ready at this level to set the value to
350
+ // so we need to step down a level and try again
351
+ return getSetParams ( target , pathParent , ( _a = { } , _a [ prop ] = value , _a ) ) ;
352
+ }
353
+ var valueToSet = value ;
354
+ return [ targetForNestedProp , prop , valueToSet ] ;
355
+ }
332
356
/**
333
357
* a function returning the mutations object
334
358
*
@@ -402,29 +426,38 @@ function pluginMutations (userState) {
402
426
}
403
427
} ,
404
428
PATCH_DOC : function ( state , patches ) {
405
- var _this = this ;
429
+ var _a ;
406
430
// Get the state prop ref
407
431
var ref = state . _conf . statePropName ? state [ state . _conf . statePropName ] : state ;
408
432
if ( state . _conf . firestoreRefType . toLowerCase ( ) === 'collection' ) {
409
433
ref = ref [ patches . id ] ;
410
434
}
411
435
if ( ! ref )
412
436
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!!
437
+ function convertHelpers ( originVal , newVal ) {
438
+ if ( isWhat . isArray ( originVal ) && isArrayHelper ( newVal ) ) {
439
+ newVal = newVal . executeOn ( originVal ) ;
424
440
}
425
- newVal = mergeAnything . merge ( { extensions : [ helpers ] } , ref [ key ] , patches [ key ] ) ;
426
- _this . _vm . $set ( ref , key , newVal ) ;
427
- } ) ;
441
+ if ( isWhat . isNumber ( originVal ) && isIncrementHelper ( newVal ) ) {
442
+ newVal = newVal . executeOn ( originVal ) ;
443
+ }
444
+ return newVal ; // always return newVal as fallback!!
445
+ }
446
+ // const refPropsPicked = filter(ref, Object.keys(patches))
447
+ // const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
448
+ var patchesFlat = flatten . flattenObject ( patches ) ;
449
+ for ( var _i = 0 , _b = Object . entries ( patchesFlat ) ; _i < _b . length ; _i ++ ) {
450
+ var _c = _b [ _i ] , path = _c [ 0 ] , value = _c [ 1 ] ;
451
+ var targetVal = pathToProp ( ref , path ) ;
452
+ var newVal = convertHelpers ( targetVal , value ) ;
453
+ // do not update anything if the values are the same
454
+ // this is technically not required, because vue takes care of this as well:
455
+ if ( targetVal === newVal )
456
+ continue ;
457
+ // update just the nested value
458
+ var setParams = getSetParams ( ref , path , newVal ) ;
459
+ ( _a = this . _vm ) . $set . apply ( _a , setParams ) ;
460
+ }
428
461
} ,
429
462
DELETE_DOC : function ( state , id ) {
430
463
if ( state . _conf . firestoreRefType . toLowerCase ( ) !== 'collection' )
@@ -1286,9 +1319,9 @@ function pluginActions (Firebase) {
1286
1319
var getters = _a . getters , commit = _a . commit ;
1287
1320
var defaultValues = getters . defaultValues ;
1288
1321
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
1322
+ var compareInfo = compareAnything . compareObjectProps ( flatten__default ( doc ) , // presentIn 0
1323
+ flatten__default ( defaultValues ) , // presentIn 1
1324
+ flatten__default ( searchTarget ) // presentIn 2
1292
1325
) ;
1293
1326
Object . keys ( compareInfo . presentIn ) . forEach ( function ( prop ) {
1294
1327
// don't worry about props not in fillables
@@ -1918,7 +1951,7 @@ function pluginGetters (Firebase) {
1918
1951
patchData . updated_by = state . _sync . userId ;
1919
1952
// clean up item
1920
1953
var cleanedPatchData = filter ( patchData , getters . fillables , getters . guard ) ;
1921
- var itemToUpdate = flatten ( cleanedPatchData ) ;
1954
+ var itemToUpdate = flatten__default ( cleanedPatchData ) ;
1922
1955
// add id (required to get ref later at apiHelpers.ts)
1923
1956
// @ts -ignore
1924
1957
itemToUpdate . id = id ;
0 commit comments