Skip to content

Instantly share code, notes, and snippets.

@chaintng
Last active August 19, 2016 10:54
Show Gist options
  • Save chaintng/8c9179c75ca743686f0c93378ec09c2f to your computer and use it in GitHub Desktop.
Save chaintng/8c9179c75ca743686f0c93378ec09c2f to your computer and use it in GitHub Desktop.
pokedex-react-1.js
// 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