Skip to content

Commit 07b313b

Browse files
committed
WIP fix PATCH_DOC
1 parent ba227dc commit 07b313b

File tree

6 files changed

+149
-95
lines changed

6 files changed

+149
-95
lines changed

dist/index.cjs.js

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ var vuexEasyAccess = require('vuex-easy-access');
1111
var isWhat = require('is-what');
1212
var copy = _interopDefault(require('copy-anything'));
1313
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'));
1517
var compareAnything = require('compare-anything');
1618
var findAndReplaceAnything = require('find-and-replace-anything');
1719
var filter = _interopDefault(require('filter-anything'));
@@ -402,31 +404,40 @@ function pluginMutations (userState) {
402404
}
403405
},
404406
PATCH_DOC: function (state, patches) {
405-
var _this = this;
406407
// Get the state prop ref
407-
var ref = state._conf.statePropName
408-
? state[state._conf.statePropName]
409-
: state;
408+
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
410409
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
411410
ref = ref[patches.id];
412411
}
413412
if (!ref)
414413
return error('patch-no-ref');
415-
return Object.keys(patches).forEach(function (key) {
416-
var newVal = patches[key];
417-
// Merge if exists
418-
function helpers(originVal, newVal) {
419-
if (isWhat.isArray(originVal) && isArrayHelper(newVal)) {
420-
newVal = newVal.executeOn(originVal);
421-
}
422-
if (isWhat.isNumber(originVal) && isIncrementHelper(newVal)) {
423-
newVal = newVal.executeOn(originVal);
424-
}
425-
return newVal; // always return newVal as fallback!!
414+
function convertHelpers(originVal, newVal) {
415+
if (isWhat.isArray(originVal) && isArrayHelper(newVal)) {
416+
newVal = newVal.executeOn(originVal);
426417
}
427-
newVal = mergeAnything.merge({ extensions: [helpers] }, ref[key], patches[key]);
428-
_this._vm.$set(ref, key, newVal);
429-
});
418+
if (isWhat.isNumber(originVal) && isIncrementHelper(newVal)) {
419+
newVal = newVal.executeOn(originVal);
420+
}
421+
return newVal; // always return newVal as fallback!!
422+
}
423+
// const refPropsPicked = filter(ref, Object.keys(patches))
424+
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
425+
var patchesFlat = flatten.flattenObject(patches);
426+
for (var _i = 0, _a = Object.entries(patchesFlat); _i < _a.length; _i++) {
427+
var _b = _a[_i], path = _b[0], value = _b[1];
428+
var targetVal = pathToProp(ref, path);
429+
var newVal = convertHelpers(targetVal, value);
430+
// do not update anything if the values are the same
431+
// this is technically not required, because vue takes care of this as well:
432+
if (targetVal === newVal)
433+
return;
434+
// update just the nested value
435+
var pathParts = path.split('.');
436+
var prop = pathParts.pop();
437+
var pathParent = pathParts.join('');
438+
var targetForNestedProp = pathToProp(ref, pathParent);
439+
this._vm.$set(targetForNestedProp, prop, newVal);
440+
}
430441
},
431442
DELETE_DOC: function (state, id) {
432443
if (state._conf.firestoreRefType.toLowerCase() !== 'collection')
@@ -439,17 +450,15 @@ function pluginMutations (userState) {
439450
}
440451
},
441452
DELETE_PROP: function (state, path) {
442-
var searchTarget = state._conf.statePropName
443-
? state[state._conf.statePropName]
444-
: state;
453+
var searchTarget = state._conf.statePropName ? state[state._conf.statePropName] : state;
445454
var propArr = path.split('.');
446455
var target = propArr.pop();
447456
if (!propArr.length) {
448457
return this._vm.$delete(searchTarget, target);
449458
}
450459
var ref = vuexEasyAccess.getDeepRef(searchTarget, propArr.join('.'));
451460
return this._vm.$delete(ref, target);
452-
}
461+
},
453462
};
454463
}
455464

