ActivationStep.js 5.24 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 Scroll from 'react-scroll'; // Imports all Mixins
import {scroller} from 'react-scroll'; //Imports scroller mixin, can use as scroller.scrollTo()

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()
    const paddingTop = window.getComputedStyle(document.getElementById('Activation-Step'), null).getPropertyValue('padding-top');

    var el = document.getElementById(elementId);
    if (el != null) {
     
      var top = el.getBoundingClientRect().top - bodyRect.top;
      
      //window.scrollTo(0, 0);
      this.setState({scrollTop: top}); 
      //this.setState({scrollTop: this.props.activationActiveStep*116}); 
      //this.setState({scrollTop: 0});    
      //window.scrollTo(0, top - parseInt(paddingTop, 10));
      //
    }

    setTimeout(() => {
      this.locked = false;
    }, 200)
     // console.log(this.state)
     //this.setState({scrollTop: (this.props.activationActiveStep*114)-96});
  }

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

    /*this.setState({activeStep: activeStep}, () => {
      
      
    });*/



    for (var i = this.props.activationSteps.length - 1; i >= 0; i--) {
      
      this.props.updateActivation(i,'isActive',i===activeStep)

    }

   // this.scrollTo(`activation-step${activeStep}`);
    
    
    
  }

  componentWillReceiveProps(nextProps){
     

      if(this.state.activeStep!==nextProps.activationActiveStep)
      {
        
        this.setState({
          ...this.state,
          activeStep:nextProps.activationActiveStep
        })
        //this.scrollTo('activation-step'+(this.props.activationActiveStep + 1))
      }
      
  }

  componentDidUpdate(prevProps){

    if(prevProps.activationActiveStep!==this.props.activationActiveStep){
      window.scrollTo(0,0)
      
     //Scroll.animateScroll.scrollMore(114*this.props.activationActiveStep)
     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>}
            
				      {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,
  }
};

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)),
});

/*Profilation.propTypes = {
  steps: PropTypes.array.isRequired,
  activeStep: PropTypes.number.isRequired,
  updatePropValue: PropTypes.func.isRequired
}*/

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