Now that we've had a tremendous amount of side discussion around reducers it's time to actually implement
our posts reducer.
Now I kind of have some bad news for you.
It turns out that having learned all this kind of new syntax over here it turns out we don't actually
need any of this syntax for the reducer that we are working on right now.
We will need it on another reducer that we're going to work on inside this application very very shortly.
But right now we don't have to do anything that fancy inside of here.
All right so to get started I'm going to remove all the code that I had inside there.
I'm going to make sure that I've got my arguments of state in action.
Our post producer is supposed to maintain a list of posts that we have fetched from that Jay sun API
because this is supposed to be dealing with an array or a list of records.
Eventually I'm going to default my state argument appear to be an empty array.
So the very first time that this thing gets called we're going to return this empty array.
Then inside of this reducer we're going to inspect the actions type.
So say if action type is equal to and we're going to watch for the action of type fecche posts whenever
we see that type right there that means that we have gotten an action with the response from RJC on
API so not going to save actual type is equal to fetch is.
Then I'm going to return action.
Payload like so.
And then of course I want to make sure that I handled the case in which we have not found an appropriate
action type so I'm going to say at the bottom return state.
So if we ever see an action that does not have the type fetch posts we're not going to worry about the
action at all.
We're just going to return the state that we had previously because remember we always have to not return
undefined from a producer.
Now the syntax you see right here is the same syntax that we've worked with previously back on the last
application but it's not actually the usual syntax you're going to see inside of or douceur in order
to make sure that we always handle every action that comes into every douceur without air error we very
frequently use a switch statement as opposed to an IF statement inside of reducer.
So let me show you what that would look like rather than having that if statement and the return at
the bottom.
We would instead have a switch statement a switch statement is valid javascript syntax this is not react
specific it's not redux specific.
For the switch we're going to take a look at the action duct tape type property and then we will define
a couple of cases inside of here.
So I'm going to first define a case of fetch oats and then I'll put a colon after that and then underneath
that case I'm going to put down what I want to do.
If the action not type property is equal to the string fetch underscore posts.
If that is the case then I want to return action payload.
So in practice if we end up having a lot of types that a single reducer cares about you'll end up seeing
a lot of case statements so you'll see like one case you'll see another case for another type and another
case for another type.
And we just add them all into this switch statement.
In this case producer is only ever going to care about the type fecche posts.
So we don't have to worry about adding in any other case statements.
Now let's make sure that we always return some value from our reducer.
We're always going to add in a default case to our switch statement.
So if we do not match the case of fecche posts once said fall through to the default Keystone here and
in the default case the case in which we do not have a action type that matches fetch posts are going
to simply return state like so.
All right so what you see right here is extremely common syntax to see inside of reducers the if statements.
We were working with before.
Definitely not very common.
I just kept it very simple so you could very easily see what was going on.
It is much more common to see inside of reducer a switch statement that looks exactly like this.
Although frankly you will have many other cases besides just the one that we have.
OK.
So this is looking pretty good.
So let's take a pause right here and we'll continue in the next video.