2015-07-09 00:29:29 -07:00
|
|
|
import React, { PropTypes } from 'react';
|
2015-07-20 16:52:48 -07:00
|
|
|
import { Spring } from 'react-motion';
|
2015-07-16 16:01:25 -07:00
|
|
|
import { Navigation, TransitionHook } from 'react-router';
|
|
|
|
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-22 13:52:18 -07:00
|
|
|
const ANSWER_THRESHOLD = 200;
|
2015-07-21 20:55:32 -07:00
|
|
|
|
2015-07-19 13:36:35 -07:00
|
|
|
export default React.createClass({
|
2015-07-17 22:33:46 -07:00
|
|
|
displayName: 'Question',
|
2015-07-20 16:52:48 -07:00
|
|
|
|
2015-07-19 13:36:35 -07:00
|
|
|
mixins: [
|
|
|
|
Navigation,
|
|
|
|
TransitionHook
|
|
|
|
],
|
2015-07-16 16:55:37 -07:00
|
|
|
|
2015-07-17 22:33:46 -07:00
|
|
|
propTypes: {
|
|
|
|
currentHike: PropTypes.object,
|
|
|
|
dashedName: PropTypes.string,
|
|
|
|
hikes: PropTypes.array,
|
|
|
|
params: PropTypes.object
|
2015-07-09 00:29:29 -07:00
|
|
|
},
|
2015-07-15 10:54:03 -07:00
|
|
|
|
2015-07-20 16:52:48 -07:00
|
|
|
getInitialState: () => ({
|
|
|
|
mouse: [0, 0],
|
2015-07-21 23:00:44 -07:00
|
|
|
correct: false,
|
2015-07-20 16:52:48 -07:00
|
|
|
delta: [0, 0],
|
|
|
|
isPressed: false,
|
|
|
|
showInfo: false,
|
|
|
|
shake: false
|
|
|
|
}),
|
|
|
|
|
|
|
|
getTweenValues() {
|
|
|
|
const { mouse: [x, y] } = this.state;
|
|
|
|
return {
|
|
|
|
val: { x, y },
|
2015-07-22 13:57:52 -07:00
|
|
|
config: [120, 10]
|
2015-07-20 16:52:48 -07:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-07-22 13:52:18 -07:00
|
|
|
handleMouseDown({ pageX, pageY, touches }) {
|
|
|
|
if (touches) {
|
|
|
|
({ pageX, pageY } = touches[0]);
|
|
|
|
}
|
2015-07-20 16:52:48 -07:00
|
|
|
const { mouse: [pressX, pressY] } = this.state;
|
|
|
|
const dx = pageX - pressX;
|
|
|
|
const dy = pageY - pressY;
|
|
|
|
this.setState({
|
|
|
|
isPressed: true,
|
|
|
|
delta: [dx, dy],
|
|
|
|
mouse: [pageX - dx, pageY - dy]
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleMouseUp() {
|
2015-07-21 23:00:44 -07:00
|
|
|
const { correct } = this.state;
|
|
|
|
if (correct) {
|
|
|
|
return this.setState({
|
|
|
|
isPressed: false,
|
|
|
|
delta: [0, 0]
|
|
|
|
});
|
|
|
|
}
|
2015-07-20 16:52:48 -07:00
|
|
|
this.setState({
|
|
|
|
isPressed: false,
|
|
|
|
mouse: [0, 0],
|
|
|
|
delta: [0, 0]
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-07-21 20:55:32 -07:00
|
|
|
handleMouseMove(answer) {
|
2015-07-22 13:52:18 -07:00
|
|
|
return (e) => {
|
|
|
|
let { pageX, pageY, touches } = e;
|
|
|
|
|
|
|
|
if (touches) {
|
|
|
|
e.preventDefault();
|
|
|
|
// these reassins the values of pageX, pageY from touches
|
|
|
|
({ pageX, pageY } = touches[0]);
|
|
|
|
}
|
|
|
|
|
2015-07-21 20:55:32 -07:00
|
|
|
const { isPressed, delta: [dx, dy] } = this.state;
|
|
|
|
if (isPressed) {
|
|
|
|
const mouse = [pageX - dx, pageY - dy];
|
|
|
|
if (mouse[0] >= ANSWER_THRESHOLD) {
|
|
|
|
this.handleMouseUp();
|
|
|
|
return this.onAnswer(answer, true)();
|
|
|
|
}
|
|
|
|
if (mouse[0] <= -ANSWER_THRESHOLD) {
|
|
|
|
this.handleMouseUp();
|
|
|
|
return this.onAnswer(answer, false)();
|
|
|
|
}
|
|
|
|
this.setState({ mouse });
|
|
|
|
}
|
|
|
|
};
|
2015-07-20 16:52:48 -07:00
|
|
|
},
|
|
|
|
|
2015-07-20 15:20:28 -07:00
|
|
|
hideInfo() {
|
|
|
|
this.setState({ showInfo: false });
|
|
|
|
},
|
|
|
|
|
2015-07-19 23:52:05 -07:00
|
|
|
onAnswer(answer, userAnswer) {
|
|
|
|
return (e) => {
|
|
|
|
if (e && e.preventDefault) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2015-07-21 23:00:44 -07:00
|
|
|
|
|
|
|
if (this.disposeTimeout) {
|
|
|
|
clearTimeout(this.disposeTimeout);
|
|
|
|
this.disposeTimeout = null;
|
|
|
|
}
|
|
|
|
|
2015-07-19 23:52:05 -07:00
|
|
|
if (answer === userAnswer) {
|
|
|
|
debug('correct answer!');
|
2015-07-21 23:00:44 -07:00
|
|
|
this.setState({
|
|
|
|
correct: true,
|
|
|
|
mouse: [ userAnswer ? 1000 : -1000, 0]
|
|
|
|
});
|
|
|
|
this.disposeTimeout = setTimeout(() => {
|
|
|
|
this.onCorrectAnswer();
|
|
|
|
}, 1000);
|
|
|
|
return;
|
2015-07-20 15:20:28 -07:00
|
|
|
}
|
2015-07-21 23:00:44 -07:00
|
|
|
|
2015-07-20 15:20:28 -07:00
|
|
|
debug('incorrect');
|
|
|
|
this.setState({
|
|
|
|
showInfo: true,
|
|
|
|
shake: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this.disposeTimeout = setTimeout(
|
|
|
|
() => this.setState({ shake: false }),
|
|
|
|
500
|
|
|
|
);
|
2015-07-19 23:52:05 -07:00
|
|
|
};
|
2015-07-17 22:33:46 -07:00
|
|
|
},
|
2015-07-15 10:54:03 -07:00
|
|
|
|
2015-07-17 22:33:46 -07:00
|
|
|
onCorrectAnswer() {
|
|
|
|
const { hikes, currentHike } = this.props;
|
|
|
|
const { dashedName, number } = this.props.params;
|
|
|
|
const { difficulty, tests } = currentHike;
|
|
|
|
const nextQuestionIndex = +number;
|
|
|
|
|
2015-07-20 15:20:28 -07:00
|
|
|
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) {
|
|
|
|
return this.transitionTo(`/hikes/${ nextHike.dashedName }`);
|
|
|
|
}
|
|
|
|
debug('next Hike was not found, currentHike %s', currentHike.dashedName);
|
|
|
|
this.transitionTo('/hikes');
|
2015-07-17 22:33:46 -07:00
|
|
|
},
|
2015-07-15 23:36:52 -07:00
|
|
|
|
2015-07-21 21:54:07 -07:00
|
|
|
routerWillLeave(nextState, router, cb) {
|
2015-07-17 22:33:46 -07:00
|
|
|
// TODO(berks): do animated transitions here stuff here
|
2015-07-21 21:54:07 -07:00
|
|
|
this.setState({
|
2015-07-21 23:00:44 -07:00
|
|
|
showInfo: false,
|
|
|
|
correct: false,
|
|
|
|
mouse: [0, 0]
|
2015-07-21 21:54:07 -07:00
|
|
|
}, cb);
|
2015-07-17 22:33:46 -07:00
|
|
|
},
|
2015-07-16 16:01:25 -07:00
|
|
|
|
2015-07-17 22:33:46 -07:00
|
|
|
renderInfo(showInfo, info) {
|
2015-07-20 15:20:28 -07:00
|
|
|
if (!info) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-07-17 22:33:46 -07:00
|
|
|
return (
|
|
|
|
<Modal
|
2015-07-20 15:20:28 -07:00
|
|
|
backdrop={ true }
|
|
|
|
onHide={ this.hideInfo }
|
2015-07-17 22:33:46 -07:00
|
|
|
show={ showInfo }>
|
|
|
|
<Modal.Body>
|
|
|
|
<h3>
|
2015-07-20 15:20:28 -07:00
|
|
|
{ info }
|
2015-07-17 22:33:46 -07:00
|
|
|
</h3>
|
|
|
|
</Modal.Body>
|
|
|
|
<Modal.Footer>
|
|
|
|
<Button
|
|
|
|
block={ true }
|
|
|
|
bsSize='large'
|
2015-07-20 15:20:28 -07:00
|
|
|
onClick={ this.hideInfo }>
|
|
|
|
hide
|
2015-07-17 22:33:46 -07:00
|
|
|
</Button>
|
|
|
|
</Modal.Footer>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
},
|
2015-07-16 16:01:25 -07:00
|
|
|
|
2015-07-22 14:18:37 -07:00
|
|
|
renderQuestion(number, question, answer, shake) {
|
2015-07-20 16:52:48 -07:00
|
|
|
return ({ val: { x } }) => {
|
|
|
|
const style = {
|
|
|
|
WebkitTransform: `translate3d(${ x }px, 0, 0)`,
|
|
|
|
transform: `translate3d(${ x }px, 0, 0)`
|
|
|
|
};
|
2015-07-22 14:18:37 -07:00
|
|
|
const title = <h4>Question { number }</h4>;
|
2015-07-20 16:52:48 -07:00
|
|
|
return (
|
|
|
|
<Panel
|
2015-07-21 20:55:32 -07:00
|
|
|
className={ shake ? 'animated swing shake' : '' }
|
2015-07-22 14:18:37 -07:00
|
|
|
header={ title }
|
2015-07-20 16:52:48 -07:00
|
|
|
onMouseDown={ this.handleMouseDown }
|
2015-07-21 20:12:13 -07:00
|
|
|
onMouseLeave={ this.handleMouseUp }
|
2015-07-21 20:55:32 -07:00
|
|
|
onMouseMove={ this.handleMouseMove(answer) }
|
2015-07-20 16:52:48 -07:00
|
|
|
onMouseUp={ this.handleMouseUp }
|
2015-07-22 13:52:18 -07:00
|
|
|
onTouchEnd={ this.handleMouseUp }
|
|
|
|
onTouchMove={ this.handleMouseMove(answer) }
|
|
|
|
onTouchStart={ this.handleMouseDown }
|
2015-07-20 16:52:48 -07:00
|
|
|
style={ style }>
|
|
|
|
<p>{ question }</p>
|
|
|
|
</Panel>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-07-17 22:33:46 -07:00
|
|
|
render() {
|
2015-07-20 15:20:28 -07:00
|
|
|
const { showInfo, shake } = this.state;
|
2015-07-17 22:33:46 -07:00
|
|
|
const { currentHike: { tests = [] } } = this.props;
|
|
|
|
const { number = '1' } = this.props.params;
|
2015-07-16 16:01:25 -07:00
|
|
|
|
2015-07-17 22:33:46 -07:00
|
|
|
const [question, answer, info] = tests[number - 1] || [];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col
|
2015-07-20 16:52:48 -07:00
|
|
|
onMouseUp={ this.handleMouseUp }
|
2015-07-17 22:33:46 -07:00
|
|
|
xs={ 8 }
|
|
|
|
xsOffset={ 2 }>
|
|
|
|
<Row>
|
2015-07-20 16:52:48 -07:00
|
|
|
<Spring endValue={ this.getTweenValues }>
|
2015-07-22 14:18:37 -07:00
|
|
|
{ this.renderQuestion(number, question, answer, shake) }
|
2015-07-20 16:52:48 -07:00
|
|
|
</Spring>
|
2015-07-17 22:33:46 -07:00
|
|
|
{ this.renderInfo(showInfo, info) }
|
|
|
|
<Panel>
|
2015-07-16 16:01:25 -07:00
|
|
|
<Button
|
|
|
|
bsSize='large'
|
2015-07-17 22:33:46 -07:00
|
|
|
className='pull-left'
|
2015-07-19 23:52:05 -07:00
|
|
|
onClick={ this.onAnswer(answer, false, info) }>
|
2015-07-17 22:33:46 -07:00
|
|
|
false
|
2015-07-16 16:01:25 -07:00
|
|
|
</Button>
|
2015-07-17 22:33:46 -07:00
|
|
|
<Button
|
|
|
|
bsSize='large'
|
|
|
|
className='pull-right'
|
2015-07-19 23:52:05 -07:00
|
|
|
onClick={ this.onAnswer(answer, true, info) }>
|
2015-07-17 22:33:46 -07:00
|
|
|
true
|
|
|
|
</Button>
|
|
|
|
</Panel>
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
);
|
|
|
|
}
|
2015-07-19 13:36:35 -07:00
|
|
|
});
|