ServerDataTransfer.js 2.24 KB
import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as SelectActionCreators from './actions/profilationSelect'
import LemaApi from '../service/service.js'


class ServerDataTransfer extends Component {
    constructor(props) {
        super(props)
        this.state = {        
            change: false
        }

        // necessario per legare il this al componente nel metodo onClick 
        this.onClick = this.onClick.bind(this);
    }

    onClick() {
        let request = {
            Job: this.props.steps[0].job,
            Service: this.props.steps[0].service,
            Expend: this.props.steps[1].expend,
            Period: this.props.steps[1].period,
            Light: this.props.steps[1].light,
            Gas: this.props.steps[1].gas,
            Environment: this.props.steps[2].environment,
            Flexibility: this.props.steps[3].flexibility,
            Customizable: this.props.steps[4].customizable

            // add more properties...
        };
        
        const lemaApi = new LemaApi();
        lemaApi.saveProfilationData(request);

        this.setState(this.state = {change: !this.change})   
    }

    render() {

        console.info("********* STATE IN COMPONENT *********");
        console.info(this.props.steps);

        {/** this works! **/}
        return (
            <div>
                <ul>
                    <li><b>JOB:</b> {this.props.steps[0].job} </li>
                    <li><b>SERVICE:</b> {this.props.steps[0].service} </li>                    
                </ul>
                <div>
                    <input type="button" value="Invia Dati" onClick={this.onClick} />
                </div>
            </div>
        )        
    }
}


const mapStateToProps = state => {
    return {
        activationSteps: state.ProfilationSelectReducer.activationSteps,
        steps: state.ProfilationSelectReducer.steps
    }
};

const mapDispatchToProps = dispatch => ({
    updatePropValue: (steps, activeStep) => dispatch(SelectActionCreators.changeValue(steps, activeStep)),
    updateActivation: (index, prop, value) => dispatch(SelectActionCreators.updateActivation(index, prop, value))
});
 
export default connect(mapStateToProps, mapDispatchToProps)(ServerDataTransfer);