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

104 lines
2.4 KiB
React
Raw Normal View History

2015-07-09 00:29:29 -07:00
import React, { PropTypes } from 'react';
import { Navigation } from 'react-router';
2015-07-09 00:29:29 -07:00
import stampit from 'react-stampit';
import { contain } from 'thundercats-react';
import {
Button,
Col,
Row,
Panel
} from 'react-bootstrap';
import debugFactory from 'debug';
const debug = debugFactory('freecc:hikes');
2015-07-09 00:29:29 -07:00
2015-07-15 10:54:03 -07:00
export default contain(
{
store: 'hikesStore',
map({ hikes, currentHike }) {
const { tests = [] } = currentHike;
return {
hikes,
currentHike,
tests
};
},
fetchAction: 'hikesActions.getHike',
getPayload({ currentHike, hikes, params: { dashedName } }) {
const filterRegex = new RegExp(dashedName, 'i');
if (currentHike && filterRegex.test(currentHike.dashedName)) {
return {
hikes: [],
isPrimed: true,
dashedName
};
}
return {
hikes,
isPrimed: false,
dashedName: dashedName
};
}
2015-07-09 00:29:29 -07:00
},
2015-07-15 10:54:03 -07:00
stampit(React, {
displayName: 'Question',
propTypes: {
params: PropTypes.object,
currentHike: PropTypes.object,
dashedName: PropTypes.string,
2015-07-15 10:54:03 -07:00
tests: PropTypes.array
},
onAnswer(answer, userAnswer, e) {
if (e && e.preventDefault) {
e.preventDefault();
}
if (answer === userAnswer) {
debug('correct answer!');
return this.onCorrectAnswer();
}
return debug('incorrect');
},
onCorrectAnswer() {
const { dashedName, number } = this.props.params;
const nextQ = +number + 1;
this.transitionTo(`/hikes/${ dashedName }/questions/${ nextQ }`);
},
2015-07-15 10:54:03 -07:00
render() {
const { tests } = this.props;
const { number = '1' } = this.props.params;
const [question, answer, info] = tests[number - 1] || [];
2015-07-09 00:29:29 -07:00
2015-07-15 10:54:03 -07:00
return (
<Col
xs={ 8 }
xsOffset={ 2 }>
2015-07-15 10:54:03 -07:00
<Row>
<Panel>
<p>{ question }</p>
</Panel>
<Panel>
<Button
bsSize='large'
className='pull-left'
onClick={ this.onAnswer.bind(this, answer, false) }>
false
</Button>
<Button
bsSize='large'
className='pull-right'
onClick={ this.onAnswer.bind(this, answer, true) }>
true
</Button>
</Panel>
2015-07-15 10:54:03 -07:00
</Row>
</Col>
);
}
}).compose(Navigation)
2015-07-15 10:54:03 -07:00
);