Add modal on correct answer

This commit is contained in:
Berkeley Martinez
2015-07-16 16:01:25 -07:00
parent 82ce7fc47e
commit 639f85c580
2 changed files with 71 additions and 11 deletions

View File

@ -1,14 +1,15 @@
import React, { PropTypes } from 'react'; import React, { PropTypes } from 'react';
import { Navigation } from 'react-router'; import { Navigation, TransitionHook } from 'react-router';
import stampit from 'react-stampit'; import stampit from 'react-stampit';
import { contain } from 'thundercats-react'; import { contain } from 'thundercats-react';
import debugFactory from 'debug';
import { import {
Button, Button,
Col, Col,
Row, Modal,
Panel Panel,
Row
} from 'react-bootstrap'; } from 'react-bootstrap';
import debugFactory from 'debug';
const debug = debugFactory('freecc:hikes'); const debug = debugFactory('freecc:hikes');
@ -41,6 +42,7 @@ export default contain(
} }
}, },
stampit(React, { stampit(React, {
state: { showInfo: false },
displayName: 'Question', displayName: 'Question',
propTypes: { propTypes: {
@ -50,24 +52,78 @@ export default contain(
tests: PropTypes.array tests: PropTypes.array
}, },
onAnswer(answer, userAnswer, e) { onAnswer(answer, userAnswer, info, e) {
if (e && e.preventDefault) { if (e && e.preventDefault) {
e.preventDefault(); e.preventDefault();
} }
if (answer === userAnswer) { if (answer === userAnswer) {
debug('correct answer!'); debug('correct answer!');
return this.onCorrectAnswer(); this.setState({ showInfo: true });
} }
return debug('incorrect'); return debug('incorrect');
}, },
onCorrectAnswer() { onCorrectAnswer() {
const { hikes, currentHike } = this.props;
const { dashedName, number } = this.props.params; const { dashedName, number } = this.props.params;
const nextQ = +number + 1; const { difficulty, tests } = currentHike;
this.transitionTo(`/hikes/${ dashedName }/questions/${ nextQ }`); 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>
);
}, },
render() { render() {
const { showInfo } = this.state;
const { tests } = this.props; const { tests } = this.props;
const { number = '1' } = this.props.params; const { number = '1' } = this.props.params;
@ -81,17 +137,18 @@ export default contain(
<Panel> <Panel>
<p>{ question }</p> <p>{ question }</p>
</Panel> </Panel>
{ this.renderInfo(showInfo, info) }
<Panel> <Panel>
<Button <Button
bsSize='large' bsSize='large'
className='pull-left' className='pull-left'
onClick={ this.onAnswer.bind(this, answer, false) }> onClick={ this.onAnswer.bind(this, answer, false, info) }>
false false
</Button> </Button>
<Button <Button
bsSize='large' bsSize='large'
className='pull-right' className='pull-right'
onClick={ this.onAnswer.bind(this, answer, true) }> onClick={ this.onAnswer.bind(this, answer, true, info) }>
true true
</Button> </Button>
</Panel> </Panel>
@ -99,5 +156,7 @@ export default contain(
</Col> </Col>
); );
} }
}).compose(Navigation) })
.compose(Navigation)
.compose(TransitionHook)
); );

View File

@ -43,6 +43,7 @@ var paths = {
'node_modules/', 'node_modules/',
'client/' 'client/'
], ],
publicJs: './public/js', publicJs: './public/js',
loopback: { loopback: {