ValidatingInput.js 6.65 KB
import React, {Component} from 'react';

import greenCheck from '../../../img/outlineGreenCheck.svg'
import alertError from '../../../img/alert_error.svg'
import  LemaApi  from '../../../service/service.js';

import * as SelectActionCreators from '../../actions/profilationSelect'
import {connect} from 'react-redux';

class ValidatingInput extends Component {
    constructor(props) {
        super(props)
        this.state = {
            focus: false,
            pasted:false
        }
        this.handlePaste = this.handlePaste.bind(this)
        this.handleChange = this.handleChange.bind(this)
        this.setFocus = this.setFocus.bind(this)
    }

    getIbanInfo = (iban) => {
        const lemaApi = new LemaApi();
        return lemaApi.checkIban(iban);    
    }

    setFocus(val) {
        this.setState({
            ...this.state,
            focus: val
        })
    }
    handlePaste(e){
        console.log('handler')
        this.setState({
            ...this.state,
            pasted:true
        })
        
    }

    handleChange(e) {
        
        var performChange = true;
        if (this.props.name === 'volumiAccise' && (e.target.value > 100 || e.target.value < 0)) {
            performChange = false
        }

        var patt = new RegExp(/[+0-9]+/);
        
        var res = patt.test(e.target.value);
        if (['referentPhone', 'sendAgainNumber', 'holderPhone', 'volumiAccise'].indexOf(this.props.name) > -1 &&
            !res && e.target.value !== '') {
            performChange = false
        }

        if (performChange) {

            if (this.props.name === 'referentPhone') {
                this.props.updateActivation(4, 'sendAgainNumber', {
                    ...this.props.activationSteps[4].sendAgainNumber,
                    value: e.target.value,
                    valid: e.target.validity.valid && e.target.value !== ''
                })
            }

            if(this.props.name==='pIva'){
                this.props.updateStoreValue('pIvaError',false)
            }

            if(this.props.name==='iban' && this.state.pasted){

                var v=e.target.value
                console.log(v)
                e.target.value=e.target.value.replace(/\s/g, "");
                
                if(e.target.validity.valid){                   
                    const lemaApi = new LemaApi();
                    lemaApi.checkIban(e.target.value)
                        .then(response => {
                            // iban OK                            
                            this.props.updateStoreValue('bankDescription', response.data.abi.banca)
                        })
                        .catch(response => {
                            // iban ko
                            this.props.updateStoreValue('bankDescription', '')
                        });
                    }
                }

                this.props.updateActivation(this.props.stepIndex, this.props.name, {
                    ...this.props.activationSteps[this.props.stepIndex][this.props.name],
                    value: e.target.value,
                    valid: e.target.validity.valid && v !== ''
                }) 
                this.setState({
                    ...this.state,
                    pasted:false
                })
            }else{

                this.props.updateActivation(this.props.stepIndex, this.props.name, {
                    ...this.props.activationSteps[this.props.stepIndex][this.props.name],
                value: e.target.value,
                valid: e.target.validity.valid && e.target.value !== ''
            })

            /*if (this.props.name==='iban'){
                console.log("is valid? " + e.target.validity.valid);

                if(e.target.validity.valid){                   
                    const lemaApi = new LemaApi();
                    lemaApi.checkIban(e.target.value)
                        .then(response => {
                            // iban OK                            
                            this.props.updateStoreValue('bankDescription', response.data.abi.banca)
                        })
                        .catch(response => {
                            // iban ko
                            this.props.updateStoreValue('bankDescription', '')
                        });
                    }
                }
            }*/
        }

    }

    render() {
        const className = 'wrapper-input ' + this.props.className
        const value = this.props.activationSteps[this.props.stepIndex][this.props.name].value
        const valid = this.props.activationSteps[this.props.stepIndex][this.props.name].valid
        return (<div className={className}>
            <label htmlFor="">{this.props.label}</label>
            {typeof this.props.subLabel !== 'undefined' &&
            <p className='wrapper-input_subLabel'>
                {this.props.subLabel}
            </p>

            }
            <div className="input_validation">
            
                <input
                    type={(typeof this.props.type === 'undefined') ? 'text' : this.props.type}
                    pattern={this.props.pattern}
                    onChange={this.handleChange}
                    onPaste={this.handlePaste}
                    className=''
                    name={this.props.name}
                    value={value}
                    placeholder={this.props.placeholder}
                    onFocus={() => this.setFocus(true)}
                    onBlur={() => this.setFocus(false)}
                />
            

            

                <div className="validationIcon">
                    {valid && value !== '' && <img src={greenCheck} alt=""/>}
                    {(!valid && value !== '' && !this.state.focus) &&
                    <img src={alertError} alt=""/>
                    }
                </div>

            </div>
        </div>)
    }
}

const mapStateToProps = state => {
    return {

        activationSteps: state.ProfilationSelectReducer.activationSteps,
        bankDescription: state.ProfilationSelectReducer.bankDescription,
    }
};

const mapDispatchToProps = dispatch => ({
    updateStoreValue: (prop, value) =>
        dispatch(SelectActionCreators.changeProp(prop, value)),
    updatePropValue: (steps, activeStep) => dispatch(SelectActionCreators.changeValue(steps, activeStep)),
    updateActivation: (index, prop, value) => dispatch(SelectActionCreators.updateActivation(index, prop, value)),

});


export default connect(mapStateToProps, mapDispatchToProps)(ValidatingInput);