ActivationStep.js 4.58 KB
import React, { Component } from 'react';
import ActivationStepComponent from './ActivationStepComponent'
import StickyHeader from '../Solution/components/StickyHeader'

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

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

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) {
      console.log('elTop',el.getBoundingClientRect().top,'bodyTop',window.scrollY)
      var top = el.getBoundingClientRect().top - bodyRect.top;
      console.log(top, paddingTop, top - parseInt(paddingTop, 10),this.state)
      //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('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){
      console.log('EDITnextProps',nextProps,this.state.activeStep)

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

	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'>
        <Sticky className='stickyMobile' mode='top' boundaryElement='.Activation-Step' topOffset={80}>
              <StickyHeader  />
            </Sticky>
				{stepComponents}
			

      <Motion
          style={{
            scrollTop: spring(this.state.scrollTop, {stiffness: 120, damping: 17})
          }}
        >
          {currentStyles => {
            return <WindowScrollSink scrollTop={currentStyles.scrollTop} />
              
          }}
        </Motion>

        </div>

      )
	}
}

class WindowScrollSink extends Component {
  componentDidUpdate (prevProps) {
    if (prevProps.scrollTop !== this.props.scrollTop) {
      //document.scrollingElement.scrollTop = this.props.scrollTop
      if (document.documentElement) {
        document.documentElement.scrollTop = this.props.scrollTop
        } else {
        document.body.scrollTop = 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);