Files
freeCodeCamp/common/app/routes/Hikes/components/Question.jsx

134 lines
3.4 KiB
React
Raw Normal View History

2015-07-09 00:29:29 -07:00
import React, { PropTypes } from 'react';
2015-07-16 16:01:25 -07:00
import { Navigation, TransitionHook } from 'react-router';
import debugFactory from 'debug';
import {
Button,
Col,
2015-07-16 16:01:25 -07:00
Modal,
Panel,
Row
} from 'react-bootstrap';
const debug = debugFactory('freecc:hikes');
2015-07-09 00:29:29 -07:00
2015-07-19 13:36:35 -07:00
export default React.createClass({
getInitialState: () => ({ showInfo: false }),
displayName: 'Question',
2015-07-19 13:36:35 -07:00
mixins: [
Navigation,
TransitionHook
],
propTypes: {
currentHike: PropTypes.object,
dashedName: PropTypes.string,
hikes: PropTypes.array,
params: PropTypes.object
2015-07-09 00:29:29 -07:00
},
2015-07-15 10:54:03 -07:00
onAnswer(answer, userAnswer, info, e) {
if (e && e.preventDefault) {
e.preventDefault();
}
if (answer === userAnswer) {
debug('correct answer!');
this.setState({ showInfo: true });
}
return debug('incorrect');
},
2015-07-15 10:54:03 -07:00
onCorrectAnswer() {
const { hikes, currentHike } = this.props;
const { dashedName, number } = this.props.params;
const { difficulty, tests } = currentHike;
const nextQuestionIndex = +number;
this.setState({ showInfo: false }, () => {
if (tests[nextQuestionIndex]) {
return this.transitionTo(
`/hikes/${ dashedName }/questions/${ nextQuestionIndex + 1 }`
);
}
// next questions does not exist;
debug('finding next hike');
const nextHike = [].slice.call(hikes)
// hikes is in oder of difficulty, lets get reverse order
.reverse()
// now lets find the hike with the difficulty right above this one
.reduce((lowerHike, hike) => {
if (hike.difficulty > difficulty) {
return hike;
}
return lowerHike;
}, null);
if (nextHike) {
return this.transitionTo(`/hikes/${ nextHike.dashedName }`);
}
debug('next Hike was not found, currentHike %s', currentHike.dashedName);
this.transitionTo('/hikes');
});
},
routerWillLeave(/* nextState, router, cb[optional] */) {
// TODO(berks): do animated transitions here stuff here
},
2015-07-16 16:01:25 -07:00
renderInfo(showInfo, info) {
return (
<Modal
backdrop={ false }
onHide={ ::this.onCorrectAnswer }
show={ showInfo }>
<Modal.Body>
<h3>
{ info || 'correct!' }
</h3>
</Modal.Body>
<Modal.Footer>
<Button
block={ true }
bsSize='large'
onClick={ ::this.onCorrectAnswer }>
To next questions
</Button>
</Modal.Footer>
</Modal>
);
},
2015-07-16 16:01:25 -07:00
render() {
const { showInfo } = this.state;
const { currentHike: { tests = [] } } = this.props;
const { number = '1' } = this.props.params;
2015-07-16 16:01:25 -07:00
const [question, answer, info] = tests[number - 1] || [];
return (
<Col
xs={ 8 }
xsOffset={ 2 }>
<Row>
<Panel>
<p>{ question }</p>
</Panel>
{ this.renderInfo(showInfo, info) }
<Panel>
2015-07-16 16:01:25 -07:00
<Button
bsSize='large'
className='pull-left'
onClick={ this.onAnswer.bind(this, answer, false, info) }>
false
2015-07-16 16:01:25 -07:00
</Button>
<Button
bsSize='large'
className='pull-right'
onClick={ this.onAnswer.bind(this, answer, true, info) }>
true
</Button>
</Panel>
</Row>
</Col>
);
}
2015-07-19 13:36:35 -07:00
});