2015-07-09 00:29:29 -07:00
|
|
|
import React, { PropTypes } from 'react';
|
2015-12-29 17:35:50 -08:00
|
|
|
import { spring, Motion } from 'react-motion';
|
2015-12-26 00:42:39 -08:00
|
|
|
import { contain } from 'thundercats-react';
|
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';
|
2015-07-23 15:17:24 -07:00
|
|
|
|
2015-07-22 13:52:18 -07:00
|
|
|
const ANSWER_THRESHOLD = 200;
|
2015-07-21 20:55:32 -07:00
|
|
|
|
2015-12-26 00:42:39 -08:00
|
|
|
export default contain(
|
|
|
|
{
|
|
|
|
store: 'appStore',
|
2015-12-29 17:35:50 -08:00
|
|
|
actions: ['hikesActions'],
|
2015-12-30 16:14:40 -08:00
|
|
|
map({ hikesApp, username }) {
|
2015-12-29 17:35:50 -08:00
|
|
|
const {
|
|
|
|
currentHike,
|
|
|
|
currentQuestion = 1,
|
|
|
|
mouse = [0, 0],
|
|
|
|
isCorrect = false,
|
|
|
|
delta = [0, 0],
|
|
|
|
isPressed = false,
|
|
|
|
showInfo = false,
|
|
|
|
shake = false
|
|
|
|
} = hikesApp;
|
2015-12-26 00:42:39 -08:00
|
|
|
return {
|
|
|
|
hike: currentHike,
|
2015-12-29 17:35:50 -08:00
|
|
|
currentQuestion,
|
|
|
|
mouse,
|
|
|
|
isCorrect,
|
|
|
|
delta,
|
|
|
|
isPressed,
|
|
|
|
showInfo,
|
2015-12-30 16:14:40 -08:00
|
|
|
shake,
|
|
|
|
username
|
2015-12-26 00:42:39 -08:00
|
|
|
};
|
2015-07-22 13:52:18 -07:00
|
|
|
}
|
2015-07-20 16:52:48 -07:00
|
|
|
},
|
2015-12-26 00:42:39 -08:00
|
|
|
React.createClass({
|
|
|
|
displayName: 'Questions',
|
2015-07-20 16:52:48 -07:00
|
|
|
|
2015-12-26 00:42:39 -08:00
|
|
|
propTypes: {
|
|
|
|
hike: PropTypes.object,
|
2015-12-29 17:35:50 -08:00
|
|
|
currentQuestion: PropTypes.number,
|
|
|
|
mouse: PropTypes.array,
|
|
|
|
isCorrect: PropTypes.bool,
|
|
|
|
delta: PropTypes.array,
|
|
|
|
isPressed: PropTypes.bool,
|
|
|
|
showInfo: PropTypes.bool,
|
|
|
|
shake: PropTypes.bool,
|
2015-12-30 16:14:40 -08:00
|
|
|
username: PropTypes.string,
|
2015-12-26 00:42:39 -08:00
|
|
|
hikesActions: PropTypes.object
|
|
|
|
},
|
|
|
|
|
|
|
|
handleMouseDown({ pageX, pageY, touches }) {
|
2015-07-22 13:52:18 -07:00
|
|
|
if (touches) {
|
|
|
|
({ pageX, pageY } = touches[0]);
|
|
|
|
}
|
2015-12-29 17:35:50 -08:00
|
|
|
const { mouse: [pressX, pressY], hikesActions } = this.props;
|
|
|
|
hikesActions.grabQuestion({ pressX, pressY, pageX, pageY });
|
2015-12-26 00:42:39 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
handleMouseUp() {
|
2015-12-29 17:35:50 -08:00
|
|
|
if (!this.props.isPressed) {
|
|
|
|
return null;
|
2015-12-26 00:42:39 -08:00
|
|
|
}
|
2015-12-29 17:35:50 -08:00
|
|
|
this.props.hikesActions.releaseQuestion();
|
2015-12-26 00:42:39 -08:00
|
|
|
},
|
2015-07-22 13:52:18 -07:00
|
|
|
|
2015-12-26 00:42:39 -08:00
|
|
|
handleMouseMove(answer) {
|
2015-12-29 17:35:50 -08:00
|
|
|
if (!this.props.isPressed) {
|
|
|
|
return () => {};
|
|
|
|
}
|
|
|
|
|
2015-12-26 00:42:39 -08:00
|
|
|
return (e) => {
|
|
|
|
let { pageX, pageY, touches } = e;
|
|
|
|
|
|
|
|
if (touches) {
|
|
|
|
e.preventDefault();
|
2015-12-29 17:35:50 -08:00
|
|
|
// these re-assigns the values of pageX, pageY from touches
|
2015-12-26 00:42:39 -08:00
|
|
|
({ pageX, pageY } = touches[0]);
|
2015-07-21 20:55:32 -07:00
|
|
|
}
|
2015-12-26 00:42:39 -08:00
|
|
|
|
2015-12-29 17:35:50 -08:00
|
|
|
const { delta: [dx, dy], hikesActions } = this.props;
|
|
|
|
const mouse = [pageX - dx, pageY - dy];
|
|
|
|
|
|
|
|
if (mouse[0] >= ANSWER_THRESHOLD) {
|
|
|
|
return this.onAnswer(answer, true)();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mouse[0] <= -ANSWER_THRESHOLD) {
|
|
|
|
return this.onAnswer(answer, false)();
|
2015-07-21 20:55:32 -07:00
|
|
|
}
|
2015-07-20 16:52:48 -07:00
|
|
|
|
2015-12-29 17:35:50 -08:00
|
|
|
return hikesActions.moveQuestion(mouse);
|
|
|
|
};
|
2015-12-26 00:42:39 -08:00
|
|
|
},
|
2015-07-20 15:20:28 -07:00
|
|
|
|
2015-12-26 00:42:39 -08:00
|
|
|
onAnswer(answer, userAnswer) {
|
2015-12-29 17:35:50 -08:00
|
|
|
const { hikesActions } = this.props;
|
2015-12-26 00:42:39 -08:00
|
|
|
return (e) => {
|
|
|
|
if (e && e.preventDefault) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2015-07-21 23:00:44 -07:00
|
|
|
|
2015-12-30 16:14:40 -08:00
|
|
|
return hikesActions.answer({
|
|
|
|
answer,
|
|
|
|
userAnswer,
|
|
|
|
props: this.props
|
|
|
|
});
|
2015-12-26 00:42:39 -08:00
|
|
|
};
|
|
|
|
},
|
2015-07-20 15:20:28 -07:00
|
|
|
|
2015-12-26 00:42:39 -08:00
|
|
|
routerWillLeave(nextState, router, cb) {
|
|
|
|
// TODO(berks): do animated transitions here stuff here
|
|
|
|
this.setState({
|
|
|
|
showInfo: false,
|
2015-12-29 17:35:50 -08:00
|
|
|
isCorrect: false,
|
2015-12-26 00:42:39 -08:00
|
|
|
mouse: [0, 0]
|
|
|
|
}, cb);
|
|
|
|
},
|
2015-07-22 23:10:57 -07:00
|
|
|
|
2015-12-29 17:35:50 -08:00
|
|
|
renderInfo(showInfo, info, hideInfo) {
|
2015-12-26 00:42:39 -08:00
|
|
|
if (!info) {
|
|
|
|
return null;
|
2015-07-22 23:10:57 -07:00
|
|
|
}
|
2015-12-26 00:42:39 -08:00
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
backdrop={ true }
|
2015-12-29 17:35:50 -08:00
|
|
|
onHide={ hideInfo }
|
2015-12-26 00:42:39 -08:00
|
|
|
show={ showInfo }>
|
|
|
|
<Modal.Body>
|
|
|
|
<h3>
|
|
|
|
{ info }
|
|
|
|
</h3>
|
|
|
|
</Modal.Body>
|
|
|
|
<Modal.Footer>
|
|
|
|
<Button
|
|
|
|
block={ true }
|
|
|
|
bsSize='large'
|
2015-12-29 17:35:50 -08:00
|
|
|
onClick={ hideInfo }>
|
2015-12-26 00:42:39 -08:00
|
|
|
hide
|
|
|
|
</Button>
|
|
|
|
</Modal.Footer>
|
|
|
|
</Modal>
|
2015-07-20 15:20:28 -07:00
|
|
|
);
|
2015-12-26 00:42:39 -08:00
|
|
|
},
|
2015-07-15 23:36:52 -07:00
|
|
|
|
2015-12-26 00:42:39 -08:00
|
|
|
renderQuestion(number, question, answer, shake) {
|
2015-12-29 17:35:50 -08:00
|
|
|
return ({ x }) => {
|
2015-12-26 00:42:39 -08:00
|
|
|
const style = {
|
|
|
|
WebkitTransform: `translate3d(${ x }px, 0, 0)`,
|
|
|
|
transform: `translate3d(${ x }px, 0, 0)`
|
|
|
|
};
|
|
|
|
const title = <h4>Question { number }</h4>;
|
|
|
|
return (
|
|
|
|
<Panel
|
|
|
|
className={ shake ? 'animated swing shake' : '' }
|
|
|
|
header={ title }
|
|
|
|
onMouseDown={ this.handleMouseDown }
|
|
|
|
onMouseLeave={ this.handleMouseUp }
|
|
|
|
onMouseMove={ this.handleMouseMove(answer) }
|
|
|
|
onMouseUp={ this.handleMouseUp }
|
|
|
|
onTouchEnd={ this.handleMouseUp }
|
|
|
|
onTouchMove={ this.handleMouseMove(answer) }
|
|
|
|
onTouchStart={ this.handleMouseDown }
|
|
|
|
style={ style }>
|
|
|
|
<p>{ question }</p>
|
|
|
|
</Panel>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
},
|
2015-07-16 16:01:25 -07:00
|
|
|
|
2015-12-26 00:42:39 -08:00
|
|
|
render() {
|
2015-12-29 17:35:50 -08:00
|
|
|
const { showInfo, shake } = this.props;
|
2015-12-26 00:42:39 -08:00
|
|
|
const {
|
|
|
|
hike: { tests = [] } = {},
|
2015-12-29 17:35:50 -08:00
|
|
|
mouse: [x],
|
|
|
|
currentQuestion,
|
|
|
|
hikesActions
|
2015-12-26 00:42:39 -08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const [ question, answer, info ] = tests[currentQuestion - 1] || [];
|
2015-07-16 16:01:25 -07:00
|
|
|
|
2015-07-20 16:52:48 -07:00
|
|
|
return (
|
2015-12-26 00:42:39 -08:00
|
|
|
<Col
|
2015-07-20 16:52:48 -07:00
|
|
|
onMouseUp={ this.handleMouseUp }
|
2015-12-26 00:42:39 -08:00
|
|
|
xs={ 8 }
|
|
|
|
xsOffset={ 2 }>
|
|
|
|
<Row>
|
2015-12-29 17:35:50 -08:00
|
|
|
<Motion style={{ x: spring(x, [120, 10]) }}>
|
2015-12-26 00:42:39 -08:00
|
|
|
{ this.renderQuestion(currentQuestion, question, answer, shake) }
|
|
|
|
</Motion>
|
2015-12-29 17:35:50 -08:00
|
|
|
{ this.renderInfo(showInfo, info, hikesActions.hideInfo) }
|
2015-12-26 00:42:39 -08:00
|
|
|
<Panel>
|
|
|
|
<Button
|
|
|
|
bsSize='large'
|
|
|
|
className='pull-left'
|
2015-12-29 17:35:50 -08:00
|
|
|
onClick={ this.onAnswer(answer, false) }>
|
2015-12-26 00:42:39 -08:00
|
|
|
false
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
bsSize='large'
|
|
|
|
className='pull-right'
|
2015-12-29 17:35:50 -08:00
|
|
|
onClick={ this.onAnswer(answer, true) }>
|
2015-12-26 00:42:39 -08:00
|
|
|
true
|
|
|
|
</Button>
|
|
|
|
</Panel>
|
|
|
|
</Row>
|
|
|
|
</Col>
|
2015-07-20 16:52:48 -07:00
|
|
|
);
|
2015-12-26 00:42:39 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|