ActivationStep.js 4.88 KB
import React, {Component} from 'react';
import ActivationStepComponent from './ActivationStepComponent'
import StickyHeader from '../Solution/components/StickyHeader'
import Sticky from 'react-sticky-el';
import * as SelectActionCreators from '../actions/profilationSelect'
import {connect} from 'react-redux';

import {Motion, spring, presets} from 'react-motion';


class ActivationStep extends Component {
    constructor(props) {
        super(props)
        this.state = {
            scrollTop: 0,
            activeStep: this.props.activationActiveStep
        }

        this.editStep = this.editStep.bind(this)

    }

    scrollTo = (elementId) => {
        this.locked = true;

        const bodyRect = document.body.getBoundingClientRect()

        var el = document.getElementById(elementId);
        if (el !== null) {

            var top = el.getBoundingClientRect().top - bodyRect.top;

            this.setState({scrollTop: top});

        }

        setTimeout(() => {
            this.locked = false;
        }, 200)

    }

    editStep(activeStep) {
        this.props.updateStoreValue('progressBar', activeStep + 7)
        this.props.updateStoreValue('activationActiveStep', activeStep)

        for (var i = this.props.activationSteps.length - 1; i >= 0; i--) {

            this.props.updateActivation(i, 'isActive', i === activeStep)

        }
    }

    componentWillReceiveProps(nextProps) {


        if (this.state.activeStep !== nextProps.activationActiveStep) {

            this.setState({
                ...this.state,
                activeStep: nextProps.activationActiveStep
            })

        }

    }

    componentDidUpdate(prevProps) {

        if (prevProps.activationActiveStep !== this.props.activationActiveStep) {
            window.scrollTo(0, 0)

            if (this.props.activationActiveStep !== 0) {
                var factor = 114
                if (window.innerWidth < 768) {
                    factor = 84
                }


                setTimeout(function () {
                    this.setState(prev => {
                        return {
                            scrollTop: factor * this.props.activationActiveStep
                        }
                    })
                }.bind(this), 100)

            } else {
                this.setState(prev => {
                    return {
                        scrollTop: 0
                    }
                })
            }
        }
    }

    render() {


        const stepComponents = (this.props.activationSteps || []).map((step, index) => (<ActivationStepComponent
            key={index}
            index={index}
            step={step}
            isActive={step.isActive}
            editStep={this.editStep}
        />));

        return (<div id='Activation-Step' className='Activation-Step'>
                {window.innerWidth >= 768 &&
                <StickyHeader  />
                }

                {window.innerWidth < 768 &&
                <Sticky className='stickyMobile' mode='top' boundaryElement='.Activation-Step' topOffset={80}>
                    <StickyHeader  />
                </Sticky>}
                
                {this.props.activationTooltip&&<div className='transparentLayer'></div>}
                
                {stepComponents}

                <Motion
                    style={{
                        scrollTop: spring(this.state.scrollTop, presets.noWobble)
                    }}
                >
                    {currentStyles => {
                        return <WindowScrollSink scrollTop={currentStyles.scrollTop}/>

                    }}
                </Motion>

            </div>

        )
    }
}

class WindowScrollSink extends Component {
    componentDidUpdate(prevProps) {

        if (prevProps.scrollTop !== this.props.scrollTop) {
            return window.scrollTo(0, this.props.scrollTop);
        }
    }

    render() {
        return null
    }
}
const mapStateToProps = state => {
    return {
        steps: state.ProfilationSelectReducer.steps,
        activeStep: state.ProfilationSelectReducer.activeStep || 0,
        activationSteps: state.ProfilationSelectReducer.activationSteps,
        activationActiveStep: state.ProfilationSelectReducer.activationActiveStep,
         activationTooltip: state.ProfilationSelectReducer.activationTooltip,
    }
};

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)(ActivationStep);