All files / nexshop-web-contents/src/component/dialog/create-that create-that.js

100% Statements 26/26
100% Branches 9/9
100% Functions 9/9
100% Lines 26/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991x 1x 1x 1x       14x   14x         14x 14x 14x 14x 14x       1x 1x       3x       1x       1x 1x   1x       24x   24x       1x   1x         24x   24x             5x                                           1x                        
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);