mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-29 04:42:41 +00:00
move validations into it's own file.
This commit is contained in:
@@ -9,21 +9,13 @@ 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 SYSTEMS = ['5e', '4e', '3.5e', 'Pathfinder'];
|
||||
|
||||
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({
|
||||
displayName : 'MetadataEditor',
|
||||
@@ -33,6 +25,7 @@ const MetadataEditor = createClass({
|
||||
editId : null,
|
||||
title : '',
|
||||
description : '',
|
||||
thumbnail : '',
|
||||
tags : [],
|
||||
published : false,
|
||||
authors : [],
|
||||
@@ -46,8 +39,8 @@ const MetadataEditor = createClass({
|
||||
|
||||
getInitialState : function(){
|
||||
return {
|
||||
showThumbnail : true,
|
||||
validationError : null
|
||||
showThumbnail : true,
|
||||
errs : null
|
||||
};
|
||||
},
|
||||
|
||||
@@ -63,20 +56,20 @@ const MetadataEditor = createClass({
|
||||
},
|
||||
|
||||
handleFieldChange : function(name, e){
|
||||
const inputRules = validators[name] ?? [];
|
||||
const inputRules = validations[name] ?? [];
|
||||
const validationErr = inputRules.map((rule)=>rule(e.target.value)).filter(Boolean);
|
||||
this.setState((prevState)=>({
|
||||
validationError : {
|
||||
...(prevState.validationError ?? {}),
|
||||
errs : {
|
||||
...(prevState.errs ?? {}),
|
||||
[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.metadata,
|
||||
[name] : e.target.value
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
},
|
||||
|
||||
|
||||
23
client/homebrew/editor/metadataEditor/validations.js
Normal file
23
client/homebrew/editor/metadataEditor/validations.js
Normal 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;
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user