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';
|
2015-07-15 23:36:52 -07:00
|
|
|
import { contain } from 'thundercats-react';
|
2015-07-16 16:01:25 -07:00
|
|
|
import debugFactory from 'debug';
|
2015-07-15 23:36:52 -07:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Col,
|
2015-07-16 16:01:25 -07:00
|
|
|
Modal,
|
|
|
|
Panel,
|
|
|
|
Row
|
2015-07-15 23:36:52 -07:00
|
|
|
} 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',
|
2015-07-16 16:55:37 -07:00
|
|
|
map({ currentHike }) {
|
2015-07-15 10:54:03 -07:00
|
|
|
const { tests = [] } = currentHike;
|
2015-07-16 16:55:37 -07:00
|
|
|
|
|
|
|
return { currentHike, tests };
|
2015-07-15 10:54:03 -07:00
|
|
|
},
|
2015-07-16 16:55:37 -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,
|
2015-07-15 23:36:52 -07:00
|
|
|
dashedName: PropTypes.string,
|
2015-07-16 16:55:37 -07:00
|
|
|
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) {
|
2015-07-15 23:36:52 -07:00
|
|
|
if (e && e.preventDefault) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
if (answer === userAnswer) {
|
|
|
|
debug('correct answer!');
|
2015-07-16 16:01:25 -07:00
|
|
|
this.setState({ showInfo: true });
|
2015-07-15 23:36:52 -07:00
|
|
|
}
|
|
|
|
return debug('incorrect');
|
|
|
|
},
|
|
|
|
|
|
|
|
onCorrectAnswer() {
|
2015-07-16 16:01:25 -07:00
|
|
|
const { hikes, currentHike } = this.props;
|
2015-07-15 23:36:52 -07:00
|
|
|
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 23:36:52 -07:00
|
|
|
},
|
|
|
|
|
2015-07-15 10:54:03 -07:00
|
|
|
render() {
|
2015-07-16 16:01:25 -07:00
|
|
|
const { showInfo } = this.state;
|
2015-07-15 21:56:06 -07:00
|
|
|
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 (
|
2015-07-15 23:36:52 -07:00
|
|
|
<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) }
|
2015-07-15 23:36:52 -07:00
|
|
|
<Panel>
|
|
|
|
<Button
|
|
|
|
bsSize='large'
|
|
|
|
className='pull-left'
|
2015-07-16 16:01:25 -07:00
|
|
|
onClick={ this.onAnswer.bind(this, answer, false, info) }>
|
2015-07-15 23:36:52 -07:00
|
|
|
false
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
bsSize='large'
|
|
|
|
className='pull-right'
|
2015-07-16 16:01:25 -07:00
|
|
|
onClick={ this.onAnswer.bind(this, answer, true, info) }>
|
2015-07-15 23:36:52 -07:00
|
|
|
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
|
|
|
);
|