Fixed an issue with clearing forms not correctly unsetting a value

This commit is contained in:
Thaum Rystra
2020-04-26 11:37:51 +02:00
parent 13cb9253c3
commit a2bae8d12a
6 changed files with 32 additions and 17 deletions

View File

@@ -1,7 +1,11 @@
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import SimpleSchema from 'simpl-schema'; import SimpleSchema from 'simpl-schema';
import ChildSchema, { RefSchema } from '/imports/api/parenting/ChildSchema.js'; import ChildSchema, { RefSchema } from '/imports/api/parenting/ChildSchema.js';
import { recomputeCreature } from '/imports/api/creature/computation/recomputeCreature.js'; import { recomputeCreature } from '/imports/api/creature/computation/recomputeCreature.js';
import LibraryNodes from '/imports/api/library/LibraryNodes.js'; import LibraryNodes from '/imports/api/library/LibraryNodes.js';
import Creatures from '/imports/api/creature/Creatures.js';
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js'; import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
import { softRemove } from '/imports/api/parenting/softRemove.js'; import { softRemove } from '/imports/api/parenting/softRemove.js';
import SoftRemovableSchema from '/imports/api/parenting/SoftRemovableSchema.js'; import SoftRemovableSchema from '/imports/api/parenting/SoftRemovableSchema.js';
@@ -150,7 +154,7 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({
const updateProperty = new ValidatedMethod({ const updateProperty = new ValidatedMethod({
name: 'CreatureProperties.methods.update', name: 'CreatureProperties.methods.update',
validate({_id, path, value}){ validate({_id, path}){
if (!_id) return false; if (!_id) return false;
// We cannot change these fields with a simple update // We cannot change these fields with a simple update
switch (path[0]){ switch (path[0]){
@@ -166,9 +170,15 @@ const updateProperty = new ValidatedMethod({
run({_id, path, value}) { run({_id, path, value}) {
let property = CreatureProperties.findOne(_id); let property = CreatureProperties.findOne(_id);
assertPropertyEditPermission(property, this.userId); assertPropertyEditPermission(property, this.userId);
CreatureProperties.update(_id, { let pathString = path.join('.');
$set: {[path.join('.')]: value}, let modifier;
}, { // unset empty values
if (value === null || value === undefined){
modifier = {$unset: {[pathString]: 1}};
} else {
modifier = {$set: {[pathString]: value}};
}
CreatureProperties.update(_id, modifier, {
selector: {type: property.type}, selector: {type: property.type},
}); });
recomputeCreatures(property); recomputeCreatures(property);

View File

@@ -1,3 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import SimpleSchema from 'simpl-schema'; import SimpleSchema from 'simpl-schema';
import ChildSchema from '/imports/api/parenting/ChildSchema.js'; import ChildSchema from '/imports/api/parenting/ChildSchema.js';
import propertySchemasIndex from '/imports/api/properties/propertySchemasIndex.js'; import propertySchemasIndex from '/imports/api/properties/propertySchemasIndex.js';
@@ -56,7 +59,7 @@ const insertNode = new ValidatedMethod({
const updateLibraryNode = new ValidatedMethod({ const updateLibraryNode = new ValidatedMethod({
name: 'LibraryNodes.methods.update', name: 'LibraryNodes.methods.update',
validate({_id, path, value, ack}){ validate({_id, path}){
if (!_id) return false; if (!_id) return false;
// We cannot change these fields with a simple update // We cannot change these fields with a simple update
switch (path[0]){ switch (path[0]){
@@ -70,9 +73,15 @@ const updateLibraryNode = new ValidatedMethod({
run({_id, path, value}) { run({_id, path, value}) {
let node = LibraryNodes.findOne(_id); let node = LibraryNodes.findOne(_id);
assertNodeEditPermission(node, this.userId); assertNodeEditPermission(node, this.userId);
return LibraryNodes.update(_id, { let pathString = path.join('.');
$set: {[path.join('.')]: value}, let modifier;
}, { // unset empty values
if (value === null || value === undefined){
modifier = {$unset: {[pathString]: 1}};
} else {
modifier = {$set: {[pathString]: value}};
}
return LibraryNodes.update(_id, modifier, {
selector: {type: node.type}, selector: {type: node.type},
}); });
}, },
@@ -127,5 +136,4 @@ export {
pullFromLibraryNode, pullFromLibraryNode,
pushToLibraryNode, pushToLibraryNode,
softRemoveLibraryNode, softRemoveLibraryNode,
libraryNodesToTree,
}; };

View File

@@ -109,7 +109,7 @@
:error-messages="errors.reset" :error-messages="errors.reset"
:menu-props="{auto: true, lazy: true}" :menu-props="{auto: true, lazy: true}"
:debounce-time="debounceTime" :debounce-time="debounceTime"
@change="(value, ack) => $emit('change', {path: ['reset'], value: value || '', ack})" @change="(value, ack) => $emit('change', {path: ['reset'], value, ack})"
/> />
</form-section> </form-section>
</form-sections> </form-sections>

View File

@@ -86,7 +86,7 @@
:error-messages="errors.reset" :error-messages="errors.reset"
:menu-props="{auto: true, lazy: true}" :menu-props="{auto: true, lazy: true}"
:debounce-time="debounceTime" :debounce-time="debounceTime"
@change="(value, ack) => $emit('change', {path: ['reset'], value: value || '', ack})" @change="(value, ack) => $emit('change', {path: ['reset'], value: value, ack})"
/> />
</div> </div>
</form-section> </form-section>

View File

@@ -61,10 +61,9 @@
<proficiency-select <proficiency-select
style="flex-basis: 300px;" style="flex-basis: 300px;"
label="Base Proficiency" label="Base Proficiency"
clearable
:value="model.baseProficiency" :value="model.baseProficiency"
:error-messages="errors.baseProficiency" :error-messages="errors.baseProficiency"
@change="(value, ack) => {$emit('change', {path: ['baseProficiency'], value: value || '', ack}); log({value, ack})}" @change="(value, ack) => {$emit('change', {path: ['baseProficiency'], value, ack})}"
/> />
</div> </div>
</form-section> </form-section>
@@ -120,9 +119,6 @@
}, },
] ]
};}, };},
methods: {
log: console.log,
},
}; };
</script> </script>

View File

@@ -1,6 +1,7 @@
<template lang="html"> <template lang="html">
<smart-select <smart-select
append-icon="arrow_drop_down" append-icon="arrow_drop_down"
clearable
class="ml-3" class="ml-3"
v-bind="$attrs" v-bind="$attrs"
:menu-props="{transition: 'slide-y-transition', lazy: true}" :menu-props="{transition: 'slide-y-transition', lazy: true}"
@@ -36,7 +37,7 @@
props: { props: {
value: { value: {
type: Number, type: Number,
default: 1, default: undefined,
}, },
}, },
data(){ return { data(){ return {