Challenge Steps

In the 'actiontypes/player.js' file, add and export a new action for SELECT_PLAYER

Define a reducer that will interpret the SELECT_PLAYER action and produce a new state for the playerDetail data

In 'reducers/player.js', in the ADD_PLAYERswitch block, set the newly added player’s created value to the current date

Add a switch case for the new SELECT_PLAYER action type. You'll need to set the selectedPlayerIndex to the value passed in -- for example, with action.index.

In the UPDATE_PLAYER_SCOREswitch case, set the player's updated value to the current date

In 'actions/player.js', create the action creator for the selectPlayer action. The new action creator should take an index argument and generate an action object with a type of SELECT_PLAYER and provide the index as the action metadata.

In the Scoreboard component, don't forget to update the mapStateToProps method to account for the selectedPlayerIndex and, in the render method, pull selectedPlayerIndex off this.props

Add a new bound action creator for the selectPlayer action creator

Use the index to pull out just the selectedPlayer from the players array if the selectedPlayerIndex is not -1 (a conditional would work great for this)

Pass the selectPlayer bound action creator to the player component and, in the return statement, the prop provided to PlayerDetail should be the selelectedPlayer object

In the new PlayerDetail component, use the data being passed via the selectedPlayer prop to display the name, score, created, and updated data, if the selectedPlayer prop is being passed. Otherwise, it should display the paragraph "Click on a player to see more details".

When you click on a player, it should display the details for that player in the PlayerDetail component. In the Player component, you'll devise a way to detect an onClick for a player that invokes the selectPlayer bound action creator

The function you write should provide the index of the player as an argument -- similar to the onClick for removePlayer

Don't forget to update your prop validation for selectedPlayerIndex in the Scoreboard component and selectPlayer the Player component, and make sure you also have prop validation on the selectedPlayer info coming into the new PlayerDetail component.

0:00

Using Redux for the simple scoreboard
application was a great way of getting

0:03

comfortable with the basics of Redux.

0:05

Now mastering when to use Redux
takes a lot more practice,

0:09

but we have to start with the basics.

0:11

So now it's your turn to use the skills,
you learn in this course

0:14

to add a new feature to the scoreboard
app using React and Redux.

0:18

Let's have a look at where
you're going to build.

0:20

In this challenge,

0:21

you're going to add a player detail
component to the scoreboard app.

0:25

I've already included some code
to get you started so to start.

0:28

You'll need to download the project
files for this challenge and

0:30

run N-P-N install to install
all the dependencies.

0:33

I've posted the link to the files
in the teacher's notes.

0:36

So the new player detail component
should display a player's name, score,

0:40

the date when the player was
added to the scoreboard,

0:43

as well as the date when
the player score was updated.

0:45

And this information
should appear down here in

0:49

the player detail component whenever a
user clicks on a player in the scoreboard.

0:54

So for example when I click on Andrew,
we see Andrew Chalkley,

0:59

score 20, created on November 9,
2016 and updated November 10, 2016.

1:05

Now if you update Andrew's score,
you see the score and

1:10

the player detail component
updated as well, and

1:13

the updated date changes to 11/15/2016,
the current date.

1:19

And when you add a player to the score
board and click on the new player,

1:24

you'll see the score, created date,
and no updated value yet but

1:29

once you update the new player score
it changes to the current date.

1:34

In the project files, I've already
included a player detail component with

1:38

some of the code to get you started, so
this is a pure component that's going

1:43

to provide the currently
selected players information.

1:46

And over in the producer's player
digest file, you'll notice that

1:50

the initial state is a little different,
it has some extra data in it.

1:55

So the initial state constant is now
an object containing players array and

2:00

in the player's array the player
objects have new data for

2:05

the created and updated date.

2:07

That will display in the player
detail component and

2:12

I just added some placeholder dates for
the initial state.

2:17

Then below the players array,
I added a separate field for

2:21

tracking the currently
selected player index you see.

2:25

You'll need to use the selected player
index to pass the correct player or

2:30

the player that was clicked to
the player detail component.

2:34

Then, you'll use what you learn about
redux like an action type reducer and

2:38

bound action creator to handle
the selected player data.

2:42

And display it in the player detail
component so now, let's walk through

2:46

the steps you'll need to take in
order to implement this feature.

2:49

I've also posted the steps
in the teacher's notes,

2:52

so first,
in the action types player.js file.

2:56

You'll need to add and
export a new action for

2:59

select the player just like you did for
these three.

3:03

Then you'll define a reducer that will
interpret the select player action and

3:07

produce a new state for
the player detail data.

3:10

So back in the reducer file down in the
add player switch block you'll need to set

3:16

the newly added players created value to
the current date with the javascript here.

3:22

Then you'll add a new switch case for the
new select Select player action type and

3:27

in this new block you'll need
to set the selected player index

3:32

to the value being passed in.

3:34

So for
example you can do it with action.index.

3:37

And finally,
in the UPDATE_PLAYER_SCORE switch case,

3:41

you also need to set the player's
updated value to the current date and

3:45

you can do things a little
different if you like.

3:48

So for example, I'm just displaying
a date for the Created and

3:52

Updated values but
you could also display a time for these.

3:56

Keep in mind that since the initial player
data is now being defined as an object,

4:01

containing a player's array.

4:03

And a selected player index field outside
that array, you'll need to make a few

4:07

changes to the way you produce a new
state in the reducer file switch block.

4:12

So for example, for the add player, remove
player and update player score cases

4:18

you'll want to update just the players
array data when returning a new state.

4:22

So you'll make a copy of the existing
state for both players and

4:26

selected player index and
change just the player's data.

4:30

Next over in the actionsPlayer.js file
you'll create the action creator for

4:35

the select player action
just like you did earlier.

4:39

So the new action creator should
take an index argument and

4:43

generate an action object
with a type of select player.

4:47

And provide the index as the action
metadata and over in scoreboard.js

4:52

Don't forget to update the map state
to props method to account for

4:57

the selected player index,
and up in the render method.

5:02

You're going to wanna pull selected
player index off this stop props,

5:07

so you'll add that here.

5:09

Then right below you'll add
a new bound action creator for

5:13

the select player action creator so
just like these three here.

5:17

And then we need to write some
javascript here that uses

5:21

the index to pull out
just the selected player

5:24

from the player's rate if the selected
player index is not negative one.

5:29

So a javascript conditional
would work great for that.

5:32

Then, right below you'll pass
the select player bound action creator

5:37

to the player component, and
the return statement the prop

5:41

provided to the playerDetail component
should be the selected player object.

5:47

All right, so back in the new player
detail component you're going to use

5:52

the data being passed via the selected
player prop to pass the name score and

6:00

the created and updated data, if
the selected player prop is being passed.

6:05

Otherwise, it should display the paragraph
click on a player to see more details and

6:09

again, when you click on a player,
it should display the details from that

6:14

player down here in
the player detail component.

6:16

So over in the player digest component,
you'll devise a way to detect

6:22

an on click for a player that invokes
the select player bound action creator.

6:27

And that function you write should provide
the index of the player as an argument, so

6:32

it's going to be very similar to
the On Click for remove player.

6:36

And finally, don't forget to
update your prop validation for

6:40

select player and scoreboard RTS and
the player component.

6:44

And make sure you also have prop
validation on the player info coming into

6:48

the new player detail component.

6:51

This extension to the scoreboard app is
a great example of a real purpose use case