1 00:00:00,190 --> 00:00:08,700 But we can add new product to the cart, but handle the cash if the item is our cart and we're going 2 00:00:08,700 --> 00:00:16,590 to do that by mapping over the cart, checking the I.D., if are matches, then of course, we'll set 3 00:00:16,590 --> 00:00:18,420 up a bit more functionality. 4 00:00:18,720 --> 00:00:21,090 And I'm going to start by creating a new era. 5 00:00:21,210 --> 00:00:22,460 So time card. 6 00:00:22,740 --> 00:00:28,720 And this is the case where it will iterate over our current cart in the state. 7 00:00:29,100 --> 00:00:34,170 Now, as far as the return, all right away set up my return where I'm going to say that I'm going to 8 00:00:34,170 --> 00:00:40,830 return all state values, of course, down to the state and then cart will be equal to my trump card. 9 00:00:41,130 --> 00:00:48,120 So the array that I'm about to set up and in here, we're going to go with state court maps. 10 00:00:48,120 --> 00:00:50,610 So we're mapping over the court. 11 00:00:50,850 --> 00:00:55,350 And then each item I'm going to represent as a court item. 12 00:00:55,650 --> 00:00:57,780 And then as far as the return, well. 13 00:00:58,810 --> 00:01:06,940 As a mapping over, I have two cases, I have a case where, of course, the idea is going to match 14 00:01:06,940 --> 00:01:11,780 to whatever I'm grabbing here, so whatever is coming from a single product. 15 00:01:11,800 --> 00:01:15,990 Just keep in mind, though, that we need to check for both of them. 16 00:01:16,240 --> 00:01:21,190 We need to check for the ID plus color, because that is the cart item. 17 00:01:22,090 --> 00:01:28,560 I'm using the ID in the product, but in the cart the ID is actually ID plus color. 18 00:01:28,810 --> 00:01:32,620 And if that is the case, then of course I'll have to change the amount. 19 00:01:32,980 --> 00:01:33,430 Correct. 20 00:01:33,820 --> 00:01:40,750 Because keep in mind, if I take a look at the complete application and if I remove the sticker, that 21 00:01:40,750 --> 00:01:44,330 utopia so far in the product, let me find it. 22 00:01:44,950 --> 00:01:47,170 Probably I won't be able to do it. 23 00:01:47,530 --> 00:01:50,260 Meaning it's just going to take me a while to find that utopia. 24 00:01:50,280 --> 00:01:53,740 So far, that's how it usually works when you want to showcase something. 25 00:01:53,920 --> 00:01:57,820 And there is now I have Utopia, so I'll use the same color. 26 00:01:58,150 --> 00:02:00,460 So it works exactly the same. 27 00:02:01,150 --> 00:02:02,940 I'm going to add one item over here. 28 00:02:03,250 --> 00:02:08,200 So now of course I have this item and then I would want to go back to continue shopping. 29 00:02:08,560 --> 00:02:11,620 And again, I'll look for the same item, same color. 30 00:02:11,770 --> 00:02:14,120 And now I want to add two more items. 31 00:02:14,380 --> 00:02:20,710 So this is the functionality that we're about to set up where if the item is already in a cart, I just 32 00:02:20,710 --> 00:02:22,120 want to increase that amount. 33 00:02:22,240 --> 00:02:23,710 That's all I have to do. 34 00:02:24,040 --> 00:02:29,810 If the item is not in the card, then of course, we're adding a new item, which, by the way, where 35 00:02:29,830 --> 00:02:35,880 it did in the previous video ligatures, that the same car item can have different colors. 36 00:02:35,890 --> 00:02:40,900 That's why the idea is actually equal to the ID plus the color. 37 00:02:41,350 --> 00:02:46,750 So in here, I would say if the ID matches, then there's going to be one functionality. 38 00:02:47,080 --> 00:02:51,520 If the ID doesn't match one, then I simply want to return that Cottenham. 39 00:02:51,760 --> 00:02:53,170 I don't want to do anything. 40 00:02:53,440 --> 00:03:01,450 I simply say return card item as it is, since I only want to update amount if the card item is already 41 00:03:01,450 --> 00:03:01,750 there. 42 00:03:01,930 --> 00:03:02,370 Correct. 43 00:03:02,680 --> 00:03:05,290 If it's not in the card, well, it doesn't really matter. 44 00:03:05,590 --> 00:03:12,910 Now the gotcher here is that of course I'm checking first for the item if it is and then I'm double 45 00:03:12,910 --> 00:03:15,880 checking one more time since I'm increasing the amount. 46 00:03:16,150 --> 00:03:18,430 So I'm checking if the item is in the card. 47 00:03:18,430 --> 00:03:18,940 Beautiful. 48 00:03:19,180 --> 00:03:24,420 And then if it is in the card, then of course I'll update this amount value. 49 00:03:24,790 --> 00:03:36,940 So say if the item ID matches and we say that by card item that ID is equal to ID plus color, then 50 00:03:36,940 --> 00:03:39,160 I'm going to create a new amount property. 51 00:03:39,460 --> 00:03:50,800 I'll say let me know and we're going with amount and that one is equal to cart item, whatever is the 52 00:03:50,800 --> 00:03:53,520 current one plus the amount that I'm passing in. 53 00:03:54,070 --> 00:03:54,550 All right. 54 00:03:54,880 --> 00:03:59,680 But the criteria is that we always need to check with our stock. 55 00:03:59,680 --> 00:04:00,160 Correct. 56 00:04:00,460 --> 00:04:04,240 And where we were setting up the stock, it was in the max property. 57 00:04:04,660 --> 00:04:10,600 So right after I set up my new amount, I actually want to check if the value is bigger than the max, 58 00:04:10,930 --> 00:04:14,760 that the amount will be equal to the max. 59 00:04:15,040 --> 00:04:21,550 So say another if statement if new amount is bigger than cart item max. 60 00:04:22,030 --> 00:04:29,260 So the property that is equal to the stock, then the new amount will be equal to a cart item. 61 00:04:29,260 --> 00:04:35,110 Max, essentially we're saying, yeah, you might have five items the first time, five items the second 62 00:04:35,110 --> 00:04:35,380 time. 63 00:04:35,620 --> 00:04:41,560 But if my stock market is just six, then there's going to be six items in court. 64 00:04:41,950 --> 00:04:47,610 And then this is going to be the case where I would want to return all the properties of the item. 65 00:04:48,070 --> 00:04:54,010 So in both cases, I'm returning the car item, or in this case, of course, I'm overriding those values. 66 00:04:54,010 --> 00:04:59,410 I say that amount is equal to a new amount like so. 67 00:04:59,710 --> 00:05:05,350 And now, of course, we can save and let's just go over the functionality one more time. 68 00:05:05,860 --> 00:05:11,860 So these are the things that we're passing in from and cut adequate amount on product. 69 00:05:12,250 --> 00:05:17,740 If the item is not in the cart, we create a new one with these properties. 70 00:05:18,070 --> 00:05:20,110 Can we keep in mind that I. 71 00:05:20,170 --> 00:05:24,940 D plus color is equal to the court item right now? 72 00:05:24,940 --> 00:05:32,590 If the item is already in the cart, then of course we iterate over the card check where the item is. 73 00:05:33,040 --> 00:05:34,750 So we find where is the item? 74 00:05:35,080 --> 00:05:42,190 And if the ID is equal to or added plus color, then of course we know that that is the current item 75 00:05:42,400 --> 00:05:45,100 and then we just need to increase the amount. 76 00:05:45,110 --> 00:05:51,250 Now, if the card item already doesn't match, then we don't want to do anything with that item. 77 00:05:51,250 --> 00:05:53,800 We just return to our array. 78 00:05:54,190 --> 00:05:57,910 And if the amount is bigger than the match, then stock. 79 00:05:58,280 --> 00:06:05,270 And of course, we just set the new amount equal to the max and now the amount properties equal to that 80 00:06:05,270 --> 00:06:11,360 number, and we return all the properties of the item, which just change the amount and the and we 81 00:06:11,360 --> 00:06:14,250 just said cost equal to that time card. 82 00:06:14,270 --> 00:06:16,060 So now we can test it out one more time. 83 00:06:17,260 --> 00:06:21,790 So I'm going to go here, are going to pick the same product so we can just double check. 84 00:06:23,130 --> 00:06:29,610 Let me check my check, of course, I would want products and I would want to have the card provider, 85 00:06:30,000 --> 00:06:33,750 so I have to make this sucker a little bit bigger at the moment. 86 00:06:33,750 --> 00:06:35,480 Is an empty array, correct? 87 00:06:35,910 --> 00:06:37,020 So there's nothing in there. 88 00:06:37,320 --> 00:06:38,520 I'm going to go with this bookshelf. 89 00:06:38,880 --> 00:06:40,320 I'll pick the yellow color. 90 00:06:40,680 --> 00:06:43,620 I'll add the amount of to there is. 91 00:06:43,890 --> 00:06:45,420 Now, we added to the card. 92 00:06:45,420 --> 00:06:47,930 We navigated to the card page. 93 00:06:47,940 --> 00:06:49,590 We're not handling that over here. 94 00:06:49,840 --> 00:06:56,100 And remember, previously, when I added the same product was there was an error in this case. 95 00:06:56,100 --> 00:07:01,230 You will see that once our functionality is in place, I go with you and I add and now, of course, 96 00:07:01,470 --> 00:07:03,960 only the amount value changed. 97 00:07:04,230 --> 00:07:06,590 Everything else stayed the same. 98 00:07:07,740 --> 00:07:13,230 Now I think we can go to the card page and handle what we are displaying.