0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-14 15:03:07 +00:00

move validations into it's own file.

This commit is contained in:
Gazook89
2022-11-04 15:32:20 -05:00
parent 7696be5d95
commit 589ec0251a
2 changed files with 33 additions and 17 deletions

View File

@@ -9,21 +9,13 @@ const Nav = require('naturalcrit/nav/nav.jsx');
const StringArrayEditor = require('../stringArrayEditor/stringArrayEditor.jsx'); const StringArrayEditor = require('../stringArrayEditor/stringArrayEditor.jsx');
const Themes = require('themes/themes.json'); const Themes = require('themes/themes.json');
const validations = require('./validations.js')
const SYSTEMS = ['5e', '4e', '3.5e', 'Pathfinder']; const SYSTEMS = ['5e', '4e', '3.5e', 'Pathfinder'];
const homebreweryThumbnail = require('../../thumbnail.png'); const homebreweryThumbnail = require('../../thumbnail.png');
const validators = {
thumbnail : [
(value)=>{
return value?.length > 5 ? 'Max URL length of 5 characters' : null;
},
(value)=>{
return (value ?? '')[0] !== 'W' ? 'URL must start with W' : null;
}
]
};
const MetadataEditor = createClass({ const MetadataEditor = createClass({
displayName : 'MetadataEditor', displayName : 'MetadataEditor',
@@ -33,6 +25,7 @@ const MetadataEditor = createClass({
editId : null, editId : null,
title : '', title : '',
description : '', description : '',
thumbnail : '',
tags : [], tags : [],
published : false, published : false,
authors : [], authors : [],
@@ -46,8 +39,8 @@ const MetadataEditor = createClass({
getInitialState : function(){ getInitialState : function(){
return { return {
showThumbnail : true, showThumbnail : true,
validationError : null errs : null
}; };
}, },
@@ -63,20 +56,20 @@ const MetadataEditor = createClass({
}, },
handleFieldChange : function(name, e){ handleFieldChange : function(name, e){
const inputRules = validators[name] ?? []; const inputRules = validations[name] ?? [];
const validationErr = inputRules.map((rule)=>rule(e.target.value)).filter(Boolean); const validationErr = inputRules.map((rule)=>rule(e.target.value)).filter(Boolean);
this.setState((prevState)=>({ this.setState((prevState)=>({
validationError : { errs : {
...(prevState.validationError ?? {}), ...(prevState.errs ?? {}),
[name] : validationErr.length > 0 ? validationErr : undefined [name] : validationErr.length > 0 ? validationErr : undefined
} }
})); }));
if(Object.values(this.state.validationError ?? {}).filter(Boolean).length === 0){ if(Object.values(this.state.errs ?? {}).filter(Boolean).length === 0){
this.props.onChange({ this.props.onChange({
...this.props.metadata, ...this.props.metadata,
[name] : e.target.value [name] : e.target.value
}); });
} };
}, },

View File

@@ -0,0 +1,23 @@
module.exports = {
title : [
(value)=>{
return value?.length > 10 ? 'Max URL length of 10 characters' : null;
}
],
description : [
(value)=>{
return value?.length > 10 ? 'Max URL length of 10 characters' : null;
}
],
thumbnail : [
(value)=>{
return value?.length > 5 ? 'Max URL length of 5 characters' : null;
},
(value)=>{
return (value ?? '')[0] !== 'W' ? 'URL must start with W' : null;
}
]
};