import React, { PropTypes, PureComponent } from 'react'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import { createSelector } from 'reselect'; import { Col, Row } from 'react-bootstrap'; import Choice from './Choice.jsx'; import { currentIndexSelector, selectedChoiceSelector, nextQuestion, selectChoice, correctSelector, incrementCorrect, resetQuiz, resetChoice } from './redux'; import { submitChallenge, challengeMetaSelector } from '../../redux'; import { challengeSelector } from '../../../../redux'; const mapStateToProps = createSelector( challengeSelector, challengeMetaSelector, currentIndexSelector, selectedChoiceSelector, correctSelector, ( { description = [], title }, meta, currentIndex, selectedChoice, correct ) => ({ title, description, meta, currentIndex, selectedChoice, correct }) ); function mapDispatchToProps(dispatch) { return () => bindActionCreators({ nextQuestion, selectChoice, incrementCorrect, resetQuiz, resetChoice, submitChallenge }, dispatch); } const propTypes = { correct: PropTypes.number, currentIndex: PropTypes.number, description: PropTypes.string, meta: PropTypes.object, nextQuestion: PropTypes.fun, resetChoice: PropTypes.fun, resetQuiz: PropTypes.fun, selectedChoice: PropTypes.number, submitChallenge: PropTypes.fun }; export class QuizChallenge extends PureComponent { constructor(props) { super(props); this.nextQuestion = this.nextQuestion.bind(this); this.submitChallenge = this.submitChallenge.bind(this); } nextQuestion() { this.props.resetChoice(); this.props.nextQuestion(); } submitChallenge() { this.props.resetQuiz(); this.props.submitChallenge(); } renderTitle() { return (

{this.props.meta.title}


); } renderResults() { const isQuizPassed = this.props.correct === this.props.description.length; return (
{this.renderTitle()}

Quiz Results:

You got {this.props.correct} out of {this.props.description.length} correct!

{isQuizPassed === false ? (

You will need to get all the questions correct in order to mark this quiz as completed.

) : ( )}
); } renderQuiz() { const currentIndex = this.props.currentIndex; const question = this.props.description[currentIndex]; return (
{this.renderTitle()}

Question {currentIndex + 1} of {this.props.description.length}:

{question.subtitle}:

Choices

{question.choices.map((choice, i) => ( ))}
{this.props.selectedChoice !== null &&
{this.props.selectedChoice === question.answer ?

Correct, great work!

:

Sorry, that is not correct!

}
{this.props.selectedChoice !== question.answer &&

Explanation:

}
}
); } render() { return (
{ this.props.currentIndex >= this.props.description.length ? this.renderResults() : this.renderQuiz()}
); } } QuizChallenge.displayName = 'QuizChallenge'; QuizChallenge.propTypes = propTypes; export default connect( mapStateToProps, mapDispatchToProps )(QuizChallenge);