Excellent work on the next hour, I want to set up the functionality where every time we increase or
decrease or we remove the item from the cart or clear the cart, we update the values as well as far
as the amount and the total.
And before we set up the functionality, let me just quickly mention that we will use reduced function.
And if you're not familiar with the reduced again, go back to Joske Nugget's playlist that I have where
we have introduction for the reduced basics.
And I'm also planning adding a nugget's video where I cover the functionality that we're going to cover
in this video in greater detail.
So if you get stuck with Rogers, just go to Joske Nuggets playlist and you'll be able to find multiple
videos.
And this one is going to be the basic one.
And then there's also going to be some videos that cover more advanced topics.
And then one of them will be the functionality that we're about to do.
That is just a side note, which we need to navigate back to our project.
And then first, let's set up the function.
Now, in this case, though, I really don't need to even set it up as a separate function in the context.
Instead, what I would want is to set up a user where every time the value in my state, more specifically
in the cart, in my state changes than I would want to call that effect.
So let's start with that.
We have used the fact we have a callback function when we have dependency.
Right.
And then in here, I'll pass in state and then I just start very simply by logging.
Now, once I save, every time we do something with our cart, we should have the Halloween console.
So there is an increasing amount.
Of course, it changes a decrease.
I remove the item and also clear cut.
So every time something happens with the card, of course we run our usufruct.
So what I'm looking for here is despatching and action and I'll call this action get totals again.
We don't really need to set up a separate function.
So we go with type on line.
We'll look for get.
Underscore totals we save, OK, beautiful, now we have our effect and of course, we would need to
navigate to the producer and deal with that action.
So again, of course I'll set up my if so, right after decrease, I'm going to go with if I'm behind,
we're going to go with action.
That type is equal to get on the score totals.
Then of course we would want to do something.
And yes, we will start by setting up separate variables.
However, it is going to be a bit different since we have two of them.
We have total and then we have the amount.
So for now.
I want to return, of course, I'm going to go with DataDot and the state, since I wouldn't want to
mess with other values, but I would want to change the values for the total property as well as the
property.
Keep that in mind.
Those are the same properties that at the moment I have hardcoded and then I will be setting up the
orders.
Now, I won't set up just symbologist.
I will set up a produce that returns a object.
So it returns an object with these two values, and that's why I'll be able to structure it right away.
So I'm going to go online.
I will set up the names exactly the same, so say total and then amount, and that one is equal to state.
So my current state, then the correct array online would go with radius and then of course we pass
in the Kobuk auction as well as well.
What is the value that we are returning?
So that is my callback function.
Then we have a comma and then what am I returning?
Well, I would want to return an object.
That's why I go with object and then to properties total, and I won't be equal to zero, and then of
course I'm also looking for the amount.
So as I'm looking at it, of course, it doesn't change much from what I have right now.
I have total an amount zero and in register pretty much the same thing.
I have my register function, it has the callback function.
I'm returning a object with two properties, total amount, and I write away the structure and then
I return the state.
So a copy of the values and I'll be overwriting total animal.
So of course that one will be equal to the property.
That's three as well as here.
OK, hopefully that is clear.
Of course we have the error because we haven't set up our two arguments and those two arguments are
court total.
And then the second argument, I'm going to call cart and then item.
So cart item represents each and every item that we're carrying over and cart total is what we returning.
Of course, in this case that as an object with these two properties and then inside my function body,
I would want to start by getting two values out of the current item.
Now, what values am I looking for?
I am looking for.
Let me just double check with the data.
I'm looking for price and I'm looking for amount.
Why?
Well, because when I'm setting up the amount and of course I'll just be looking for the amount property
value.
But then when I'm looking for a total, I would want to multiply that.
So since I have multiple items and then they have different amounts, I would need to multiply that
by the price.
So let me navigate back to my producer and then I'm looking for these two properties.
Each and every item has them.
I'm looking for price and none of the amount I want is coming from my cart and an item.
Let me close the sidebar here.
And then as far as return, let's just start simply by returning court total.
So let's go to ten.
And then Kartal and now once we save, we shouldn't get errors now if you want, you can definitely
control these properties if that helps, you know the price and that amount.
And you'll see that of course we have the price.
So five hundred ninety nine online the amount is one.
I notice how for each item we have a different price, but the amount stays the same.
So what is the first thing that I would want.
Well, I would want to first get the amount of items that I have total in the car, so I have my total
amount.
So this is the amount for all the items in the car.
So this is the sum of all the amount property values in my items.
So each item has the amount property and of course it is some kind of value.
So now, of course, out of return, the total of all those properties and the way we do that.
We simply say, you know what total is an object on an object, we have the amount property and that
every time I'm iterating over, I would just want to add the amount now also say plus equals and then
the amount.
And what that simply means is whatever is the value, just add the amount property value.
And now you'll notice that the amount will also change nerdish.
Now, of course, it is three, right?
Why?
Well, because we write.
We run the usufruct.
Correct.
And then it is counting how many items we have in a car.
And then more specifically, for every item we have that amount property.
So as I'm increasing notice how the amount is also increasing.
Remember, no, right away the structure, the amount property coming from the state.
So there's two amount properties.
So there's one that is specific for every item and then there's one in the state that just sums up all
these factors.
So that was amount that was a bit more straightforward because we simply needed to add the value that
is coming from each item and we sum them up.
And as far as the total.
So how much is the total of the cost?
Of course, in this case, we need to set up a separate variable because I would want to multiply the
amount by price.
So how many pieces of that item I have in the cart and then multiply by the price of each piece?
So we're going to go here with CONSED and that item total amount is equal to a price multiplied by the
amount.
And of course now I would want to add this total to the cart, total total property.
So the same how we worked with the amount I'll do with a total.
So let's go to the next line and I'll say kaat total total property plus equals plus equals.
And then of course we're adding this item total value.
So now one Gotcher is going to be that once in a while we might get some weird buyers.
So as you can see, everything works nicely.
So we can increase.
We can decrease, but not once in a while.
We get these weird values and essentially avoid that.
We would just need to make sure that we have only a set amount of numbers after the dot.
And in order to do that, I'll change this around.
Also, let me show now, of course, I'm still getting these two properties, but I would want to do
a little bit more work on total.
So I'm going to say here, total is equal to a partial float like so.
And I would want to say total and then two fixed.
So this is going to limit the amount of numbers after that, but it does return a string.
So I just need to pass it through the parcel.
And now you'll notice that still functionality works, but you won't get these weird marks again, iterating
over the array.
We have the car total and then car item.
As far as the car total, we return an object where we have two properties total on the amount.
So of course that is the total of my card and the amount of items I have in the car.
However, as we iterating, each item also has the amount and price property.
And then as far as the amount, I just sum up these values.
What gives me that total amount?
And then as far as total?
Well, since I would want to count the pieces times the price.
So of course that is going to give me the total.
That's why I set up the variable as a price times the amount.
So amount of items in the cost of that specific item multiplied by price.
And then just add to the total that is coming from the state.
Hopefully it is clear and of course we can nicely increase, decrease and more.
And every time we do something with the cart, of course we call our user factor in the user we call
get totals.
So we dispatch the type of gutturals and we take care of that in the user.