Skip to content

Commit

Permalink
Add Sagas
Browse files Browse the repository at this point in the history
  • Loading branch information
derekgreenberg committed Feb 26, 2016
1 parent bd27582 commit bd578d9
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions App/Actions/ActionCreators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ import types from './ActionTypes'
const createAction = (type, params = null) => ({ type, ...params })

export const initializeGame = (tileCount, maxRows, maxColumns) => createAction(types.INITIALIZE_GAME, { tileCount, maxRows, maxColumns })
export const tileTaken = (row, column, owner, orientation) => createAction(types.TILE_TAKEN, { row, column, owner, orientation })
3 changes: 2 additions & 1 deletion App/Actions/ActionTypes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default {
INITIALIZE_GAME: 'INITIALIZE_GAME'
INITIALIZE_GAME: 'INITIALIZE_GAME',
TILE_TAKEN: 'TILE_TAKEN'
}
5 changes: 4 additions & 1 deletion App/Components/GameTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ class GameTile extends Component {
}

handlePress () {
const {onTilePress, row, column, currentPlayer} = this.props
if (this.canPress()) {
this.updateOrientation()
this.updateOwner()
onTilePress(row, column, currentPlayer, this.state.orientation)
}
}

Expand Down Expand Up @@ -145,7 +147,8 @@ GameTile.propTypes = {
row: PropTypes.number,
column: PropTypes.number,
currentPlayer: PropTypes.string,
gameInProgress: PropTypes.bool
gameInProgress: PropTypes.bool,
onTilePress: PropTypes.func
}

export default GameTile
19 changes: 17 additions & 2 deletions App/Containers/GameBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ class GameBoard extends Component {
this.tileCount = 0
this.maxColumns = 0
this.maxRows = 0
this.handleTilePress = this.handleTilePress.bind(this)
}

componentDidMount () {
this.props.dispatch(actions.initializeGame(this.tileCount, this.maxRows, this.maxColumns))
const {dispatch} = this.props
dispatch(actions.initializeGame(this.tileCount, this.maxRows, this.maxColumns))
}

handleTilePress (row, column, owner, orientation) {
const {dispatch} = this.props
dispatch(actions.tileTaken(row, column, owner, orientation))
}

renderColumn (row) {
Expand All @@ -31,7 +38,15 @@ class GameBoard extends Component {
this.maxColumns = maxColumns
for (let col = 0; col < maxColumns; col++) {
let id = `row_${row}_col_${col}`
let element = <GameTile key={id} ref={id} row={row} column={col} currentPlayer={currentPlayer} gameInProgress={gameInProgress} />
let element = <GameTile
key={id}
ref={id}
row={row}
column={col}
currentPlayer={currentPlayer}
gameInProgress={gameInProgress}
onTilePress={this.handleTilePress}
/>
tiles.push(element)
this.tileCount++
}
Expand Down
7 changes: 5 additions & 2 deletions App/Root.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

import GameScreen from './Components/GameScreen'
import { Provider } from 'react-redux'
import configureStore from './Store/ConfigureStore'
Expand All @@ -11,6 +9,11 @@ import React, {
} from 'react-native'

export default class Root extends Component {
constructor (props) {
super(props)
console.disableYellowBox = false
}

render () {
return (
<Provider store={store}>
Expand Down
12 changes: 12 additions & 0 deletions App/Sagas/Gameboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { put, take } from 'redux-saga/effects'
import Types from '../Actions/ActionTypes'
import Actions from '../Actions/ActionCreators'

export function * watchTilePress () {
while (true) {
const {row, column, owner, orientation} = yield take(Types.TILE_TAKEN)
console.log('************* saga invoked!!!')
console.log({row, column, owner, orientation})

}
}
5 changes: 5 additions & 0 deletions App/Sagas/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { watchTilePress } from './Gameboard'

export default [
watchTilePress
]
7 changes: 6 additions & 1 deletion App/Store/ConfigureStore.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { createStore, applyMiddleware } from 'redux'
import RootReducer from '../Reducers/'
import createLogger from 'redux-logger'
import R from 'ramda'

// middleware that injects into the store chain
import createLogger from 'redux-logger'
import sagaMiddleWare from 'redux-saga'
import sagas from '../Sagas'

const LOGGING_BLACKLIST = ['EFFECT_RESOLVED', 'EFFECT_TRIGGERED', 'EFFECT_REJECTED']

// the master switch to turn off logging
Expand All @@ -21,6 +25,7 @@ const logger = createLogger({
// NOTE: it's important that logger is last apparently.
// https://github.com/fcomb/redux-logger/issues/20
const finalCreateStore = applyMiddleware(
sagaMiddleWare(...sagas),
logger
)(createStore)

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"react-redux": "^4.4.0",
"redux": "^3.3.1",
"redux-logger": "^2.5.2",
"redux-saga": "^0.9.1",
"seamless-immutable": "^5.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit bd578d9

Please sign in to comment.