1 00:00:00,180 --> 00:00:06,470 Great, we can increase the amount of items in a cart, so now let's start working with a decrease, 2 00:00:06,500 --> 00:00:09,060 let's bite the bullet and get it done. 3 00:00:09,560 --> 00:00:13,830 Now, the functionality will be almost exactly the same. 4 00:00:13,830 --> 00:00:18,480 However, we do need to check whether the amount is equal to one. 5 00:00:18,820 --> 00:00:24,620 So if the amount is equal to one, then we will remove the item from the cart. 6 00:00:25,020 --> 00:00:27,510 But initial set up will be exactly the same. 7 00:00:27,780 --> 00:00:34,560 Again, we are returning a new state so that let's copy the old state values for the time being will 8 00:00:34,560 --> 00:00:38,100 have a cost equal to our temporary variable. 9 00:00:38,460 --> 00:00:43,160 So I'm going to go with Unlet and stamp cardigan. 10 00:00:43,680 --> 00:00:49,470 And as far as the value for the time being, I'm going to set this equal to an empty array just because 11 00:00:49,470 --> 00:00:53,590 we are going to be checking if this is going to be undefined, we'll have another. 12 00:00:54,000 --> 00:00:56,940 So let me just overwrite this as a temp card then. 13 00:00:56,940 --> 00:00:58,380 Like I said, we have two options. 14 00:00:58,680 --> 00:01:03,870 Either the amount is equal to or bigger than one or it is in fact equal to one. 15 00:01:04,020 --> 00:01:06,230 And then, of course, we would want to remove that item. 16 00:01:06,240 --> 00:01:15,030 So I'm going to go with if action payload amount is equal to one, then do one thing. 17 00:01:15,390 --> 00:01:20,730 But then if the actual amount is not equal to one, which is going to be the case, of course, if the 18 00:01:20,730 --> 00:01:27,240 amount is bigger than one, then do something else and we will start with that, something else, because 19 00:01:27,240 --> 00:01:33,120 it's going to be a bit more easier because the actual functionality is exactly the same like we already 20 00:01:33,330 --> 00:01:34,200 have here. 21 00:01:34,560 --> 00:01:39,310 The only difference is that instead of increasing the amount, we will decrease. 22 00:01:39,690 --> 00:01:46,860 So if the amount is equal to one was just log, hey, it's one. 23 00:01:47,040 --> 00:01:48,390 And of course I cannot do this. 24 00:01:48,390 --> 00:01:50,130 I need to have my template literals. 25 00:01:50,520 --> 00:01:53,220 So let's write, hey, it's one. 26 00:01:53,520 --> 00:01:57,150 And then if it's not the case, then we're just going to decrease the amount. 27 00:01:57,450 --> 00:02:00,750 So like I said, we have our trump card at the moment. 28 00:02:00,750 --> 00:02:07,850 It is equal to an empty array, but we have already functionality for increasing and decreasing. 29 00:02:08,070 --> 00:02:09,870 It's exactly the same. 30 00:02:10,200 --> 00:02:11,550 Now let me copy and paste. 31 00:02:12,480 --> 00:02:18,170 And the only thing will change is, of course, we're not going to declare a new variable or variable 32 00:02:18,180 --> 00:02:19,060 will stay the same. 33 00:02:19,380 --> 00:02:20,850 So I'm going to go with timecard. 34 00:02:21,060 --> 00:02:26,130 And what I'm saying right now is, yes, at the moment it will be an empty array at the very beginning. 35 00:02:26,400 --> 00:02:32,460 But then if the amount is bigger than one and of course, we are going to assign it to a new array. 36 00:02:32,640 --> 00:02:38,460 Again, we're mapping over the items, were checking the ID if the matches and we just subtract one 37 00:02:38,610 --> 00:02:40,080 instead of add one. 38 00:02:40,170 --> 00:02:40,610 That's all. 39 00:02:40,770 --> 00:02:42,430 That's the only change we would need. 40 00:02:42,450 --> 00:02:42,680 Right. 41 00:02:42,990 --> 00:02:43,890 So let me save it. 42 00:02:44,110 --> 00:02:46,170 And then, of course, I'm going to be increasing. 43 00:02:46,200 --> 00:02:46,830 OK, fine. 44 00:02:46,840 --> 00:02:47,220 Awesome. 45 00:02:47,430 --> 00:02:52,660 And since that is bigger than one, each and every time I'm going to click, I will decrease that amount. 46 00:02:52,920 --> 00:02:53,730 So that is good. 47 00:02:53,970 --> 00:02:57,580 But of course, by the time I'm going to get the one, there should be a console. 48 00:02:57,610 --> 00:02:59,460 OK, so now it is equal to one. 49 00:02:59,700 --> 00:03:02,490 I click on it and now I just deleted the whole card. 50 00:03:02,670 --> 00:03:07,110 Why I deleted the cohort because time code is equal to an empty array. 51 00:03:07,470 --> 00:03:07,910 Correct. 52 00:03:08,220 --> 00:03:12,360 So we had in statement the value was equal to one. 53 00:03:12,610 --> 00:03:19,560 So we just cancel out page one and then instead of going for and else where we actually set our time 54 00:03:19,560 --> 00:03:24,900 card equal to an array with a decreased item, we just left it as an empty array. 55 00:03:25,110 --> 00:03:27,180 And that's why our card right now is empty. 56 00:03:27,420 --> 00:03:28,920 But we have our console log. 57 00:03:29,100 --> 00:03:29,850 Hey, it's one. 58 00:03:30,330 --> 00:03:36,060 So we successfully set up the functionality where we can decrease if the amount is bigger than one. 59 00:03:36,270 --> 00:03:38,850 Now let's fix up our code in our IF statement. 60 00:03:39,090 --> 00:03:45,960 So instead of simple console log, we will do a filter functionality again, just like we had before. 61 00:03:46,620 --> 00:03:54,480 So the same thing again, we just need to check if the amount is equal to one, then remove that item 62 00:03:54,570 --> 00:03:55,310 altogether. 63 00:03:55,530 --> 00:03:59,730 And again, this would be a case where it's much more simpler just to copy and paste. 64 00:04:00,060 --> 00:04:03,630 Now, I'm not a big fan of copying pasting the same time. 65 00:04:03,630 --> 00:04:06,310 Rewriting the same syntax just doesn't make sense. 66 00:04:06,540 --> 00:04:10,240 So let me copy everything from the state like. 67 00:04:10,260 --> 00:04:14,310 So let's scroll up where we have the amount, copy and paste. 68 00:04:14,490 --> 00:04:20,400 And then in this case, we are just going to set our timecard equal to state court. 69 00:04:20,590 --> 00:04:24,180 We filter out and then again, we're calling our callback function. 70 00:04:24,390 --> 00:04:26,110 We have our court remedy. 71 00:04:26,130 --> 00:04:27,890 So each and every item has the duty. 72 00:04:28,170 --> 00:04:34,260 And if the item does not match and as I can see here, I made some kind of bug because it should be 73 00:04:34,270 --> 00:04:36,610 action payload ID. 74 00:04:36,660 --> 00:04:42,810 Let me say it here and also probably, I should say, right here in the bottom as well, because something 75 00:04:42,810 --> 00:04:46,130 must have happened while I was working with my producer. 76 00:04:46,320 --> 00:04:49,650 So again, we just need to go back to action payload ID. 77 00:04:49,930 --> 00:04:52,080 That's the property that we're looking for. 78 00:04:52,320 --> 00:04:55,110 And now in this case, I can just check, OK? 79 00:04:55,200 --> 00:04:58,050 I can increase I can increase all of them, of course. 80 00:04:58,290 --> 00:05:04,620 And then the moment I'm going to go below one, you'll see how I remove the item again, which is decrease 81 00:05:04,620 --> 00:05:05,070 the amount. 82 00:05:05,070 --> 00:05:09,840 And then once we go below one, then we just remove the item from the cart.