@@ -1290,9 +1299,9 @@ function pluginActions (Firebase) {
12901299
var getters = _a.getters, commit = _a.commit;
12911300
var defaultValues = getters.defaultValues;
12921301
var searchTarget = getters.collectionMode ? getters.storeRef[doc.id] : getters.storeRef;
1293-
var compareInfo = compareAnything.compareObjectProps(flatten(doc), // presentIn 0
1294-
flatten(defaultValues), // presentIn 1
1295-
flatten(searchTarget) // presentIn 2
1302+
var compareInfo = compareAnything.compareObjectProps(flatten__default(doc), // presentIn 0
1303+
flatten__default(defaultValues), // presentIn 1
1304+
flatten__default(searchTarget) // presentIn 2
12961305
);
12971306
Object.keys(compareInfo.presentIn).forEach(function (prop) {
12981307
// don't worry about props not in fillables
@@ -1902,7 +1911,7 @@ function pluginGetters (Firebase) {
19021911
patchData.updated_by = state._sync.userId;
19031912
// clean up item
19041913
var cleanedPatchData = filter(patchData, getters.fillables, getters.guard);
1905-
var itemToUpdate = flatten(cleanedPatchData);
1914+
var itemToUpdate = flatten__default(cleanedPatchData);
19061915
// add id (required to get ref later at apiHelpers.ts)
19071916
// @ts-ignore
19081917
itemToUpdate.id = id;

dist/index.esm.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { getDeepRef, getKeysFromPath } from 'vuex-easy-access';
55
import { isAnyObject, isPlainObject, isArray, isFunction, isNumber, isString, isDate } from 'is-what';
66
import copy from 'copy-anything';
77
import { merge } from 'merge-anything';
8-
import flatten from 'flatten-anything';
8+
import flatten, { flattenObject } from 'flatten-anything';
9+
import pathToProp from 'path-to-prop';
910
import { compareObjectProps } from 'compare-anything';
1011
import { findAndReplace, findAndReplaceIf } from 'find-and-replace-anything';
1112
import filter from 'filter-anything';
@@ -396,31 +397,40 @@ function pluginMutations (userState) {
396397
}
397398
},
398399
PATCH_DOC: function (state, patches) {
399-
var _this = this;
400400
// Get the state prop ref
401-
var ref = state._conf.statePropName
402-
? state[state._conf.statePropName]
403-
: state;
401+
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
404402
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
405403
ref = ref[patches.id];
406404
}
407405
if (!ref)
408406
return error('patch-no-ref');
409-
return Object.keys(patches).forEach(function (key) {
410-
var newVal = patches[key];
411-
// Merge if exists
412-
function helpers(originVal, newVal) {
413-
if (isArray(originVal) && isArrayHelper(newVal)) {
414-
newVal = newVal.executeOn(originVal);
415-
}
416-
if (isNumber(originVal) && isIncrementHelper(newVal)) {
417-
newVal = newVal.executeOn(originVal);
418-
}
419-
return newVal; // always return newVal as fallback!!
407+
function convertHelpers(originVal, newVal) {
408+
if (isArray(originVal) && isArrayHelper(newVal)) {
409+
newVal = newVal.executeOn(originVal);
420410
}
421-
newVal = merge({ extensions: [helpers] }, ref[key], patches[key]);
422-
_this._vm.$set(ref, key, newVal);
423-
});
411+
if (isNumber(originVal) && isIncrementHelper(newVal)) {
412+
newVal = newVal.executeOn(originVal);
413+
}
414+
return newVal; // always return newVal as fallback!!
415+
}
416+
// const refPropsPicked = filter(ref, Object.keys(patches))
417+
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
418+
var patchesFlat = flattenObject(patches);
419+
for (var _i = 0, _a = Object.entries(patchesFlat); _i < _a.length; _i++) {
420+
var _b = _a[_i], path = _b[0], value = _b[1];
421+
var targetVal = pathToProp(ref, path);
422+
var newVal = convertHelpers(targetVal, value);
423+
// do not update anything if the values are the same
424+
// this is technically not required, because vue takes care of this as well:
425+
if (targetVal === newVal)
426+
return;
427+
// update just the nested value
428+
var pathParts = path.split('.');
429+
var prop = pathParts.pop();
430+
var pathParent = pathParts.join('');
431+
var targetForNestedProp = pathToProp(ref, pathParent);
432+
this._vm.$set(targetForNestedProp, prop, newVal);
433+
}
424434
},
425435
DELETE_DOC: function (state, id) {
426436
if (state._conf.firestoreRefType.toLowerCase() !== 'collection')
@@ -433,17 +443,15 @@ function pluginMutations (userState) {
433443
}
434444
},
435445
DELETE_PROP: function (state, path) {
436-
var searchTarget = state._conf.statePropName
437-
? state[state._conf.statePropName]
438-
: state;
446+
var searchTarget = state._conf.statePropName ? state[state._conf.statePropName] : state;
439447
var propArr = path.split('.');
440448
var target = propArr.pop();
441449
if (!propArr.length) {
442450
return this._vm.$delete(searchTarget, target);
443451
}
444452
var ref = getDeepRef(searchTarget, propArr.join('.'));
445453
return this._vm.$delete(ref, target);
446-
}
454+
},
447455
};
448456
}
449457

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"flatten-anything": "^1.4.1",
4646
"is-what": "^3.8.0",
4747
"merge-anything": "^2.4.4",
48+
"path-to-prop": "0.0.3",
4849
"vuex-easy-access": "^3.1.8"
4950
},
5051
"devDependencies": {
@@ -71,8 +72,14 @@
7172
},
7273
"ava": {
7374
"compileEnhancements": false,
74-
"extensions": ["ts"],
75-
"require": ["ts-node/register"],
76-
"helpers": ["**/helpers/**/*"]
75+
"extensions": [
76+
"ts"
77+
],
78+
"require": [
79+
"ts-node/register"
80+
],
81+
"helpers": [
82+
"**/helpers/**/*"
83+
]
7784
}
7885
}

