Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Building Applications with React and Redux Putting it all Together Challenge: The Player Detail Component

shu Chan
shu Chan
2,951 Points

selectedPlayerIndex not updating when removePlayer function is invoked

Hello As the title suggests I cannot figure out how to update the selectedPlayerIndex to -1 when it is invoked, this is what I've tried

import * as PlayerActionTypes from '../actiontypes/player';

const initialState = {
    players: [{
        name: 'Jim Hoskins',
      score: 31,
        created: '11/8/2016',
        updated: '11/9/2016'
    },
    {
        name: 'Andrew Chalkley',
        score: 20,
        created: '11/9/2016',
        updated: '11/10/2016'
    },
    {
        name: 'Alena Holligan',
        score: 50,
        created: '11/11/2016',
        updated: '11/12/2016'
    }
    ],
    selectedPlayerIndex: -1
}

let date = () => {
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth()+1; //January is 0!

  var yyyy = today.getFullYear();
  if(dd<10){
      dd='0'+dd;
  } 
  if(mm<10){
      mm='0'+mm;
  } 
  var today = dd+'/'+mm+'/'+yyyy;
  return today
}


export default function Player(state=initialState, action) {
  switch(action.type) {
    case PlayerActionTypes.ADD_PLAYER:
      return {
        players: [
          ...state.players,
          {
            name: action.name,
            score: 0,
            created: date(),
            updated: null
          }
        ],
        selectedPlayerIndex: state.selectedPlayerIndex
      };

    case PlayerActionTypes.REMOVE_PLAYER:
    const removePlayerList = [
      ...state.players.slice(0, action.index),
      ...state.players.slice(action.index + 1)
    ];
      return {
                  ...state,
          players: removePlayerList
      }

    case PlayerActionTypes.UPDATE_PLAYER_SCORE:
      return {
      players: 
        state.players.map((player, index) => {
          if(index === action.index) {
            return {
            ...player,
            score: player.score + action.score,
            updated: date()
            };
          }
        return player;
        })
      ,
      selectedPlayerIndex: state.selectedPlayerIndex
    }

    case PlayerActionTypes.SELECT_PLAYER:
      return {
        ...state,
        selectedPlayerIndex: action.index
      }

    default:
      return state;
  }
}

1 Answer

How about this?

case PlayerActionTypes.REMOVE_PLAYER:
    const removePlayerList = [
        ...state.players.slice(0, action.index),
        ...state.players.slice(action.index + 1)
    ];
    return {
        ...state,
        players: removePlayerList,
        selectedPlayerIndex: (state.selectedPlayerIndex === action.index) ? -1 : state.selectedPlayerIndex,
    };