import React from 'react';
import {connect} from 'react-redux';
import UserStore from 'trc-client-core/src/user/UserStore';
import {vehiclesRequest} from 'trc-client-core/src/product/ProductActions';
import {gapReportRequest} from 'trc-client-core/src/report/ReportActions';

const PRODUCT_TRAINING_PLAN_ID = 'product_training_plan';

class ProductVehicleHandler extends React.Component {
    constructor(props) {
        super(props);
        this.displayName = 'ProductVehicleHandler';
    }
    componentWillMount() {
        var {dispatch} = this.props;
        if (!this.props.vehicle) {
            dispatch(vehiclesRequest());
        }    
        
        dispatch(gapReportRequest({
            pathwayId: PRODUCT_TRAINING_PLAN_ID,
            participantId: UserStore.get('participantId'),
            dealerCode: UserStore.get('currentBranchCode')
        }))
    }
    render() {
        var {params, vehicles} = this.props;
        if(!vehicles) {
            return null;
        }
        return React.cloneElement(this.props.children, {
            vehicles: (params.id) ? vehicles.get(params.id) : vehicles.toList(),
            learningPlan: this.props.learningPlan
        });
    }
}

export default connect(
    state => ({
        vehicles: state.product.get('vehicles'),
        learningPlan: state.gapReport.get(PRODUCT_TRAINING_PLAN_ID)
    })
)(ProductVehicleHandler);
