Our application is actually complete, but I'm fully aware of that.
There's definitely some people who are very, very upset about the fact that they're somewhat repeating
functionality.
So now I would like to refactor it and set it up as one function.
Again, if you're not interested in that, just move on to the next video and also if you'd like to
set it up yourself.
So if you would want to have a challenge, definitely do so.
In my case, I will refactor this into one function.
So if you want to stop the video, try to do it yourself and then reserve it.
That is definitely a good, good option.
Now I've got two functions and then I would want to set up as one.
So first, I guess I would need to go to the context and I would need to come up with a function here
first.
And I'm going to do that after first data since.
Again, that is something that we're doing as an extra and I'm going to say const toggle and amount.
So that is how I'm going to call my function now in this function.
I'm going to be looking for two parameters.
I'm going to be looking for the entity and then I'm going to be looking at, well, what am I doing?
Am I increasing or am I decreasing?
And then I'm going to say here type.
So those are my two parameters.
And now, of course, I would want to set up my register.
So say here, dispatch, and we're going to go with type.
I will call this toggle as well.
So essentially it's going to be toggle and then underscore amount.
And now, of course, in the payload, I'll need two things.
So instead of setting up two separate properties, I'm going to go with Kharma and the object.
So that is my payload object.
So I'll set it equal, of course, to my payload.
So so that is my object.
And then in here I'll pass and these two things apart in the area and then type.
So whether I would want to increase or decrease.
So once I've got this in place, then of course I need to navigate here and then we'll say toggle AMOLED,
not the function we're passing on, but of course we navigate to the current item line and here we need
to structure it first.
So toggle and amount and of course where we have the increase and decrease.
Now I would want to change this around.
So I have my two cursors are old enough, both of them, and I'll say Tomago an amount.
And now remember, the function is looking for two things.
It is looking for the very first and then the type.
Now, the idea I'm already passing is first.
And then, of course, for the increase, I'll just say ink and learn for decrease.
I want to say the easy.
OK, beautiful.
And then now we need to navigate to the register.
And then again I'll do that right after display items because it is an extra in my opinion.
And I'll say if action that type is equal to Torgau and underscore amount, I'll double check so I don't
get some weird marks.
Yep.
Targo amount.
Then of course I would want to handle that and I'll start by setting up again.
A new variable will say a lot and then stamp cart that will be equal to my empty array for the time
being.
And then of course I would want to return.
So we're going to go with the return to that state and then we'll change the cart around where it's
going to be equal to my temper.
Now, as far as the functionality for timecard.
I will set up the map, however, in this case, I'll check whether we're increasing or decreasing,
and if that is the case, then of course, I'll also change the values.
So we're going to go with state then part of the map.
So we're mapping over and I'll still call this court item as item.
And that in here, I'll say first, if the I.D. matches, then I would want to do something.
So say court item, Heidi, if it matches action, then dot and then payload and then ID, well, then
I would want to return essentially new item if it doesn't match.
And of course we're doing the same thing where we just return the crime.
And as far as the changes we would want to do, we would need to set up two more if statements.
So this is the first case where the ID matches the action payload and then since payload is an object
and then I'm looking for that property, if that is the case, well, then I would want to check what
is the type.
So I go with action, then payload and then type.
And I say if it is increasing, then I would want to increase amount by one.
So I'll say I can't say or if it is decreasing then I would want to return something else.
So I'm going to say again if action and then dot and of course I'm looking for payload and then type
is equal to a decreasing one, then of course worked on something else in the first case will return
everything that currently item has so that that dot and then I'll call this court item and you can probably
already guess that we're just going to go cart item and that amount plus one, and that if we are decreasing
then of course we'll have minus one like so and the last thing that I would want is where I setting
up the time card.
I also would want to filter it.
Remember, this is something that we're doing when we're decreasing and in order to speed this up,
just going to grab the filter, navigate down.
I'm looking for my timecard, I'll pass it on the dot.
And then every time the value will be below zero, of course, he is going to remove that item from
the card.
So now of course, I can increase and decrease my function.
I and I can also remove the item from the card.
And for those of you who are upset that we had two somewhat similar functionalities and of course we
have only one.
And I guess the last thing that I would want, instead of returning the state, I'm just going to throw
the error if the action type does not match.
And the way we do that, we simply say throw new error, throw you are and let's just say here, no
matching action type.
So if we'll try to dispatch some kind of action that doesn't exist, we're going to get there.
Now, the fastest thing to check that notice where we're dispatching it totals.
Let me dispatch something that I'm not handling some research type and I'm going to say, I don't know,
random, random over here.
So the moment I run this, of course, there's going to be an error that says no matching action type,
OK, beautiful and remote.
I was just for demonstration purposes.
And if I navigate to the big screen, of course, our functionality works.
That was our application.
Hopefully everyone enjoy and I hope to see the next one.