In the last section we finished modeling our person dropping off the form and the form itself by creating
a collection of different action creators and actions.
We're going to skip over this dispatch step right here.
Remember the dispatch is like our form receiver who is going to receive a form or an action as a refer
to it make a copy of that form and then send it out to all the different departments.
This dispatch thing right here is actually a part of the redux library itself.
So we don't have to write that from scratch.
So we're going to skip over dispatch and move over to implementing some reducers.
The idea behind these reducers things are that we're going to go a couple of different functions and
each function is going to model a different department inside of our company.
So we're going to have a reducer that is in charge of modeling the behavior of our claims history department.
A second rate douceur for our accounting department and a third reduce for our policies Department as
well.
Each one of these reducers is going to be called within action or a form that had been originally created
by an action creator.
The reducer is then going to inspect that form or that action and decide whether or not and needs to
modify some data based upon that action.
Now one quick thing Eleanor remind you about remember we had said that in order to make sure that this
management out here was able to easily read all the data produced by our departments we were going to
move all of our data to this outside repository of sorts.
And only when we had a form to pass in a department would we take that department's little slice of
data inside that repository and pass it into that department.
So that's going to start to become relevant when we put this reducers thing together.
All right.
So let's get to it.
I'm going to flip back over and then underneath our reaction creators are going to put down a new section
for our reducers And remember these are like our departments.
So we're going to first work on our claims history reducer every reduced or that we put together is
always going to be a function.
It always gets to arguments that are always passed in the exact same order.
The first argument is going to be whatever existing little piece of data from our central repository
that belongs to this particular department.
So I don't see that as an argument that I would call old list of claims I'm calling an old list of claims
because this is going to get this old set of claims right here.
And the expectation is that the reducers going to somehow update that list of claims depending upon
the contents of an action that is passed in as the second argument.
And remember that action right there is like our form.
So this is the same scenario that you just saw a little bit ago inside this diagram that claims history
which is a function is going to be passed the old list of claims and the form.
So now the redo cert needs to look at that form and decide whether or not it cares about that type of
this form.
If it is of type create claim that we want to make sure that the code inside of this function is going
to attempt to pull off that pillar property and add it to our list of claims and then return that if
it's not of a type that we care about that we don't care about this form at all and we're just going
to return our existing list of claims completely unchanged.
All right now.
Wrong Teb.
There we go.
So inside of here we're going to look at the actions type.
So I going to say action not type if that is equal to create claim then that means we care about this
action or form.
But if we don't get into that statement then we don't care about the form or let's call it what it really
is an action like so.
So now if we are inside the if statement we're going to update our list of claims with the payload of
this action right here or otherwise we will just return.
So inside the statement I'm going to say return again.
This next little bit of code right here is going to look a little bit strange but just bear with me
for a second.
I'm going to say Dot dot dot.
Old list of claims and then a comma and then action.
Payload like so.
All right.
So what's going on here.
This is just a little bit of yes 20:15 syntax.
What this does is it takes a array or the list of claims that's going to be a list right there in array
b dot dot dot means that we're going to take all the records inside there and add them to a brand new
array.
And then after that we're going to add in this new record right here of action not payload to help you
understand that syntax.
We can put together a quick example over and our consul over here I could say something like Konst numbers
equals 1 to 3.
And then I can create a new array by putting in a square bracket dot dot dot numbers and some new number
to insert that's going to create a brand new array with the values from numbers.
So when I run that I see one two three four dot dot dot dot.
In particular it essentially means that we're not going to nest a array inside this one.
We're just going to take all the values out of numbers and add it to the new array that we are creating.
So this is abbreviated syntax to essentially join the contents of two arrays together.
Now we could have written this line of code right here by putting together old list of claims Fourche
action not payload.
We could have done that.
But there's a very distinct difference between this line right here that we just wrote or that I just
thought right there.
And the one after it.
The return with the return statement.
The difference is that in the this case right here we are creating a brand new array and adding records
to it with the push function we are modifying an existing array.
This is something going to talk to you about in great great detail nonstop as we start working on reducers.
Any time that we want to change a record or change something inside a producer we always always always
100 percent of the time want to return a new array or a new object as opposed to modifying an existing
one.
So we always avoid as much as possible modifying existing data structures inside of a producer.
And I'll tell you why and just a little bit as we start to get towards the end of this example.
So I'm going to remove the push statement.
In general you're just about never going to see push inside of a reducer.
All right.
So this right here handles the case in which we care about this action.
We take our whole list of claims and we add on are the payload off the form or the action that we just
got.
And now down here we're going to handle the case in which we don't care about the action.
So I'll say return old list of claims like the
all right so looks good.
Now there's one little additional thing that we have to add to this reducer the reducer is a function.
So remember it doesn't really have any idea of what its data is until it gets cold for the first time.
We need to make sure that for this all list of claims we need to make sure that we handle the case in
which our seducer is being called for the very first time in which a we might not have some original
list of claims.
So what you mean by that is that the very first time that our reduced or gets called there will be no
data no previous list of claims to be passed in here.
Instead the first time this producer gets called we essentially received the value undefined as that
first argument.
So in case this reducers being called for the very first time we need to default the value of that first
argument to do so.
I'm going to out on a equals array like so.
So now if someone ever calls claims history with a value of undefined for old list of claims then that
undefined value will be placed replaced with an empty array.
Ok so thats say completed reducer right there.
Now lets keep writing our other two.
We're going to get through the other two reducers much more quickly.
So I'm now going to get myself a little bit of space and then I'll put down a How about accounting will
take care of the accounting department next oh I just realized I got a little typo right there.
It should be all list of claims with a lower case L.
That's better.
Ok so now for the accounting reducer are again going to make a function that's going to be called with
all our current bag of money.
Because remember that's the purpose of the accounting department.
So we get our bag of money right here that's going to basically be a simple number as opposed to the
array that we had for our claims history.
And then the accounting department is going to have a little bit more complex logic inside of it.
So the first argument will be our bag of money.
The second argument will be our action.
Now the first thing we want to do is see if the action that's come in has something to do with a claim
if it does then we want to somehow pull off the money from the profit the payload of that and make sure
that we subtract some amount of money from our bag of money.
So inside of hearing what to say if action that type equals creates claim if that's the case then I'm
going to return a bag of money minus.
And remember this action right here.
The actual amount of money that we want to deduct the amount of money that our customer wants to collect
can be accessed from this object right here.
So it will be action.
Payload dot amount of money to collect.
And I just realized I got a little typo there as well as should be amount of money to collect not collection.
That's better.
OK.
Now I'll go back down.
So it's going to be action that payload that amount of money to collect cool.
All right.
Now let's take care of the other case the case in which this is not trying to create a claim.
So the next year we're going to do is check to see if the action is trying to create a policy if they
are trying to create a policy then we're going to take whatever money was included in that action's
payload and add it into our bag of original bag of money and return it.
So if else or somebody else if else if action type is equal to create policy then we're going to return
a bag of money plus action payload amount.
And then finally the only other case is that someone is not trying to create a new claim or create a
new policy.
And so in that case we don't care about this form at all.
So it's going to return the current value of our bag of money.
And so I'm going to return a bag of money just as before the first time this producer gets called.
We don't have any value for bag of money.
It's going to be of value undefined and so we're going to default the value of this thing to some reasonable
default value.
We'll start off saying that our company initially has a $100 like so and so if someone tries to create
a claim and take money from our company we'll subtract 100 minus how much money they're trying to collect
if they're trying to create a policy and pay us money.
We'll take our bag of money and add in that amount and return it.
Otherwise we'll just return our bag of money.
All right let's take another pause right here.
When we come back the next section we're going to work on our last reduced our policies department so
quick pause and I'll see you in just a minute.