It this section we're going to write out a research for our very last department our policies Department.
So in this case we're going to get a list of existing policies we need to see if someone's trying to
create or end up policy if they are then we want to either remove their name from a list of our policies
or add it onto our list.
So let's get to it.
Back over here I'm going to make a new reducer called policies.
This is again going to be an arrow function.
It's going to be called with a list of all of our existing policies.
They'll be our first argument which I'll refer to as list of policies just as before I'm going to default
the value of this thing in case our reducers being called for the very first time.
And so a default value with an array like so and then the second argument will be an action or what
are referred to as a form then inside of here.
We're going to look to see if the action type is trying to create policy if that's the purpose of the
action then we're going to do a very similar operation that we did previously up inside of our claims
history reducer.
So we want to take whoever's trying to sign up for a policy and add it to our existing list using the
syntax you see right here because remember we did not want to directly mutate or modify the array that
we are passed as the first argument right.
So inside of your all say return a new array.
Dot dot dot.
List of policies and then we're going to add in action payload.
And in this case we only care about including this person's name.
So I'm going to take the name property out of.
Case that helps the case in which someone is trying to create a policy.
Next we'll say an LCF if the action type is equal to delete policy then we're going to return.
And we want to take our list of policies we want to find the person's name who's trying trying to end
their policy and remove it from that array to do so.
We're going to use the filter helper which is built into every single javascript array to give you a
very quick example on how the filter helper works over here on the console.
I'll do a quick example or say Konst numbers equals 1 to 3.
Again Ari did that.
So if I reference numbers OK there's the array.
So now I can do numbers sort filter all paths in a callback function.
This inner function is going to be called with each number inside of that array which I'll abbreviate
as an argument called numb.
So then inside of here I can do a little boolean check to decide whether or not each value should be
included or discarded from the original array.
So I could do something like return numb not equal to two like so.
And that's going to move the value to from the numbers array.
Now to be precise it doesn't actually remove the number two from the numbers array the original numbers
Ray still has the original set of values.
So when we call a filter it produces a new array that has the filtered values inside of it.
So essentially what we want to do right here is that same type of filter operation and we want to somehow
reject or get rid of the person who has a policy with a name equal to action that payload does name.
So long story short we're going to do list of policies that filter that's going to have the name of
everyone who's signed up for a policy.
And we're going to try to find and rechecked the name that is equal to action.
Payload name like so
OK.
And then finally at the very bottom we're going to return our list of policies.
All right.
So that's it.
That's the hard part.
We just got through all the bad stuff.
We've got three action creators for each of the three different types of forms that we have.
Now we've got three different reducers one for each of our different departments are claims history
our accounting and our policies.
As you can probably tell there's that kind of a couple of different fine grained rules around how you
put these different reducers together stuff like making sure that you do not modify the array that gets
passed in or this first argument right here.
And making sure that we always return some value but you'll get a better handle on some those fine grained
rules over time.
For right now I just care about making sure that you understand the overall goal of the producer the
overall goal of reducer is to take some existing data some action and then modify and return that existing
data based upon the contents of an action.
All right.
So now that we've put all this baseline stuff together we're going to take one more quick pause and
then when we come back the next section we're going to use all is code that we've written to actually
create a new instance of something called a redux store and then we're going to be able to test out
our code so quick paused and see you in just a minute.