From 208593d2036c7d21269348dc90e44384e10ed01a Mon Sep 17 00:00:00 2001 From: Charlie Humphreys Date: Mon, 5 Dec 2022 22:06:06 -0600 Subject: [PATCH 1/3] add callIfExists, which will call a method only if it exists --- .../editor/metadataEditor/metadataEditor.jsx | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/client/homebrew/editor/metadataEditor/metadataEditor.jsx b/client/homebrew/editor/metadataEditor/metadataEditor.jsx index b5f9ddc25..973ab3da0 100644 --- a/client/homebrew/editor/metadataEditor/metadataEditor.jsx +++ b/client/homebrew/editor/metadataEditor/metadataEditor.jsx @@ -9,7 +9,7 @@ const Nav = require('naturalcrit/nav/nav.jsx'); const StringArrayEditor = require('../stringArrayEditor/stringArrayEditor.jsx'); const Themes = require('themes/themes.json'); -const validations = require('./validations.js') +const validations = require('./validations.js'); const SYSTEMS = ['5e', '4e', '3.5e', 'Pathfinder']; @@ -53,7 +53,13 @@ const MetadataEditor = createClass({ }, handleFieldChange : function(name, e){ - e.persist(); + const callIfExists = (val, fn, ...args)=>{ + if(val[fn]) { + val[fn](...args); + } + }; + + callIfExists(e, 'persist'); // load validation rules, and check input value against them const inputRules = validations[name] ?? []; @@ -61,20 +67,19 @@ const MetadataEditor = createClass({ // if no validation rules, save to props if(validationErr.length === 0){ - e.target.setCustomValidity(''); + callIfExists(e.target, 'setCustomValidity', ''); this.props.onChange({ ...this.props.metadata, [name] : e.target.value }); } else { // if validation issues, display built-in browser error popup with each error. - console.log(validationErr); const errMessage = validationErr.map((err)=>{ return `- ${err}`; }).join('\n'); - e.target.setCustomValidity(errMessage); - e.target.reportValidity(); - }; + callIfExists(e.target, 'setCustomValidity', errMessage); + callIfExists(e.target, 'reportValidity'); + } }, handleSystem : function(system, e){ From 4e2f6b1d26052445010404da80cc72e7464ca68e Mon Sep 17 00:00:00 2001 From: Charlie Humphreys Date: Mon, 5 Dec 2022 22:39:38 -0600 Subject: [PATCH 2/3] move callIfExists to base file scope --- .../editor/metadataEditor/metadataEditor.jsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/client/homebrew/editor/metadataEditor/metadataEditor.jsx b/client/homebrew/editor/metadataEditor/metadataEditor.jsx index 973ab3da0..b9e702c83 100644 --- a/client/homebrew/editor/metadataEditor/metadataEditor.jsx +++ b/client/homebrew/editor/metadataEditor/metadataEditor.jsx @@ -15,6 +15,12 @@ const SYSTEMS = ['5e', '4e', '3.5e', 'Pathfinder']; const homebreweryThumbnail = require('../../thumbnail.png'); +const callIfExists = (val, fn, ...args)=>{ + if(val[fn]) { + val[fn](...args); + } +}; + const MetadataEditor = createClass({ displayName : 'MetadataEditor', getDefaultProps : function() { @@ -53,12 +59,6 @@ const MetadataEditor = createClass({ }, handleFieldChange : function(name, e){ - const callIfExists = (val, fn, ...args)=>{ - if(val[fn]) { - val[fn](...args); - } - }; - callIfExists(e, 'persist'); // load validation rules, and check input value against them From 8a110567fc13c503b920568fc6bc85d5c94484e7 Mon Sep 17 00:00:00 2001 From: Charlie Humphreys Date: Wed, 7 Dec 2022 07:32:22 -0600 Subject: [PATCH 3/3] remove call to persist --- client/homebrew/editor/metadataEditor/metadataEditor.jsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/client/homebrew/editor/metadataEditor/metadataEditor.jsx b/client/homebrew/editor/metadataEditor/metadataEditor.jsx index b9e702c83..ac644f1a1 100644 --- a/client/homebrew/editor/metadataEditor/metadataEditor.jsx +++ b/client/homebrew/editor/metadataEditor/metadataEditor.jsx @@ -59,8 +59,6 @@ const MetadataEditor = createClass({ }, handleFieldChange : function(name, e){ - callIfExists(e, 'persist'); - // load validation rules, and check input value against them const inputRules = validations[name] ?? []; const validationErr = inputRules.map((rule)=>rule(e.target.value)).filter(Boolean);