Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,060 --> 00:00:03,710
As always in programming,
2
00:00:03,710 --> 00:00:06,770
there will be multiple ways of fixing that issue
3
00:00:06,770 --> 00:00:08,640
we detected in the last lecture,
4
00:00:08,640 --> 00:00:10,940
where we automatically resent the cart,
5
00:00:10,940 --> 00:00:13,121
when the application loaded.
6
00:00:13,121 --> 00:00:15,680
One possible solution, could be to go to
7
00:00:15,680 --> 00:00:20,680
our cart-slice and here in that initial state,
8
00:00:22,034 --> 00:00:26,120
we, for example, add a changed property,
9
00:00:26,120 --> 00:00:27,643
which is false, let's say.
10
00:00:28,950 --> 00:00:33,050
And we don't change this if we replaced a cart,
11
00:00:33,050 --> 00:00:36,110
but we do change it if we add or remove items,
12
00:00:36,110 --> 00:00:37,870
to or from the cart.
13
00:00:37,870 --> 00:00:40,713
Then we set state.changed to true.
14
00:00:42,519 --> 00:00:46,150
And removeItemFromcart and addItemToCart,
15
00:00:46,150 --> 00:00:50,160
are only executed from our local application.
16
00:00:50,160 --> 00:00:52,550
So, when we fetch data from Firebase,
17
00:00:52,550 --> 00:00:55,427
where we then execute replaceCart,
18
00:00:55,427 --> 00:00:56,260
this will not change.
19
00:00:56,260 --> 00:01:00,900
It will stay false. Now, how can this help us?
20
00:01:00,900 --> 00:01:04,269
Well in App.js, where we have this effect
21
00:01:04,269 --> 00:01:07,370
for sending CartData, we can also check
22
00:01:07,370 --> 00:01:10,110
if cart changed.
23
00:01:10,110 --> 00:01:12,310
So if our cart, which we're selecting here,
24
00:01:12,310 --> 00:01:15,630
if that changed property, is true
25
00:01:15,630 --> 00:01:18,700
and only if it's true, we wanna dispatch
26
00:01:18,700 --> 00:01:19,900
and send the CartData.
27
00:01:19,900 --> 00:01:22,730
If it's false, so if it hasn't changed locally,
28
00:01:22,730 --> 00:01:24,053
we don't wanna send it.
29
00:01:25,230 --> 00:01:27,030
With this simple change implemented,
30
00:01:27,030 --> 00:01:29,630
if we reload, we're not sending this again.
31
00:01:29,630 --> 00:01:32,177
Only if I add something to the cart.
32
00:01:32,177 --> 00:01:34,930
Now, as a side note, this changed property
33
00:01:34,930 --> 00:01:37,150
is now all the part of Firebase,
34
00:01:37,150 --> 00:01:39,850
because we're sending the overall cart state,
35
00:01:39,850 --> 00:01:42,370
as it's stored by Redux to Firebase.
36
00:01:42,370 --> 00:01:44,690
If we would wanna avoid this, we could of course,
37
00:01:44,690 --> 00:01:46,900
go to the cart-actions and there,
38
00:01:46,900 --> 00:01:49,600
where we send our cart data, instead of taking
39
00:01:49,600 --> 00:01:51,270
the whole cart, we could create
40
00:01:51,270 --> 00:01:53,910
a new objects, where we then just use
41
00:01:53,910 --> 00:01:56,570
items from cart items and just
42
00:01:56,570 --> 00:02:00,933
the totalQuantity from cart.totalQuantity.
43
00:02:03,170 --> 00:02:04,910
So we would create a new object,
44
00:02:04,910 --> 00:02:07,100
which does not contain changed.
45
00:02:07,100 --> 00:02:09,350
That's something we could do.
46
00:02:09,350 --> 00:02:13,943
And with that, it is removed, if we send something.
47
00:02:14,950 --> 00:02:16,973
Now it's no longer part of Firebase.
48
00:02:17,890 --> 00:02:20,480
So now, that should be working as we want.
49
00:02:20,480 --> 00:02:23,430
Now we can add items to the cart,
50
00:02:23,430 --> 00:02:25,970
change our cart, however we need to,
51
00:02:25,970 --> 00:02:27,650
add more and less.
52
00:02:27,650 --> 00:02:30,418
And that is then synced to Firebase.
53
00:02:30,418 --> 00:02:32,610
Though I see that the price here
54
00:02:32,610 --> 00:02:33,550
doesn't update correctly.
55
00:02:33,550 --> 00:02:35,000
We should look into this.
56
00:02:35,000 --> 00:02:37,121
But that's now synced to Firebase.
57
00:02:37,121 --> 00:02:39,500
And they offer this now works.
58
00:02:39,500 --> 00:02:41,800
At least when it comes to the side effects
59
00:02:41,800 --> 00:02:45,210
and to HTTP requests, we are of course
60
00:02:45,210 --> 00:02:47,690
also loading data correctly.
61
00:02:47,690 --> 00:02:49,400
I just detected that we have a issue
62
00:02:49,400 --> 00:02:51,520
regarding the price, that does not update correctly
63
00:02:51,520 --> 00:02:54,960
if I reduce my cart quantity.
64
00:02:54,960 --> 00:02:56,850
So let's quickly look into that here,
65
00:02:56,850 --> 00:03:00,570
in cart-slice and removeItemFromCart.
66
00:03:00,570 --> 00:03:03,250
Yeah, for existing items, I am reducing the quantity,
67
00:03:03,250 --> 00:03:05,180
but I'm not updating the price.
68
00:03:05,180 --> 00:03:06,621
We should of course be doing that.
69
00:03:06,621 --> 00:03:11,460
I'll set totalPrice equal to existingItem.totalPrice,
70
00:03:11,460 --> 00:03:14,260
not totalQuantity, thank you, totalPrice,
71
00:03:14,260 --> 00:03:18,170
minus existingItem.price.
72
00:03:18,170 --> 00:03:21,620
To reduce the total price, by the price
73
00:03:21,620 --> 00:03:23,300
of a single item, since we're removing
74
00:03:23,300 --> 00:03:24,700
a single item.
75
00:03:24,700 --> 00:03:27,530
If we do that and I reload
76
00:03:27,530 --> 00:03:30,940
and I clear my cart, to start from scratch again,
77
00:03:30,940 --> 00:03:33,930
if you do that and I try to start editing again,
78
00:03:33,930 --> 00:03:36,160
we get another error and that actually
79
00:03:36,160 --> 00:03:39,220
is an expected error, which I wanted to dive in.
80
00:03:39,220 --> 00:03:40,690
We're getting this error because
81
00:03:40,690 --> 00:03:43,520
I cleared my cart entirely and I reloaded
82
00:03:43,520 --> 00:03:47,020
and therefore I fetched my cart data from Firebase.
83
00:03:47,020 --> 00:03:49,660
Since I cleared the cart entirely though,
84
00:03:49,660 --> 00:03:51,576
we see that on Firebase here,
85
00:03:51,576 --> 00:03:55,170
we have no items key in the cart anymore.
86
00:03:55,170 --> 00:03:58,040
So we fetched that data from Firebase
87
00:03:58,040 --> 00:04:00,960
and we set our local carts to the fetched cart.
88
00:04:00,960 --> 00:04:04,270
And that means that items is now not an empty array,
89
00:04:04,270 --> 00:04:06,370
but undefined and therefore trying
90
00:04:06,370 --> 00:04:09,790
to call find on undefined, fails.
91
00:04:09,790 --> 00:04:12,317
To solve this, we should go to cart-actions
92
00:04:12,317 --> 00:04:14,843
and to the place where we fetch our cart.
93
00:04:14,843 --> 00:04:18,990
And then, when we replace our cart with cartData,
94
00:04:18,990 --> 00:04:22,240
there indeed is a tiny transformation we should make.
95
00:04:22,240 --> 00:04:24,890
We should make sure, that the payload we pass
96
00:04:24,890 --> 00:04:28,570
to replaceCart, is a object which always has
97
00:04:28,570 --> 00:04:32,870
a items key, which is either cartsData.items,
98
00:04:32,870 --> 00:04:34,833
or if that should be undefined and therefore
99
00:04:34,833 --> 00:04:37,797
a falsy and empty array.
100
00:04:37,797 --> 00:04:42,797
And total quantity is always cartData.totalQuantity,
101
00:04:43,630 --> 00:04:47,008
because that still is part of Firebase.
102
00:04:47,008 --> 00:04:49,505
With that, we ensure that we never end up
103
00:04:49,505 --> 00:04:51,660
with items being undefined.
104
00:04:51,660 --> 00:04:54,470
Instead, it will always be an empty array.
105
00:04:54,470 --> 00:04:57,315
With that, if we reload, we can add to the cart again
106
00:04:57,315 --> 00:05:00,940
and now we can also deduct items from the cart.
107
00:05:00,940 --> 00:05:03,732
And now the price updates correctly.
108
00:05:03,732 --> 00:05:05,270
Can reload again.
109
00:05:05,270 --> 00:05:07,130
And this now all works.
110
00:05:07,130 --> 00:05:09,590
And now we also implemented HTTP,
111
00:05:09,590 --> 00:05:11,990
in side effects with Redux.
112
00:05:11,990 --> 00:05:13,930
And you learned about different approaches
113
00:05:13,930 --> 00:05:14,833
of doing that.
8593
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.