import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {dialogEnhancer} from 'nexshop-web-dialog'
import ResolutionDropDown from "../../resolution-drop-down/resolution-drop-down";
class CreateThat extends Component {
constructor(props) {
super(props);
this.state = {
resolution: null,
title: '',
};
this.changeResolution = this.changeResolution.bind(this);
this.changeTitle = this.changeTitle.bind(this);
this.onCreated = this.onCreated.bind(this);
this.validateStates = this.validateStates.bind(this);
this.checkDupContentName = this.checkDupContentName.bind(this);
}
changeTitle(e) {
this.props.replaceContentNameValidity('NONE');
this.setState({title: e.target.value});
}
componentDidMount() {
this.titleInput.focus();
}
changeResolution(resolution) {
this.setState({resolution})
}
onCreated() {
const {title, resolution} = this.state;
const {onCreated} = this.props;
onCreated(title, resolution);
}
validateStates() {
const {title, resolution} = this.state;
return title !== '' && resolution !== null && this.props.contentNameExisted === 'NOT_EXIST';
}
checkDupContentName() {
const {fetchExistContentNameAsync, whatThatIs, breadcrumb} = this.props;
fetchExistContentNameAsync(breadcrumb[breadcrumb.length - 1].id, whatThatIs, this.titleInput.value);
}
render() {
const {whatThatIs, onCancel, contentNameExisted} = this.props;
return (
<div>
<div className="create-that-body">
<div className="create-that-body__title">{whatThatIs} Name</div>
<input type="text" className="create-that-body__input" maxLength={50}
onChange={this.changeTitle}
onBlur={this.checkDupContentName}
ref={input => this.titleInput = input}/>
<div className="create-that-body__error-message">
{
(this.titleInput && this.titleInput.value.length > 0) && contentNameExisted === 'EXIST' &&
'Above name already exists. Try again with a different name.'
}
</div>
<div className="create-that-body__resolution-drop-down">
<ResolutionDropDown onChange={this.changeResolution}/>
</div>
</div>
<div className="create-that-footer">
<div className={`create-that-footer__create${this.validateStates() ? '' : '--inactive'}`}
onClick={this.onCreated}>CREATE
</div>
<div className="create-that-footer__cancel" onClick={onCancel}>CANCEL</div>
</div>
</div>
);
}
}
CreateThat.propTypes = {
whatThatIs: PropTypes.string.isRequired,
visibility: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
onCancel: PropTypes.func,
onCreated: PropTypes.func,
contentNameExisted: PropTypes.string.isRequired,
fetchExistContentNameAsync: PropTypes.func.isRequired,
breadcrumb: PropTypes.array.isRequired,
replaceContentNameValidity: PropTypes.func.isRequired,
};
export default dialogEnhancer(CreateThat); |