shake on wrong answer

This commit is contained in:
Berkeley Martinez
2015-07-20 15:20:28 -07:00
parent 2ceef5f0f6
commit 9a73d20d5a

View File

@ -12,7 +12,10 @@ import {
const debug = debugFactory('freecc:hikes'); const debug = debugFactory('freecc:hikes');
export default React.createClass({ export default React.createClass({
getInitialState: () => ({ showInfo: false }), getInitialState: () => ({
showInfo: false,
shake: false
}),
displayName: 'Question', displayName: 'Question',
mixins: [ mixins: [
Navigation, Navigation,
@ -26,6 +29,10 @@ export default React.createClass({
params: PropTypes.object params: PropTypes.object
}, },
hideInfo() {
this.setState({ showInfo: false });
},
onAnswer(answer, userAnswer) { onAnswer(answer, userAnswer) {
return (e) => { return (e) => {
if (e && e.preventDefault) { if (e && e.preventDefault) {
@ -33,9 +40,22 @@ export default React.createClass({
} }
if (answer === userAnswer) { if (answer === userAnswer) {
debug('correct answer!'); debug('correct answer!');
this.setState({ showInfo: true }); return this.onCorrectAnswer();
} }
return debug('incorrect'); debug('incorrect');
this.setState({
showInfo: true,
shake: true
});
if (this.disposeTimeout) {
clearTimeout(this.disposeTimeout);
this.disposeTimeout = null;
}
this.disposeTimeout = setTimeout(
() => this.setState({ shake: false }),
500
);
}; };
}, },
@ -44,31 +64,30 @@ export default React.createClass({
const { dashedName, number } = this.props.params; const { dashedName, number } = this.props.params;
const { difficulty, tests } = currentHike; const { difficulty, tests } = currentHike;
const nextQuestionIndex = +number; 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) { if (tests[nextQuestionIndex]) {
return this.transitionTo(`/hikes/${ nextHike.dashedName }`); return this.transitionTo(
} `/hikes/${ dashedName }/questions/${ nextQuestionIndex + 1 }`
debug('next Hike was not found, currentHike %s', currentHike.dashedName); );
this.transitionTo('/hikes'); }
}); // 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] */) { routerWillLeave(/* nextState, router, cb[optional] */) {
@ -76,22 +95,25 @@ export default React.createClass({
}, },
renderInfo(showInfo, info) { renderInfo(showInfo, info) {
if (!info) {
return null;
}
return ( return (
<Modal <Modal
backdrop={ false } backdrop={ true }
onHide={ this.onCorrectAnswer } onHide={ this.hideInfo }
show={ showInfo }> show={ showInfo }>
<Modal.Body> <Modal.Body>
<h3> <h3>
{ info || 'correct!' } { info }
</h3> </h3>
</Modal.Body> </Modal.Body>
<Modal.Footer> <Modal.Footer>
<Button <Button
block={ true } block={ true }
bsSize='large' bsSize='large'
onClick={ this.onCorrectAnswer }> onClick={ this.hideInfo }>
To next questions hide
</Button> </Button>
</Modal.Footer> </Modal.Footer>
</Modal> </Modal>
@ -99,7 +121,7 @@ export default React.createClass({
}, },
render() { render() {
const { showInfo } = this.state; const { showInfo, shake } = this.state;
const { currentHike: { tests = [] } } = this.props; const { currentHike: { tests = [] } } = this.props;
const { number = '1' } = this.props.params; const { number = '1' } = this.props.params;
@ -110,7 +132,7 @@ export default React.createClass({
xs={ 8 } xs={ 8 }
xsOffset={ 2 }> xsOffset={ 2 }>
<Row> <Row>
<Panel> <Panel className={ shake ? 'animated shake' : '' }>
<p>{ question }</p> <p>{ question }</p>
</Panel> </Panel>
{ this.renderInfo(showInfo, info) } { this.renderInfo(showInfo, info) }