src/module/mutations.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { isArray, isFunction, isNumber } from 'is-what'
22
import { getDeepRef } from 'vuex-easy-access'
3+
import { flattenObject } from 'flatten-anything'
4+
import pathToProp from 'path-to-prop'
35
import logError from './errors'
46
import copy from 'copy-anything'
57
import { merge } from 'merge-anything'
@@ -79,21 +81,35 @@ export default function (userState: object): AnyObject {
7981
ref = ref[patches.id]
8082
}
8183
if (!ref) return logError('patch-no-ref')
82-
return Object.keys(patches).forEach(key => {
83-
let newVal = patches[key]
84-
// Merge if exists
85-
function helpers (originVal, newVal) {
86-
if (isArray(originVal) && isArrayHelper(newVal)) {
87-
newVal = newVal.executeOn(originVal)
88-
}
89-
if (isNumber(originVal) && isIncrementHelper(newVal)) {
90-
newVal = newVal.executeOn(originVal)
91-
}
92-
return newVal // always return newVal as fallback!!
84+
85+
function convertHelpers (originVal, newVal) {
86+
if (isArray(originVal) && isArrayHelper(newVal)) {
87+
newVal = newVal.executeOn(originVal)
9388
}
94-
newVal = merge({ extensions: [helpers] }, ref[key], patches[key])
95-
this._vm.$set(ref, key, newVal)
96-
})
89+
if (isNumber(originVal) && isIncrementHelper(newVal)) {
90+
newVal = newVal.executeOn(originVal)
91+
}
92+
return newVal // always return newVal as fallback!!
93+
}
94+
95+
// const refPropsPicked = filter(ref, Object.keys(patches))
96+
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
97+
98+
const patchesFlat = flattenObject(patches)
99+
100+
for (const [path, value] of Object.entries(patchesFlat)) {
101+
const targetVal = pathToProp(ref, path)
102+
const newVal = convertHelpers(targetVal, value)
103+
// do not update anything if the values are the same
104+
// this is technically not required, because vue takes care of this as well:
105+
if (targetVal === newVal) return
106+
// update just the nested value
107+
const pathParts = path.split('.')
108+
const prop = pathParts.pop()
109+
const pathParent = pathParts.join('')
110+
const targetForNestedProp = pathToProp(ref, pathParent)
111+
this._vm.$set(targetForNestedProp, prop, newVal)
112+
}
97113
},
98114
DELETE_DOC (state, id) {
99115
if (state._conf.firestoreRefType.toLowerCase() !== 'collection') return

0 commit comments

Comments
 (0)