1 00:00:00,770 --> 00:00:05,970 It this section we're going to write out a research for our very last department our policies Department. 2 00:00:05,970 --> 00:00:10,380 So in this case we're going to get a list of existing policies we need to see if someone's trying to 3 00:00:10,380 --> 00:00:16,470 create or end up policy if they are then we want to either remove their name from a list of our policies 4 00:00:16,620 --> 00:00:18,260 or add it onto our list. 5 00:00:18,360 --> 00:00:19,520 So let's get to it. 6 00:00:20,460 --> 00:00:24,700 Back over here I'm going to make a new reducer called policies. 7 00:00:24,750 --> 00:00:28,550 This is again going to be an arrow function. 8 00:00:28,770 --> 00:00:32,580 It's going to be called with a list of all of our existing policies. 9 00:00:32,980 --> 00:00:39,330 They'll be our first argument which I'll refer to as list of policies just as before I'm going to default 10 00:00:39,330 --> 00:00:43,380 the value of this thing in case our reducers being called for the very first time. 11 00:00:43,440 --> 00:00:49,650 And so a default value with an array like so and then the second argument will be an action or what 12 00:00:49,650 --> 00:00:52,860 are referred to as a form then inside of here. 13 00:00:52,860 --> 00:01:01,760 We're going to look to see if the action type is trying to create policy if that's the purpose of the 14 00:01:01,760 --> 00:01:06,830 action then we're going to do a very similar operation that we did previously up inside of our claims 15 00:01:06,830 --> 00:01:08,000 history reducer. 16 00:01:08,270 --> 00:01:13,190 So we want to take whoever's trying to sign up for a policy and add it to our existing list using the 17 00:01:13,190 --> 00:01:19,010 syntax you see right here because remember we did not want to directly mutate or modify the array that 18 00:01:19,010 --> 00:01:22,970 we are passed as the first argument right. 19 00:01:22,970 --> 00:01:26,120 So inside of your all say return a new array. 20 00:01:26,120 --> 00:01:27,130 Dot dot dot. 21 00:01:27,200 --> 00:01:32,710 List of policies and then we're going to add in action payload. 22 00:01:32,960 --> 00:01:35,820 And in this case we only care about including this person's name. 23 00:01:35,860 --> 00:01:38,190 So I'm going to take the name property out of. 24 00:01:39,630 --> 00:01:43,030 Case that helps the case in which someone is trying to create a policy. 25 00:01:43,130 --> 00:01:53,530 Next we'll say an LCF if the action type is equal to delete policy then we're going to return. 26 00:01:53,550 --> 00:01:58,500 And we want to take our list of policies we want to find the person's name who's trying trying to end 27 00:01:58,500 --> 00:02:01,990 their policy and remove it from that array to do so. 28 00:02:02,010 --> 00:02:07,470 We're going to use the filter helper which is built into every single javascript array to give you a 29 00:02:07,470 --> 00:02:11,880 very quick example on how the filter helper works over here on the console. 30 00:02:11,880 --> 00:02:14,780 I'll do a quick example or say Konst numbers equals 1 to 3. 31 00:02:14,780 --> 00:02:17,160 Again Ari did that. 32 00:02:17,160 --> 00:02:19,390 So if I reference numbers OK there's the array. 33 00:02:19,800 --> 00:02:24,710 So now I can do numbers sort filter all paths in a callback function. 34 00:02:24,840 --> 00:02:32,070 This inner function is going to be called with each number inside of that array which I'll abbreviate 35 00:02:32,100 --> 00:02:33,500 as an argument called numb. 36 00:02:33,780 --> 00:02:40,050 So then inside of here I can do a little boolean check to decide whether or not each value should be 37 00:02:40,050 --> 00:02:43,040 included or discarded from the original array. 38 00:02:43,140 --> 00:02:49,710 So I could do something like return numb not equal to two like so. 39 00:02:51,390 --> 00:02:54,750 And that's going to move the value to from the numbers array. 40 00:02:54,780 --> 00:03:00,380 Now to be precise it doesn't actually remove the number two from the numbers array the original numbers 41 00:03:00,390 --> 00:03:02,660 Ray still has the original set of values. 42 00:03:02,790 --> 00:03:08,520 So when we call a filter it produces a new array that has the filtered values inside of it. 43 00:03:08,520 --> 00:03:13,140 So essentially what we want to do right here is that same type of filter operation and we want to somehow 44 00:03:13,140 --> 00:03:20,370 reject or get rid of the person who has a policy with a name equal to action that payload does name. 45 00:03:20,370 --> 00:03:28,630 So long story short we're going to do list of policies that filter that's going to have the name of 46 00:03:28,660 --> 00:03:30,420 everyone who's signed up for a policy. 47 00:03:30,610 --> 00:03:36,010 And we're going to try to find and rechecked the name that is equal to action. 48 00:03:36,040 --> 00:03:37,870 Payload name like so 49 00:03:42,420 --> 00:03:42,810 OK. 50 00:03:42,830 --> 00:03:50,120 And then finally at the very bottom we're going to return our list of policies. 51 00:03:50,130 --> 00:03:50,610 All right. 52 00:03:50,610 --> 00:03:51,580 So that's it. 53 00:03:51,630 --> 00:03:52,430 That's the hard part. 54 00:03:52,470 --> 00:03:54,080 We just got through all the bad stuff. 55 00:03:54,120 --> 00:03:58,720 We've got three action creators for each of the three different types of forms that we have. 56 00:03:59,060 --> 00:04:04,260 Now we've got three different reducers one for each of our different departments are claims history 57 00:04:04,290 --> 00:04:06,730 our accounting and our policies. 58 00:04:06,820 --> 00:04:11,460 As you can probably tell there's that kind of a couple of different fine grained rules around how you 59 00:04:11,460 --> 00:04:16,680 put these different reducers together stuff like making sure that you do not modify the array that gets 60 00:04:16,680 --> 00:04:18,720 passed in or this first argument right here. 61 00:04:18,840 --> 00:04:23,910 And making sure that we always return some value but you'll get a better handle on some those fine grained 62 00:04:23,910 --> 00:04:24,920 rules over time. 63 00:04:24,930 --> 00:04:29,730 For right now I just care about making sure that you understand the overall goal of the producer the 64 00:04:29,730 --> 00:04:36,480 overall goal of reducer is to take some existing data some action and then modify and return that existing 65 00:04:36,480 --> 00:04:39,660 data based upon the contents of an action. 66 00:04:39,660 --> 00:04:39,920 All right. 67 00:04:39,960 --> 00:04:44,220 So now that we've put all this baseline stuff together we're going to take one more quick pause and 68 00:04:44,220 --> 00:04:48,060 then when we come back the next section we're going to use all is code that we've written to actually 69 00:04:48,090 --> 00:04:52,860 create a new instance of something called a redux store and then we're going to be able to test out 70 00:04:52,890 --> 00:04:55,590 our code so quick paused and see you in just a minute.