All right to wrap everything up.
We just have to add in our case to handle this new action type of edit blog post.
So inside my reducer right above the delete blog post hype all adding a new case statement in watch
for edit underscore why post so then inside of here like we just said we need to take a look at this
action object's payload property inside that payload property is the idea of the blog post so we want
to update so then we'll go through our state array we're going to make a change to the given blog post
and then return a brand new array with all of our existing blog posts and the brand new edited one as
well.
So to do so I'm going to add in a return.
We're then going to do a state dot map.
So we're going to map through all of our different blog posts.
Once we find the one with the correct I.D. we will return the blog post inside of our action dot payload
property as opposed to the blog post we are iterating over.
Let's write out some code and you'll see what we're doing really quickly.
So inside my state dot map function I'm going to receive each of the old blog posts one by one so then
inside of here it will say if the blog posts we are currently iterating over if it's I.D. is equal to
action dot payload I.D..
So if we just found the right blog post then we do not want to return the old blog post.
Instead we want to return the edited one which is available as action not payload.
So I will return.
Action Dot.
Payload otherwise we will return the existing blog post.
So in this case I will return blog post like so.
Now if we wanted to we could definitely use a Turner expression to abbreviate this code just a little
bit.
This is a classic classic.
If else statement right here so we could abbreviate this I can write out a Turner expression right above
and then eventually delete the return statement or the if statement.
So I will say return blog post dot I.D. equal to action payload I.D. and we'll put in our question mark
on the next line down Just make sure this is a little bit legible and we'll say action dot payload and
then otherwise indicated by the colon blog post.
So now we can delete that entire if statement and that's a little bit more concise okay.
Let's save this.
You'll notice my code jumps as usual again that's from my code for matter what you see on the screen
is 100 percent equivalent to what I had just a moment ago.
So now I can save this file I'll flip back over.
I'm going to take a look at test post one right here.
I'll tap on edit and I'll make some changes.
So how about edited post 1 and edited content for post 1.
I'll then save that change.
Now when I type save right here we're not currently doing any navigation which is totally fine.
So we need to manually navigate back to our list of blog posts so I'll hit back back and I see the edited
title right there.
So clearly we have successfully updated our blog post.
Let's make sure we can still create a blog post as well.
So I will hit that add button and then blog 2 and 2 I'll save and yep that's created as well.
So this looks pretty good.
So now I think the very last thing we have to do here is make sure that after we successfully edit a
post we either navigate the user back to the previous screen right here or we should just send them
directly back to the index screen one or the other.
So let's figure out how to tackle that in the next video.