Commit e221d8c8 authored by Roman's avatar Roman

BRCD-1676 feat(SettingsPlay) - add error support and prevent from submit

parent bb05aaf0
......@@ -6,7 +6,7 @@ import Field from '../../Field';
const PlayForm = ({
item,
isNameAlreadyExists,
isNameHasError,
isAllowedDisableAction,
isAllowedEditName,
isAllowedEditDefault,
......@@ -17,17 +17,17 @@ const PlayForm = ({
}) => (
<Form horizontal>
{isAllowedEditName && (
<FormGroup validationState={isNameAlreadyExists ? 'error' : null} >
<FormGroup validationState={isNameHasError ? 'error' : null} >
<Col componentClass={ControlLabel} sm={3}>
Name
Name <span className="danger-red"> *</span>
</Col>
<Col sm={7}>
<Field
onChange={onChangeName}
value={item.get('name', '')}
/>
{ isNameAlreadyExists && (
<HelpBlock>Name already exists</HelpBlock>
{ isNameHasError && (
<HelpBlock>{isNameHasError}</HelpBlock>
)}
</Col>
</FormGroup>
......@@ -71,7 +71,10 @@ const PlayForm = ({
PlayForm.propTypes = {
item: PropTypes.instanceOf(Immutable.Map),
isNameAlreadyExists: PropTypes.bool,
isNameHasError: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
]),
isAllowedDisableAction: PropTypes.bool,
isAllowedEditName: PropTypes.bool,
isAllowedEditDefault: PropTypes.bool,
......@@ -83,7 +86,7 @@ PlayForm.propTypes = {
PlayForm.defaultProps = {
item: Immutable.Map(),
isNameAlreadyExists: true,
isNameHasError: false,
isAllowedDisableAction: true,
isAllowedEditName: true,
isAllowedEditDefault: true,
......
import { connect } from 'react-redux';
import { titleCase } from 'change-case';
import PlayForm from './PlayForm';
const mapStateToProps = (state, props) => ({
isNameAlreadyExists: (props.existingNames) && props.existingNames.includes(props.item.get('name')),
isAllowedDisableAction: !props.item.get('default', false),
isAllowedEditName: props.mode === 'create',
isAllowedEditDefault: props.mode === 'create',
isNameHasError: (props.errors) && props.errors.get('name', false),
});
const mapDispatchToProps = (dispatch, { item, mode, updateField, ...otherProps }) => ({ // eslint-disable-line no-unused-vars
const mapDispatchToProps = (dispatch, props) => ({
onChangeName: (e) => {
const { item, updateField, setError, existingNames, errors } = props;
const { value } = e.target;
const cleanValue = value.toUpperCase().replace(globalSetting.keyUppercaseCleanRegex, '');
updateField(['name'], cleanValue);
if (existingNames.includes(cleanValue)) {
setError('name', 'Name already exists');
} else if (errors.get('name', false)) {
setError('name');
}
if (item.get('label', '') === '' || item.get('label', '').toUpperCase() === item.get('name', '').toUpperCase()) {
updateField('label', titleCase(cleanValue));
}
},
onChangeLabel: (e) => {
const { updateField } = props;
const { value } = e.target;
updateField('label', value);
},
onChangeDefault: (e) => {
const { updateField } = props;
const { value } = e.target;
updateField('default', value);
if (value) {
......@@ -32,6 +43,7 @@ const mapDispatchToProps = (dispatch, { item, mode, updateField, ...otherProps }
},
onChangeEnabled: (e) => {
const { updateField } = props;
const { value } = e.target;
updateField('enabled', value);
},
......
......@@ -2,9 +2,10 @@ import { connect } from 'react-redux';
import Immutable from 'immutable';
import Plays from './Plays';
import PlayForm from './PlayFormContainer';
import { showFormModal } from '../../../actions/guiStateActions/pageActions';
import { showFormModal, setFormModalError } from '../../../actions/guiStateActions/pageActions';
import { saveSettings, getSettings } from '../../../actions/settingsActions';
const mapStateToProps = null; // eslint-disable-line no-unused-vars
const mapDispatchToProps = (dispatch, props) => ({
......@@ -18,6 +19,10 @@ const mapDispatchToProps = (dispatch, props) => ({
default: data.isEmpty(),
});
const onOk = (newItem) => {
if (newItem.get('name', '') === '') {
dispatch(setFormModalError('name', 'Name is required'));
return false;
}
if (newItem.get('default', false)) {
data.forEach((p, index) => {
props.onChange('plays', [index, 'default'], false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment