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

158 lines
4.1 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';
2015-07-09 00:29:29 -07:00
import stampit from 'react-stampit';
import { contain } from 'thundercats-react';
2015-07-16 16:01:25 -07:00
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-15 10:54:03 -07:00
export default contain(
{
store: 'hikesStore',
map({ currentHike }) {
2015-07-15 10:54:03 -07:00
const { tests = [] } = currentHike;
return { currentHike, tests };
2015-07-15 10:54:03 -07:00
},
fetchAction: 'hikesActions.fetchCurrentHike',
getPayload({ currentHike, params: { dashedName } }) {
2015-07-15 10:54:03 -07:00
const filterRegex = new RegExp(dashedName, 'i');
if (currentHike && filterRegex.test(currentHike.dashedName)) {
return {
isPrimed: true,
dashedName
};
}
return {
isPrimed: false,
dashedName: dashedName
};
}
2015-07-09 00:29:29 -07:00
},
2015-07-15 10:54:03 -07:00
stampit(React, {
2015-07-16 16:01:25 -07:00
state: { showInfo: false },
2015-07-15 10:54:03 -07:00
displayName: 'Question',
propTypes: {
currentHike: PropTypes.object,
dashedName: PropTypes.string,
params: PropTypes.object,
2015-07-15 10:54:03 -07:00
tests: PropTypes.array
},
2015-07-16 16:01:25 -07:00
onAnswer(answer, userAnswer, info, e) {
if (e && e.preventDefault) {
e.preventDefault();
}
if (answer === userAnswer) {
debug('correct answer!');
2015-07-16 16:01:25 -07:00
this.setState({ showInfo: true });
}
return debug('incorrect');
},
onCorrectAnswer() {
2015-07-16 16:01:25 -07:00
const { hikes, currentHike } = this.props;
const { dashedName, number } = this.props.params;
2015-07-16 16:01:25 -07:00
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 exit
// find next hike
//
const nextHike = 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(`${ nextHike.dashedName }`);
}
debug('next Hike was not found');
});
},
routerWillLeave(/* nextState, router, cb[optional] */) {
// TODO(berks): do animated transitions here stuff here
},
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-15 10:54:03 -07:00
render() {
2015-07-16 16:01:25 -07:00
const { showInfo } = this.state;
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>
2015-07-16 16:01:25 -07:00
{ this.renderInfo(showInfo, info) }
<Panel>
<Button
bsSize='large'
className='pull-left'
2015-07-16 16:01:25 -07:00
onClick={ this.onAnswer.bind(this, answer, false, info) }>
false
</Button>
<Button
bsSize='large'
className='pull-right'
2015-07-16 16:01:25 -07:00
onClick={ this.onAnswer.bind(this, answer, true, info) }>
true
</Button>
</Panel>
2015-07-15 10:54:03 -07:00
</Row>
</Col>
);
}
2015-07-16 16:01:25 -07:00
})
.compose(Navigation)
.compose(TransitionHook)
2015-07-15 10:54:03 -07:00
);