ModalValidatingInput.js 5.61 KB
import React, { Component } from 'react';

import greenCheck from '../../img/outlineGreenCheck.svg'
import alertError from '../../img/alert_error.svg'

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

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

	setFocus(val){
		this.setState({
			...this.state,
			focus:val
		})
	}

	handleChange(e){

		if((this.props.modal==='activationSaveModal' || this.props.modal==='furnitureModal' || this.props.modal==='trainerModal' || this.props.modal==='contactModal') && !this.props.modalPrivacy.visible){
			this.props.updateStoreValue('modalPrivacy',{
				value:false,
				visible:true
			})
		}

		if((this.props.modal==='activationSaveModal' || this.props.modal==='furnitureModal' || this.props.modal==='trainerModal' || this.props.modal==='contactModal') && !this.props.adAccepted.visible){
			this.props.updateStoreValue('adAccepted',{
				value:false,
				visible:true
			})
		}

		if((this.props.modal==='furnitureModal' || this.props.modal==='trainerModal' || this.props.modal==='contactModal') && (this.props.name==='contact' || this.props.name==='name')){
			
			var modal=''

			if(/^[A-Za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/.test(e.target.value)){
				modal='activationSaveModal'

				this.props.updateModalValues(modal,this.props.name,{
				...this.props.modalContacts[modal][this.props.name],
				value:e.target.value,
				valid:e.target.validity.valid&&e.target.value!==''})	
			}
			
			
			modal='trainerModal'

			this.props.updateModalValues(modal,this.props.name,{
			...this.props.modalContacts[modal][this.props.name],
			value:e.target.value,
			valid:e.target.validity.valid&&e.target.value!==''})	

			modal='furnitureModal'

			this.props.updateModalValues(modal,this.props.name,{
			...this.props.modalContacts[modal][this.props.name],
			value:e.target.value,
			valid:e.target.validity.valid&&e.target.value!==''})	

			modal='contactModal'

			this.props.updateModalValues(modal,this.props.name,{
			...this.props.modalContacts[modal][this.props.name],
			value:e.target.value,
			valid:e.target.validity.valid&&e.target.value!==''})	
				
		}else {
			this.props.updateModalValues(this.props.modal,this.props.name,{
			...this.props.modalContacts[this.props.modal][this.props.name],
			value:e.target.value,
			valid:e.target.validity.valid&&e.target.value!==''})	
		}

		if(this.props.modal==='activationSaveModal'){
			modal='trainerModal'

			this.props.updateModalValues(modal,this.props.name,{
			...this.props.modalContacts[modal][this.props.name],
			value:e.target.value,
			valid:e.target.validity.valid&&e.target.value!==''})	

			modal='furnitureModal'

			this.props.updateModalValues(modal,this.props.name,{
			...this.props.modalContacts[modal][this.props.name],
			value:e.target.value,
			valid:e.target.validity.valid&&e.target.value!==''})	

			modal='contactModal'

			this.props.updateModalValues(modal,this.props.name,{
			...this.props.modalContacts[modal][this.props.name],
			value:e.target.value,
			valid:e.target.validity.valid&&e.target.value!==''})	
		}

		

		
		
	}

	render(){

		const className='wrapper-input '+this.props.className
		const value=this.props.modalContacts[this.props.modal][this.props.name].value
		const valid=this.props.modalContacts[this.props.modal][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} 
		                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 {

    modalContacts: state.ProfilationSelectReducer.modalContacts,
     activationSteps: state.ProfilationSelectReducer.activationSteps,
     adAccepted: state.ProfilationSelectReducer.adAccepted,
     modalPrivacy: state.ProfilationSelectReducer.modalPrivacy,

  }
};

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

});

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