Great, we can increase the amount of items in a cart, so now let's start working with a decrease,
let's bite the bullet and get it done.
Now, the functionality will be almost exactly the same.
However, we do need to check whether the amount is equal to one.
So if the amount is equal to one, then we will remove the item from the cart.
But initial set up will be exactly the same.
Again, we are returning a new state so that let's copy the old state values for the time being will
have a cost equal to our temporary variable.
So I'm going to go with Unlet and stamp cardigan.
And as far as the value for the time being, I'm going to set this equal to an empty array just because
we are going to be checking if this is going to be undefined, we'll have another.
So let me just overwrite this as a temp card then.
Like I said, we have two options.
Either the amount is equal to or bigger than one or it is in fact equal to one.
And then, of course, we would want to remove that item.
So I'm going to go with if action payload amount is equal to one, then do one thing.
But then if the actual amount is not equal to one, which is going to be the case, of course, if the
amount is bigger than one, then do something else and we will start with that, something else, because
it's going to be a bit more easier because the actual functionality is exactly the same like we already
have here.
The only difference is that instead of increasing the amount, we will decrease.
So if the amount is equal to one was just log, hey, it's one.
And of course I cannot do this.
I need to have my template literals.
So let's write, hey, it's one.
And then if it's not the case, then we're just going to decrease the amount.
So like I said, we have our trump card at the moment.
It is equal to an empty array, but we have already functionality for increasing and decreasing.
It's exactly the same.
Now let me copy and paste.
And the only thing will change is, of course, we're not going to declare a new variable or variable
will stay the same.
So I'm going to go with timecard.
And what I'm saying right now is, yes, at the moment it will be an empty array at the very beginning.
But then if the amount is bigger than one and of course, we are going to assign it to a new array.
Again, we're mapping over the items, were checking the ID if the matches and we just subtract one
instead of add one.
That's all.
That's the only change we would need.
Right.
So let me save it.
And then, of course, I'm going to be increasing.
OK, fine.
Awesome.
And since that is bigger than one, each and every time I'm going to click, I will decrease that amount.
So that is good.
But of course, by the time I'm going to get the one, there should be a console.
OK, so now it is equal to one.
I click on it and now I just deleted the whole card.
Why I deleted the cohort because time code is equal to an empty array.
Correct.
So we had in statement the value was equal to one.
So we just cancel out page one and then instead of going for and else where we actually set our time
card equal to an array with a decreased item, we just left it as an empty array.
And that's why our card right now is empty.
But we have our console log.
Hey, it's one.
So we successfully set up the functionality where we can decrease if the amount is bigger than one.
Now let's fix up our code in our IF statement.
So instead of simple console log, we will do a filter functionality again, just like we had before.
So the same thing again, we just need to check if the amount is equal to one, then remove that item
altogether.
And again, this would be a case where it's much more simpler just to copy and paste.
Now, I'm not a big fan of copying pasting the same time.
Rewriting the same syntax just doesn't make sense.
So let me copy everything from the state like.
So let's scroll up where we have the amount, copy and paste.
And then in this case, we are just going to set our timecard equal to state court.
We filter out and then again, we're calling our callback function.
We have our court remedy.
So each and every item has the duty.
And if the item does not match and as I can see here, I made some kind of bug because it should be
action payload ID.
Let me say it here and also probably, I should say, right here in the bottom as well, because something
must have happened while I was working with my producer.
So again, we just need to go back to action payload ID.
That's the property that we're looking for.
And now in this case, I can just check, OK?
I can increase I can increase all of them, of course.
And then the moment I'm going to go below one, you'll see how I remove the item again, which is decrease
the amount.
And then once we go below one, then we just remove the item from the cart.