import React, { PropTypes } from 'react'; import classnames from 'classnames'; import { connect } from 'react-redux'; import { createSelector } from 'reselect'; import PureComponent from 'react-pure-render/component'; import ReactTransitionReplace from 'react-css-transition-replace'; import { submitChallenge, goToStep } from '../../redux/actions'; import { challengeSelector } from '../../redux/selectors'; import { Button, Col, Image, Row } from 'react-bootstrap'; const transitionTimeout = 1000; const mapStateToProps = createSelector( challengeSelector, state => state.challengesApp.currentStep, state => state.challengesApp.previousStep, ({ challenge }, currentStep, previousStep) => ({ challenge, currentStep, previousStep, numOfSteps: Array.isArray(challenge.description) ? challenge.description.length : 0, isGoingForward: currentStep > previousStep }) ); const dispatchActions = { goToStep, submitChallenge }; export class StepChallenge extends PureComponent { constructor(...args) { super(...args); this.handleNextClick = this.handleNextClick.bind(this); this.handleBackClick = this.handleBackClick.bind(this); } static displayName = 'StepChallenge'; static defaultProps = { currentStep: 0, previousStep: -1 }; static propTypes = { challenge: PropTypes.object, currentStep: PropTypes.number, previousStep: PropTypes.number, isGoingForward: PropTypes.bool, goToStep: PropTypes.func, submitChallenge: PropTypes.func, numOfSteps: PropTypes.number }; handleNextClick() { const { numOfSteps, currentStep, submitChallenge, goToStep } = this.props; const isLastStep = currentStep + 1 >= numOfSteps; if (isLastStep) { return submitChallenge(); } return goToStep(currentStep + 1); } handleBackClick() { const { currentStep, goToStep } = this.props; goToStep(currentStep - 1); } renderActionButton(action) { if (!action) { return null; } return (
); } renderBackButton(index) { if (index === 0) { return ( { ' ' } ); } return ( ); } renderNextButton(hasAction, index, numOfSteps, isCompleted) { const isLastStep = index + 1 >= numOfSteps; const btnClass = classnames({ 'col-sm-4 col-xs-12': true, disabled: hasAction && !isCompleted }); return ( ); } renderStep(step, currentStep, numOfSteps) { if (!Array.isArray(step)) { return null; } const [imgUrl, imgAlt, info, action] = step; return (
{

{ this.renderActionButton(action) } { this.renderBackButton(currentStep) } ( { currentStep + 1 } / { numOfSteps }) { this.renderNextButton( !!action, currentStep, numOfSteps, true ) }
); } renderImages(steps) { // will preload all the image if (!Array.isArray(steps)) { return null; } return steps.map(([imgUrl, imgAlt]) => (
{
)); } render() { const { numOfSteps, challenge, currentStep, isGoingForward } = this.props; const step = challenge.description[currentStep]; const transitionName = 'challenge-step-' + (isGoingForward ? 'forward' : 'backward'); return ( { this.renderStep(step, currentStep, numOfSteps) }
{ this.renderImages(challenge.description) }
); } } export default connect(mapStateToProps, dispatchActions)(StepChallenge);