Last active
August 19, 2016 10:54
-
-
Save chaintng/8c9179c75ca743686f0c93378ec09c2f to your computer and use it in GitHub Desktop.
pokedex-react-1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. set Reducer function to accept store.dispatch() | |
var reducer = function (state, action) { | |
var newState = state; | |
if (typeof state === 'undefined') { | |
return { | |
pokemonType: null, | |
pokemonAtk: null | |
}; | |
} | |
switch (action.type) { | |
case 'CHANGE_POKEMON_TYPE': | |
newState.pokemonType = action.pokemonType; | |
return newState; | |
case 'CHANGE_POKEMON_ATK': | |
newState.pokemonAtk = (action.pokemonAtk && action.pokemonAtk.length > 0) ? action.pokemonAtk : null; | |
return newState; | |
default: | |
return null; | |
} | |
}; | |
var store = Redux.createStore(reducer); | |
// 2. set Listener to dispatch such event | |
document.getElementById('pokemonTypeFilter') | |
.addEventListener('change', function (e) { | |
store.dispatch({ type: 'CHANGE_POKEMON_TYPE', pokemonType: e.target.value }) | |
}); | |
document.getElementById('pokemonAtkFilter') | |
.addEventListener('keyup', function (e) { | |
store.dispatch({ type: 'CHANGE_POKEMON_ATK', pokemonAtk: e.target.value }) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment