Skip to content

Instantly share code, notes, and snippets.

@antronic
Forked from chaintng/pokedex-react-1.js
Created August 19, 2016 10:54
Show Gist options
  • Save antronic/2a8efc81b064bfbb18e771a6d5af2446 to your computer and use it in GitHub Desktop.
Save antronic/2a8efc81b064bfbb18e771a6d5af2446 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