Skip to content

Commit

Permalink
feat(core): work on loading texts
Browse files Browse the repository at this point in the history
  • Loading branch information
meriadec committed Oct 9, 2015
1 parent 68ed699 commit 3929c4d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions actions/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export function reset (context) {
context.dispatch('RESET_GAME');
}

export function destroyGame (context) {
context.dispatch('DESTROY_GAME');
}

export function tick (context) {
context.dispatch('GAME_TICK');
}
Expand Down
5 changes: 2 additions & 3 deletions components/game/SourceInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import { setFocus } from '../../actions/game';
export default class SourceInput extends React.Component {

componentDidMount () {
setTimeout(() => {
this.props.context.executeAction(setFocus, true);
});
const input = React.findDOMNode(this.refs.input);
input.focus();
}

componentDidUpdate () {
Expand Down
4 changes: 4 additions & 0 deletions components/game/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import GameStats from './GameStats';

// actions
import {
destroyGame,
tick,
beginTest,
updateWord,
Expand Down Expand Up @@ -66,6 +67,9 @@ class Game extends React.Component {
if (this._hasGameTick) {
clearInterval(this._gameTick);
}
setTimeout(() => {
this.props.context.executeAction(destroyGame);
});
}

updateStats () {
Expand Down
19 changes: 13 additions & 6 deletions components/pages/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
const gameStore = context.getStore(GameStore);
const routeStore = context.getStore(RouteStore);
return {
text: gameStore.getText(),
isFetching: gameStore.isFetching(),
textSource: gameStore.getTextSource(),
route: routeStore.getCurrentNavigate()
};
})
Expand All @@ -24,19 +25,23 @@ export default class GamePage extends React.Component {
constructor (props) {
super(props);

if (!props.text) {
const id = props.route.url.split('/')[2];
props.context.executeAction(loadText, id);
const id = props.route.url.split('/')[2];

if (!props.textSource || id !== String(props.textSource.id)) {
setTimeout(() => {
props.context.executeAction(loadText, id);
});
}
}

render () {

const { text } = this.props;
const hasText = !!text;
const { textSource } = this.props;
const hasText = !!textSource;

return (
<div>

{hasText && (
<Game
context={this.props.context} />
Expand All @@ -47,7 +52,9 @@ export default class GamePage extends React.Component {
<Loader />
</div>
)}

</div>
);
}
}

2 changes: 2 additions & 0 deletions components/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import React from 'react';
import { NavLink } from 'fluxible-router';
import { loadRandom } from '../../actions/game';

import { Loader } from '../ui';

export default class Home extends React.Component {

componentDidMount () {
Expand Down
14 changes: 14 additions & 0 deletions stores/GameStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class GameStore extends BaseStore {
init (text) {
_.assign(this, {

// check if a text is currently being fetched
_isFetching: false,

// source from server
_source: text || null,

Expand Down Expand Up @@ -68,12 +71,14 @@ class GameStore extends BaseStore {

getStats () { return this._stats; }
getText () { return this.text; }
getTextSource () { return this._source; }
getTextId () { return _.get(this._source, 'id'); }
getDuration () {
if (!this._startDate) { return 0; }
return moment().diff(this._startDate);
}
typedLetters () { return this._typedLetters; }
isFetching () { return this._isFetching; }
isPlaying () { return this._isPlaying; }
isReady () {
return this._isReady;
Expand Down Expand Up @@ -152,6 +157,9 @@ class GameStore extends BaseStore {

handleTextLoad () {
this._isReady = false;
this._isFetching = true;
this.text = null;
this._source = null;
this.emitChange();
}

Expand All @@ -165,11 +173,17 @@ class GameStore extends BaseStore {
this.emitChange();
}

handleDestroyGame () {
this.init();
this.emitChange();
}

}

GameStore.storeName = 'GameStore';

GameStore.handlers = {
DESTROY_GAME: 'handleDestroyGame',
TEXT_LOAD: 'handleTextLoad',
TEXT_LOADED: 'handleTextLoaded',
RANDOM_TEXT_LOADED: 'handleRandomTextLoaded',
Expand Down

0 comments on commit 3929c4d

Please sign in to comment.