But we can add new product to the cart, but handle the cash if the item is our cart and we're going
to do that by mapping over the cart, checking the I.D., if are matches, then of course, we'll set
up a bit more functionality.
And I'm going to start by creating a new era.
So time card.
And this is the case where it will iterate over our current cart in the state.
Now, as far as the return, all right away set up my return where I'm going to say that I'm going to
return all state values, of course, down to the state and then cart will be equal to my trump card.
So the array that I'm about to set up and in here, we're going to go with state court maps.
So we're mapping over the court.
And then each item I'm going to represent as a court item.
And then as far as the return, well.
As a mapping over, I have two cases, I have a case where, of course, the idea is going to match
to whatever I'm grabbing here, so whatever is coming from a single product.
Just keep in mind, though, that we need to check for both of them.
We need to check for the ID plus color, because that is the cart item.
I'm using the ID in the product, but in the cart the ID is actually ID plus color.
And if that is the case, then of course I'll have to change the amount.
Correct.
Because keep in mind, if I take a look at the complete application and if I remove the sticker, that
utopia so far in the product, let me find it.
Probably I won't be able to do it.
Meaning it's just going to take me a while to find that utopia.
So far, that's how it usually works when you want to showcase something.
And there is now I have Utopia, so I'll use the same color.
So it works exactly the same.
I'm going to add one item over here.
So now of course I have this item and then I would want to go back to continue shopping.
And again, I'll look for the same item, same color.
And now I want to add two more items.
So this is the functionality that we're about to set up where if the item is already in a cart, I just
want to increase that amount.
That's all I have to do.
If the item is not in the card, then of course, we're adding a new item, which, by the way, where
it did in the previous video ligatures, that the same car item can have different colors.
That's why the idea is actually equal to the ID plus the color.
So in here, I would say if the ID matches, then there's going to be one functionality.
If the ID doesn't match one, then I simply want to return that Cottenham.
I don't want to do anything.
I simply say return card item as it is, since I only want to update amount if the card item is already
there.
Correct.
If it's not in the card, well, it doesn't really matter.
Now the gotcher here is that of course I'm checking first for the item if it is and then I'm double
checking one more time since I'm increasing the amount.
So I'm checking if the item is in the card.
Beautiful.
And then if it is in the card, then of course I'll update this amount value.
So say if the item ID matches and we say that by card item that ID is equal to ID plus color, then
I'm going to create a new amount property.
I'll say let me know and we're going with amount and that one is equal to cart item, whatever is the
current one plus the amount that I'm passing in.
All right.
But the criteria is that we always need to check with our stock.
Correct.
And where we were setting up the stock, it was in the max property.
So right after I set up my new amount, I actually want to check if the value is bigger than the max,
that the amount will be equal to the max.
So say another if statement if new amount is bigger than cart item max.
So the property that is equal to the stock, then the new amount will be equal to a cart item.
Max, essentially we're saying, yeah, you might have five items the first time, five items the second
time.
But if my stock market is just six, then there's going to be six items in court.
And then this is going to be the case where I would want to return all the properties of the item.
So in both cases, I'm returning the car item, or in this case, of course, I'm overriding those values.
I say that amount is equal to a new amount like so.
And now, of course, we can save and let's just go over the functionality one more time.
So these are the things that we're passing in from and cut adequate amount on product.
If the item is not in the cart, we create a new one with these properties.
Can we keep in mind that I.
D plus color is equal to the court item right now?
If the item is already in the cart, then of course we iterate over the card check where the item is.
So we find where is the item?
And if the ID is equal to or added plus color, then of course we know that that is the current item
and then we just need to increase the amount.
Now, if the card item already doesn't match, then we don't want to do anything with that item.
We just return to our array.
And if the amount is bigger than the match, then stock.
And of course, we just set the new amount equal to the max and now the amount properties equal to that
number, and we return all the properties of the item, which just change the amount and the and we
just said cost equal to that time card.
So now we can test it out one more time.
So I'm going to go here, are going to pick the same product so we can just double check.
Let me check my check, of course, I would want products and I would want to have the card provider,
so I have to make this sucker a little bit bigger at the moment.
Is an empty array, correct?
So there's nothing in there.
I'm going to go with this bookshelf.
I'll pick the yellow color.
I'll add the amount of to there is.
Now, we added to the card.
We navigated to the card page.
We're not handling that over here.
And remember, previously, when I added the same product was there was an error in this case.
You will see that once our functionality is in place, I go with you and I add and now, of course,
only the amount value changed.
Everything else stayed the same.
Now I think we can go to the card page and handle what we are displaying.