From 811756b0d17d5da6c184e031a3ef453eacb84971 Mon Sep 17 00:00:00 2001 From: Vincent Taverna Date: Tue, 28 Feb 2017 17:36:33 -0500 Subject: [PATCH] fix #190 sorting pokemon list after sorting species list --- app/reducers/trainer.js | 58 +++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/app/reducers/trainer.js b/app/reducers/trainer.js index b0a0d7c..ef36233 100644 --- a/app/reducers/trainer.js +++ b/app/reducers/trainer.js @@ -111,42 +111,46 @@ function getNewMonsters(state, monsters, sortBy, sortDir) { // Anytime the speciesState changes we should recount selected function updateSpecies(state, index, updater) { - const speciesAtIndex = state.monsters.species[index] - const updatedSpecies = Object.assign({}, speciesAtIndex, updater(speciesAtIndex)) - - const updatedMonsters = Object.assign({}, state.monsters, { - species: Immutable.array.set(state.monsters.species, index, updatedSpecies) - }) + const specieAtIndexPokemonId = index + 1 + const indexForSpecie = state.monsters.species.findIndex(specie => specie.pokemon_id === specieAtIndexPokemonId) + const specieAtIndex = state.monsters.species[indexForSpecie] + const updatedSpecies = { ...specieAtIndex, ...updater(specieAtIndex) } + + const updatedMonsters = { + ...state.monsters, + species: Immutable.array.set(state.monsters.species, indexForSpecie, updatedSpecies), + } - const updatedStateWithMonsters = Object.assign({}, state, { - monsters: updatedMonsters - }) + const updatedStateWithMonsters = { + ...state, + monsters: updatedMonsters, + } const { speciesState, selectedCount } = getNewSpeciesState(updatedStateWithMonsters) - return Object.assign({}, updatedStateWithMonsters, { + return { + ...updatedStateWithMonsters, speciesState, - selectedCount - }) + selectedCount, + } } function updateSpeciesState(state, id, updater) { const { - speciesState + speciesState, } = state - const newSpecieState = {} const existingSpecieState = speciesState[String(id)] - newSpecieState[String(id)] = Object.assign( - {}, - existingSpecieState, - updater(existingSpecieState) - ) - - return Object.assign({}, speciesState, newSpecieState) + return { + ...speciesState, + [String(id)]: { + ...existingSpecieState, + ...updater(existingSpecieState), + }, + } } function updatePokemonState(speciesState, pid, updater) { @@ -252,17 +256,19 @@ export default handleActions({ SORT_SPECIES(state, action) { const { sortBy, - speciesIndex + speciesIndex, // this index could be wrong when sorted, so we use findIndex } = action.payload - const pokemonId = state.monsters.species[speciesIndex].pokemon_id + const pokemonId = speciesIndex + 1 + const specieState = state.speciesState[pokemonId] const sortDir = getNewSortDirectionFromSortBy(sortBy, specieState) - const updatedSpeciesState = Object.assign({}, state, { - speciesState: updateSpeciesState(state, pokemonId, () => ({ sortDir, sortBy })) - }) + const updatedSpeciesState = { + ...state, + speciesState: updateSpeciesState(state, pokemonId, () => ({ sortDir, sortBy })), + } return updateSpecies(updatedSpeciesState, speciesIndex, (speciesAtIndex) => { const sorted = getSortedPokemon(speciesAtIndex, null, sortBy, sortDir)