@@ -11,7 +11,9 @@ var vuexEasyAccess = require('vuex-easy-access');
1111var isWhat = require ( 'is-what' ) ;
1212var copy = _interopDefault ( require ( 'copy-anything' ) ) ;
1313var 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' ) ) ;
1517var compareAnything = require ( 'compare-anything' ) ;
1618var findAndReplaceAnything = require ( 'find-and-replace-anything' ) ;
1719var filter = _interopDefault ( require ( 'filter-anything' ) ) ;
@@ -329,6 +331,28 @@ function isIncrementHelper(payload) {
329331 payload . isIncrementHelper === true ) ;
330332}
331333
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+ }
332356/**
333357 * a function returning the mutations object
334358 *
@@ -402,29 +426,38 @@ function pluginMutations (userState) {
402426 }
403427 } ,
404428 PATCH_DOC : function ( state , patches ) {
405- var _this = this ;
429+ var _a ;
406430 // Get the state prop ref
407431 var ref = state . _conf . statePropName ? state [ state . _conf . statePropName ] : state ;
408432 if ( state . _conf . firestoreRefType . toLowerCase ( ) === 'collection' ) {
409433 ref = ref [ patches . id ] ;
410434 }
411435 if ( ! ref )
412436 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 ) ;
424440 }
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+ }
428461 } ,
429462 DELETE_DOC : function ( state , id ) {
430463 if ( state . _conf . firestoreRefType . toLowerCase ( ) !== 'collection' )
@@ -1286,9 +1319,9 @@ function pluginActions (Firebase) {
12861319 var getters = _a . getters , commit = _a . commit ;
12871320 var defaultValues = getters . defaultValues ;
12881321 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
12921325 ) ;
12931326 Object . keys ( compareInfo . presentIn ) . forEach ( function ( prop ) {
12941327 // don't worry about props not in fillables
@@ -1918,7 +1951,7 @@ function pluginGetters (Firebase) {
19181951 patchData . updated_by = state . _sync . userId ;
19191952 // clean up item
19201953 var cleanedPatchData = filter ( patchData , getters . fillables , getters . guard ) ;
1921- var itemToUpdate = flatten ( cleanedPatchData ) ;
1954+ var itemToUpdate = flatten__default ( cleanedPatchData ) ;
19221955 // add id (required to get ref later at apiHelpers.ts)
19231956 // @ts -ignore
19241957 itemToUpdate . id = id ;
0 commit comments