From 688c10bdec57d58a46cbef70457fadce89982c48 Mon Sep 17 00:00:00 2001 From: meriadec Date: Fri, 9 Oct 2015 10:41:52 +0200 Subject: [PATCH] refactor(random): better use of executeAction --- actions/game.js | 10 ++++++--- components/Header.js | 11 ++++++--- components/game/index.js | 2 ++ components/pages/Home.js | 17 +++++++++----- components/pages/Random.js | 46 -------------------------------------- routes.js | 7 ------ stores/GameStore.js | 26 +++++++++++---------- 7 files changed, 43 insertions(+), 76 deletions(-) delete mode 100644 components/pages/Random.js diff --git a/actions/game.js b/actions/game.js index 996feea..1b6b412 100644 --- a/actions/game.js +++ b/actions/game.js @@ -2,6 +2,7 @@ import moment from 'moment'; import superagent from 'superagent'; +import { navigateAction } from 'fluxible-router'; export function updateWord (context, payload) { context.dispatch('UPDATE_WORD', payload); @@ -23,19 +24,22 @@ export function reset (context) { context.dispatch('RESET_GAME'); } +export function destroyGame (context) { + context.dispatch('DESTROY_GAME'); +} + export function tick (context) { context.dispatch('GAME_TICK'); } -export function loadRandom (context, done) { - - context.dispatch('RANDOM_TEXT_LOAD'); +export function loadRandom (context, payload, done) { superagent.get(`${context.api._getUrl()}/texts/rand`) .accept('json') .end((err, res) => { if (err) { throw err; } context.dispatch('RANDOM_TEXT_LOADED', res.body); + context.executeAction(navigateAction, { url: `/game/${res.body.id}` }); done(); }); diff --git a/components/Header.js b/components/Header.js index d42e97d..feba81c 100644 --- a/components/Header.js +++ b/components/Header.js @@ -6,6 +6,7 @@ import { NavLink } from 'fluxible-router'; import connectToStores from 'fluxible-addons-react/connectToStores'; import AuthStore from '../stores/AuthStore'; import { logout } from '../actions/auth'; +import { loadRandom } from '../actions/game'; import { GithubButton } from './ui'; import ProfileBox from './ProfileBox'; @@ -16,6 +17,10 @@ class Header extends React.Component { super(props); } + randomGame () { + this.props.context.executeAction(loadRandom); + } + logout () { this.props.context.executeAction(logout); } @@ -37,15 +42,15 @@ class Header extends React.Component { src='assets/images/logo-1.svg' /> - {'Random game'} - + @@ -17,13 +22,15 @@ export default class Home extends React.Component {
- - +

@@ -37,15 +44,15 @@ export default class Home extends React.Component {

- {'Play a random game'} - +

{'or '} diff --git a/components/pages/Random.js b/components/pages/Random.js deleted file mode 100644 index 3786afe..0000000 --- a/components/pages/Random.js +++ /dev/null @@ -1,46 +0,0 @@ -'use strict'; - -import React from 'react'; -import connectToStores from 'fluxible-addons-react/connectToStores'; - -import { navigateAction } from 'fluxible-router'; -import { Loader } from '../ui'; -import { loadRandom } from '../../actions/game'; -import { GameStore } from '../../stores'; - -class RandomPage extends React.Component { - - componentDidMount () { - setTimeout(() => { - this.props.context.executeAction(loadRandom); - }); - } - - componentDidUpdate () { - if (this.props.isReady) { - this.props.context.executeAction( - navigateAction, - { - url: `/game/${this.props.textId}` - } - ); - } - } - - render () { - return ( -

- -
- ); - } - -} - -export default connectToStores(RandomPage, [GameStore], ctx => { - const gameStore = ctx.getStore(GameStore); - return { - isReady: gameStore.isReady(), - textId: gameStore.getTextId() - }; -}); diff --git a/routes.js b/routes.js index 2dc6745..5d1530b 100644 --- a/routes.js +++ b/routes.js @@ -15,13 +15,6 @@ export default { title: 'Profile', handler: require('./components/pages/Profile') }, - random: { - path: '/random', - method: 'get', - page: 'random', - title: 'Random game', - handler: require('./components/pages/Random') - }, languages: { path: '/languages', method: 'get', diff --git a/stores/GameStore.js b/stores/GameStore.js index 0a65ce2..d538edd 100644 --- a/stores/GameStore.js +++ b/stores/GameStore.js @@ -26,15 +26,12 @@ class GameStore extends BaseStore { } init (text) { - if (!text) { - throw new Error('No text'); - } _.assign(this, { // source from server - _source: text, + _source: text || null, - _isReady: true, + _isReady: !!text, // stats on the current game _stats: { @@ -52,7 +49,7 @@ class GameStore extends BaseStore { _typedWord: '', // the text used - text: Parser.parseText(text.data), + text: text ? Parser.parseText(text.data) : null, // used to see if a test is in progress _isPlaying: false, @@ -61,7 +58,7 @@ class GameStore extends BaseStore { _isFinished: false, // input is focused ? - _isFocused: false, + _isFocused: !!text, // begin/end timestamps _startDate: null @@ -144,17 +141,16 @@ class GameStore extends BaseStore { handleReset () { this.init(this._source); - this._isFocused = true; this.emitChange(); } - handleGameTick () { - this.calcStats(); + handleDestroyGame () { + this.init(); this.emitChange(); } - handleRandomTextLoaded (text) { - this.init(text); + handleGameTick () { + this.calcStats(); this.emitChange(); } @@ -163,11 +159,17 @@ class GameStore extends BaseStore { this.emitChange(); } + handleRandomTextLoaded (text) { + this.init(text); + this.emitChange(); + } + } GameStore.storeName = 'GameStore'; GameStore.handlers = { + DESTROY_GAME: 'handleDestroyGame', RANDOM_TEXT_LOAD: 'handleRandomTextLoad', RANDOM_TEXT_LOADED: 'handleRandomTextLoaded', GAME_TICK: 'handleGameTick',