Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,180 --> 00:00:05,050
So let's now make the cart work
2
00:00:05,050 --> 00:00:07,680
and let's make sure we can add and delete items
3
00:00:07,680 --> 00:00:09,023
from there as well.
4
00:00:09,860 --> 00:00:12,770
Now, actually we can start with adding items
5
00:00:12,770 --> 00:00:16,440
because we got all the logic for that in place already.
6
00:00:16,440 --> 00:00:19,960
And in the Cart.js file, we've got the cartItemHandler
7
00:00:19,960 --> 00:00:22,570
which is being triggered when this plus button
8
00:00:22,570 --> 00:00:24,970
for a CartItem is clicked.
9
00:00:24,970 --> 00:00:28,810
So in the cartItemHandler, we now simply need to reach out
10
00:00:28,810 --> 00:00:30,980
to our cartCtx and call addItem there
11
00:00:32,500 --> 00:00:34,700
and simply forward the item.
12
00:00:34,700 --> 00:00:38,270
And that will trigger that addItem function
13
00:00:38,270 --> 00:00:40,530
in the CartProvider.
14
00:00:40,530 --> 00:00:44,090
Here it is, which dispatches this CartAction
15
00:00:44,090 --> 00:00:47,200
and that will therefore already do the rest.
16
00:00:47,200 --> 00:00:50,970
So with that, if we save that if we reload,
17
00:00:50,970 --> 00:00:52,540
if I add a couple of items,
18
00:00:52,540 --> 00:00:54,760
if I click the plus next to sushi,
19
00:00:54,760 --> 00:00:57,890
this already updates and the same for the schnitzel.
20
00:00:57,890 --> 00:00:59,810
So this is working.
21
00:00:59,810 --> 00:01:03,140
Now what's of course not working is removing items.
22
00:01:03,140 --> 00:01:05,519
So therefore let's work on this now.
23
00:01:05,519 --> 00:01:08,590
We already got the removeItemFromCartHandler
24
00:01:08,590 --> 00:01:12,020
in the CartProvider where we dispatch an action
25
00:01:12,020 --> 00:01:15,970
but currently we're not really handling that action.
26
00:01:15,970 --> 00:01:20,970
In the cartReducer, we should now add another if check
27
00:01:21,650 --> 00:01:26,650
where we check if the action type is equal to remove
28
00:01:29,410 --> 00:01:33,620
like this and actually we should do that, not here
29
00:01:33,620 --> 00:01:36,550
but next to the add if check.
30
00:01:36,550 --> 00:01:40,580
So here, right before we return our defaultCartState
31
00:01:40,580 --> 00:01:43,963
which is our fallback in case we had some other action.
32
00:01:45,350 --> 00:01:48,560
Now in here we now wanna update the cart
33
00:01:48,560 --> 00:01:51,940
and again we'll have to make a differentiation here.
34
00:01:51,940 --> 00:01:55,920
Generally, we wanna update the existing cart item.
35
00:01:55,920 --> 00:02:00,100
So for example here, if we already have sushi in the cart
36
00:02:00,100 --> 00:02:04,300
and we click minus, we wanna decrease the amount by one
37
00:02:04,300 --> 00:02:07,380
but sushi will still be a cart item
38
00:02:07,380 --> 00:02:10,639
unless we had an amount of one here.
39
00:02:10,639 --> 00:02:14,790
Then if I press minus again if this was one here,
40
00:02:14,790 --> 00:02:18,330
I want to remove the item entirely from the cart.
41
00:02:18,330 --> 00:02:20,570
So we'll have to differentiate
42
00:02:20,570 --> 00:02:23,983
and to check for that here in our reducer.
43
00:02:25,230 --> 00:02:28,340
We can start by finding the cart item
44
00:02:28,340 --> 00:02:30,180
as a first step though.
45
00:02:30,180 --> 00:02:35,180
For this I'll again get my existing CartItemIndex here
46
00:02:35,700 --> 00:02:39,150
by reaching out to the state.items.findIndex.
47
00:02:39,150 --> 00:02:40,963
Same logic as before.
48
00:02:42,190 --> 00:02:46,410
And here just as before we can copy that entire code here
49
00:02:48,450 --> 00:02:49,870
and replace this actually.
50
00:02:49,870 --> 00:02:52,993
We now find the index of an existing item.
51
00:02:54,340 --> 00:02:56,840
Now we can also get to the item itself
52
00:02:56,840 --> 00:02:59,080
by then reaching out to state.items
53
00:02:59,080 --> 00:03:02,093
for that identified index here.
54
00:03:03,210 --> 00:03:06,980
We also wanna update the amount and we can do that first.
55
00:03:06,980 --> 00:03:11,337
The updatedTotalAmount is simply state.item
56
00:03:12,520 --> 00:03:14,280
as state.totalAmount
57
00:03:15,290 --> 00:03:20,290
minus simply existingItem.price.
58
00:03:20,650 --> 00:03:23,910
And I should move this past the existing item of course
59
00:03:24,970 --> 00:03:28,640
because either way if we remove the item entirely
60
00:03:28,640 --> 00:03:31,780
from the array or if we just decreased the amount
61
00:03:31,780 --> 00:03:36,150
in the cart, we simply remove one item of that type.
62
00:03:36,150 --> 00:03:39,520
So the total amount definitely decreases
63
00:03:39,520 --> 00:03:43,250
by the price of one single removed item.
64
00:03:43,250 --> 00:03:46,473
So this line here is always correct.
65
00:03:48,690 --> 00:03:52,860
Now we can again create our updatedItems variable
66
00:03:52,860 --> 00:03:56,430
and then again differentiate and simply check
67
00:03:56,430 --> 00:04:00,730
if for the existing item the amount is equal to one
68
00:04:00,730 --> 00:04:04,180
which means it's the last item of that type
69
00:04:04,180 --> 00:04:06,580
in the cart, which we wanna remove.
70
00:04:06,580 --> 00:04:09,150
In which case we wanna remove the entire item
71
00:04:09,150 --> 00:04:12,760
from the array. Else if it's bigger than one,
72
00:04:12,760 --> 00:04:15,180
we wanna keep the item in the array,
73
00:04:15,180 --> 00:04:17,500
we wanna keep the item in the cart
74
00:04:17,500 --> 00:04:20,209
but we wanna decrease the amount.
75
00:04:20,209 --> 00:04:22,540
So let's start with the if case.
76
00:04:22,540 --> 00:04:27,540
Here, updatedItems is simply state.items.filter.
77
00:04:28,240 --> 00:04:31,940
Filter is a built-in method which returns a new array,
78
00:04:31,940 --> 00:04:35,600
build that by applying specific conditions.
79
00:04:35,600 --> 00:04:37,500
And we pass a function to filter
80
00:04:37,500 --> 00:04:40,320
which is executed for every item in the array.
81
00:04:40,320 --> 00:04:42,620
And that function receives the item.
82
00:04:42,620 --> 00:04:44,520
And if it returned true here,
83
00:04:44,520 --> 00:04:47,670
we keep the item in the newly returned array.
84
00:04:47,670 --> 00:04:50,313
If it returned false, we get rid of it.
85
00:04:51,440 --> 00:04:54,570
So here we wanna keep all items
86
00:04:54,570 --> 00:04:57,620
but we wanna remove the item with that id
87
00:04:57,620 --> 00:04:59,790
which we get on our action here.
88
00:04:59,790 --> 00:05:01,600
That's the item we wanna get rid of.
89
00:05:01,600 --> 00:05:05,160
And by the way here, we should just check action id,
90
00:05:05,160 --> 00:05:08,930
not action item id because for removing items
91
00:05:08,930 --> 00:05:10,650
we are dispatching an object
92
00:05:10,650 --> 00:05:13,673
which just has the id, not the entire item.
93
00:05:14,600 --> 00:05:17,890
So that's a little fixed. We should implement anyways.
94
00:05:17,890 --> 00:05:20,660
But now back to filter, here we wanna check
95
00:05:20,660 --> 00:05:24,633
if item id is not equal to action.id.
96
00:05:26,460 --> 00:05:29,860
With this check we make sure that all items
97
00:05:29,860 --> 00:05:34,350
where the id is not equal to the action id are kept
98
00:05:34,350 --> 00:05:37,790
because this returns true and hence the items are kept.
99
00:05:37,790 --> 00:05:41,200
But the one item where the item id is equal
100
00:05:41,200 --> 00:05:45,300
to the id of the action, which is the to-be-removed id
101
00:05:45,300 --> 00:05:48,920
for that one item we return false here.
102
00:05:48,920 --> 00:05:53,920
And then we remove that item from the newly generated array.
103
00:05:53,940 --> 00:05:56,700
So that gives us a new array where this item
104
00:05:56,700 --> 00:06:00,280
with id is not part anymore.
105
00:06:00,280 --> 00:06:01,810
And that's the entire logic
106
00:06:01,810 --> 00:06:04,223
for this case that we had an amount of one.
107
00:06:05,340 --> 00:06:08,300
Now if the amount is greater than one,
108
00:06:08,300 --> 00:06:10,990
we don't wanna remove the item from the array.
109
00:06:10,990 --> 00:06:13,510
We just wanna update the amount.
110
00:06:13,510 --> 00:06:15,960
So here we have the updated item
111
00:06:15,960 --> 00:06:18,380
which is a copy of the existing item
112
00:06:18,380 --> 00:06:21,620
in a new object with the spread operator.
113
00:06:21,620 --> 00:06:23,670
And we just update the amount.
114
00:06:23,670 --> 00:06:28,120
We set the amount to existingItem.amount minus one
115
00:06:28,120 --> 00:06:29,810
to decrease it by one
116
00:06:31,400 --> 00:06:36,400
and updatedItems is then a copy of state.items
117
00:06:36,690 --> 00:06:40,160
to create a new array with the old items.
118
00:06:40,160 --> 00:06:42,560
But we override one of these items.
119
00:06:42,560 --> 00:06:46,900
The item for that index which we got here
120
00:06:46,900 --> 00:06:49,960
where we then override the old item in the array
121
00:06:49,960 --> 00:06:53,203
with the updated item which has the updated amount.
122
00:06:54,810 --> 00:06:57,730
And then here after this if else blank,
123
00:06:57,730 --> 00:07:00,900
we simply return a new state object
124
00:07:00,900 --> 00:07:05,770
where items is updatedItems, add the totalAmount is equal
125
00:07:05,770 --> 00:07:07,727
to the updatedTotalAmount.
126
00:07:09,410 --> 00:07:13,223
And that's our logic for removing items from the cart.
127
00:07:14,220 --> 00:07:16,970
Now we just need to wire up this button
128
00:07:16,970 --> 00:07:21,970
in the cart component here in the CartItemRemoveHandler.
129
00:07:22,070 --> 00:07:25,128
We now wanna call cartCtx,
130
00:07:25,128 --> 00:07:27,723
removeItem and forward that id.
131
00:07:29,790 --> 00:07:32,243
And if we do that and save everything,
132
00:07:33,240 --> 00:07:36,750
if I reload and I add a couple of items.
133
00:07:36,750 --> 00:07:39,400
I have one of all these items in there.
134
00:07:39,400 --> 00:07:43,730
If I now remove sushi, it's removed entirely from the array.
135
00:07:43,730 --> 00:07:47,570
If I add one to the schnitzel and I then remove that,
136
00:07:47,570 --> 00:07:49,710
the amount has just decreased
137
00:07:49,710 --> 00:07:52,823
and only if I remove that again, it's removed from the cart.
138
00:07:53,750 --> 00:07:56,530
So now it looks like adding and removing to
139
00:07:56,530 --> 00:08:00,160
and from the cart is working correctly.
140
00:08:00,160 --> 00:08:03,293
And that's exactly, of course what we want here.
141
00:08:04,640 --> 00:08:06,520
Now with that we're almost done
142
00:08:06,520 --> 00:08:10,420
but I wanna play a little animation on the cart button now,
143
00:08:10,420 --> 00:08:13,210
whenever a new item is added to a highlight
144
00:08:13,210 --> 00:08:16,703
that this badge changed and that the cart overall changed.
11662
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.