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 { 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,
isGoingForward: currentStep > previousStep
})
);
const dispatchActions = {
goToStep
};
export class StepChallenge extends PureComponent {
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
};
renderActionButton(action) {
if (!action) {
return null;
}
return (
);
}
renderBackButton(index) {
const { goToStep } = this.props;
if (index === 0) {
return (
{ ' ' }
);
}
return (
);
}
renderNextButton(hasAction, index, numOfSteps, isCompleted) {
const { goToStep } = this.props;
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 { challenge, currentStep, isGoingForward } = this.props;
const numOfSteps = Array.isArray(challenge.description) ?
challenge.description.length :
0;
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);