Module 6: Workout Table
In this module, we'll create a component that displays our workout and has buttons to edit and delete workouts.
Step 1. Setting Up Our Component
Since we defined our functions that make API calls in our WorkoutIndex
we can make this component a functional component, since we don't need to deal with the state at all. Let's add some code to WorkoutTable.js
, start it off with the following:
Notice that we're including props so that we can use them here.
Step 2. Return
Since we're dealing with a functional component, we really just need to work on our return, to correctly display the workout information in the format that we want it in.
Change your return to look like this:
Analysis
So, here we're setting up our table to hold our information, with our header, and our table started. 1. Right now our body is empty though, so we need to figure out a way to take our workout information that we're getting from above as a prop, and map it out into the correct format. Put the following code in between the <tbody>
and </tbody>
tags.
Analysis
We're using map, which iterates through an array and performs the same operation on each item in the array, in this case, we're formatting each item of the array as a table row.
See how we used to return, this is important when doing a multi-line map, or it won't know what to return.
Notice how we need a key because react requires unique keys for elements created that are otherwise identical.
Check out the two buttons, one is for deleting, and one is for updating. Notice how
onClick
we're calling functions that come from the props, that we defined in ourWorkoutIndex
Back to WorkoutIndex
In WorkoutIndex.js
we need to actually render this component.
Let's first import our component:
Next, we need to create a function that'll allow us to delete workouts. In the workout table we see that we have something called props.delete
but we haven't yet created a function that'll allow us to delete. Let's do that now. Create the following function beneath fetchWorkouts
, call it workoutDelete
.
Analysis
Notice that we're passing the event to this function, so that we can use it in #3, to grab the information from the event.
The big difference between this method and things like the create method, is our method is now
DELETE
.Notice that in the body, we're specifically passing the id, which is all we need on the backend to delete a workout. We need to be able to grab the id of the workout and delete it.
Again, we're passing our token to our Authorization header, so that we can make API calls.
After the delete is done, we're fetching workouts again, so that we can update our display.
Next, we'll need to work on our return to actually get our Workouts Table to display!
Inside of the render()
but before the return()
, we need to add some code to format our workouts for us.
Analysis:
first thing we're doing is checking to see if the workouts array in the state of our
WorkoutIndex.js
is greater than or equal or 1, i.e are there are any workouts. Remember when the component mounts, we're grabbing all the workouts in a get, so they should be there if there are any in the DB.If there are workouts, we display
<WorkoutsTable>
, if not we say "log a workout to see table"Notice all of the props that we are sending to
WorkoutsTable
, the workouts (which we need in WorkoutsTable to render them), the update function (so that we can call it), and the delete function (so that we can call it in the table).
Then we need to actually put this constant we just created into the render in WorkoutIndex
. See the adding workouts
comment below. This is the render code of WorkoutIndex
at this point.
This is What it Should Look Like:
Now, after we've linked everything up in WorkoutIndex.js
, we should see this:
Finished Code
Last, we're going to work on our Workout Edit!
Last updated