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:05,010
Now I got another starting project for you
2
00:00:05,010 --> 00:00:08,540
to which we're now going to add Redux and some logic.
3
00:00:08,540 --> 00:00:10,440
And that will then be the project
4
00:00:10,440 --> 00:00:12,200
which will be used in the next lecture
5
00:00:12,200 --> 00:00:16,059
to add some side effects and some asynchronous code.
6
00:00:16,059 --> 00:00:19,270
But before we do that, we need to add Redux.
7
00:00:19,270 --> 00:00:21,510
Because the starting project I give you
8
00:00:21,510 --> 00:00:23,650
has a couple of React components.
9
00:00:23,650 --> 00:00:28,600
And if you run NPM install to install all dependencies
10
00:00:28,600 --> 00:00:32,759
and there after NPM start to start the development server,
11
00:00:32,759 --> 00:00:37,760
you will see this application here, this dummy application
12
00:00:38,200 --> 00:00:40,160
which is not interactive.
13
00:00:40,160 --> 00:00:42,400
Clicking those buttons won't do anything.
14
00:00:42,400 --> 00:00:44,410
This is just dummy data.
15
00:00:44,410 --> 00:00:46,870
Now in this lecture, we are going to make sure
16
00:00:46,870 --> 00:00:49,310
that those buttons do something.
17
00:00:49,310 --> 00:00:50,510
And to be precise,
18
00:00:50,510 --> 00:00:53,790
I wanna ensure that if we click the My Cart button,
19
00:00:53,790 --> 00:00:56,810
we toggle this cart, so we show it
20
00:00:56,810 --> 00:00:59,830
And if it is already showing up, we hide it.
21
00:00:59,830 --> 00:01:03,180
I wanna make sure that if we click Add to Cart on a product,
22
00:01:03,180 --> 00:01:05,250
we do add it to the cart.
23
00:01:05,250 --> 00:01:07,350
And if it's already part of the cart,
24
00:01:07,350 --> 00:01:10,940
we just increase the quantity of the existing item.
25
00:01:10,940 --> 00:01:13,710
You can also add more dummy products for that,
26
00:01:13,710 --> 00:01:16,750
so that you have more products to choose from.
27
00:01:16,750 --> 00:01:21,250
And in the cart, with these buttons Plus and Minus,
28
00:01:21,250 --> 00:01:24,140
we also want to control the quantity.
29
00:01:24,140 --> 00:01:27,120
And if the quantity is one and we click Minus,
30
00:01:27,120 --> 00:01:30,430
we remove the item entirely from the cart.
31
00:01:30,430 --> 00:01:33,490
Now that's the logic we're going to add in this lecture.
32
00:01:33,490 --> 00:01:35,680
And as I said in the last lecture,
33
00:01:35,680 --> 00:01:39,040
this all is a great practice for you.
34
00:01:39,040 --> 00:01:42,470
Try implementing all those features which I described
35
00:01:42,470 --> 00:01:47,330
including that toggle mode for the cart with Redux,
36
00:01:47,330 --> 00:01:50,250
potentially with multiple state slices if you want.
37
00:01:50,250 --> 00:01:53,140
Here is a pause for you to pause the video
38
00:01:53,140 --> 00:01:54,810
and try it on your own.
39
00:01:54,810 --> 00:01:57,470
There after we're going to implement it together
40
00:01:57,470 --> 00:02:00,113
step by step practicing what we learned.
41
00:02:02,920 --> 00:02:04,440
Were you successful?
42
00:02:04,440 --> 00:02:06,500
Let's implement this together.
43
00:02:06,500 --> 00:02:10,070
And for this, let's set up Redux in this application.
44
00:02:10,070 --> 00:02:12,910
Now for this I'll first of all quit the Def server
45
00:02:12,910 --> 00:02:15,673
and then install @reduxjstoolkit
46
00:02:16,730 --> 00:02:18,600
to install the Redux Toolkit
47
00:02:18,600 --> 00:02:20,830
because I still wanna use that.
48
00:02:20,830 --> 00:02:25,210
You could also use just Redux and just React Redux
49
00:02:25,210 --> 00:02:28,320
but working with Redux Toolkit, simply makes working
50
00:02:28,320 --> 00:02:30,350
with Redux much easier.
51
00:02:30,350 --> 00:02:32,000
Now, one thing I forgot of course
52
00:02:32,000 --> 00:02:35,210
is that we still need to install, React Redux though.
53
00:02:35,210 --> 00:02:37,960
So you should always install React Redux
54
00:02:37,960 --> 00:02:39,683
in addition to the toolkit.
55
00:02:40,950 --> 00:02:44,120
Now, once both is installed, we can start setting
56
00:02:44,120 --> 00:02:48,860
up our Redux logic and connecting it to the React app.
57
00:02:48,860 --> 00:02:52,890
For this in the source folder, I'll add a store folder.
58
00:02:52,890 --> 00:02:54,930
Again, it doesn't have to be named store
59
00:02:54,930 --> 00:02:56,960
but it is a common convention.
60
00:02:56,960 --> 00:03:01,320
And in that store folder, I wanna add my index JS file
61
00:03:01,320 --> 00:03:05,940
which will set up my Redux store and so on.
62
00:03:05,940 --> 00:03:10,290
And I also wanna create multiple slices, one slice
63
00:03:10,290 --> 00:03:14,730
for managing the cart and one slice for user interface
64
00:03:14,730 --> 00:03:17,150
logic like toggling the cart,
65
00:03:17,150 --> 00:03:19,520
which should go into its own slice here.
66
00:03:19,520 --> 00:03:22,170
And therefore I will create two files here,
67
00:03:22,170 --> 00:03:27,170
a UI Slice JS file and then also mycart-slicejs file.
68
00:03:30,510 --> 00:03:33,250
That's not a must do, you could put it all
69
00:03:33,250 --> 00:03:35,660
into one code file, but splitting it up
70
00:03:35,660 --> 00:03:39,400
ensures that all the code stays maintainable and manageable
71
00:03:39,400 --> 00:03:43,550
and we don't end up with super large code files.
72
00:03:43,550 --> 00:03:47,030
Now let's maybe get started with the UI Slice,
73
00:03:47,030 --> 00:03:48,990
so that when we click My Cart,
74
00:03:48,990 --> 00:03:53,500
we do toggle that cart area here.
75
00:03:53,500 --> 00:03:56,280
For this in the UI Slice JS file,
76
00:03:56,280 --> 00:04:01,280
I'll import something from @reduxjs/toolkit.
77
00:04:01,340 --> 00:04:03,000
And that something as you learned,
78
00:04:03,000 --> 00:04:05,090
is to create slice function
79
00:04:05,090 --> 00:04:07,350
which does what the name implies.
80
00:04:07,350 --> 00:04:11,233
When we call it, it creates a slice and you'll learned
81
00:04:11,233 --> 00:04:14,440
that it needs to object for a configuration
82
00:04:14,440 --> 00:04:17,870
where we give this slice a unique name
83
00:04:17,870 --> 00:04:20,690
and here I'll choose UI as a name,
84
00:04:20,690 --> 00:04:22,490
but of course the name is up to you.
85
00:04:23,440 --> 00:04:26,750
Then in addition we wanna set up some initial state
86
00:04:26,750 --> 00:04:29,450
and we can create a separate constant for this,
87
00:04:29,450 --> 00:04:33,040
or do it here on the fly and I'll do the ladder
88
00:04:33,040 --> 00:04:36,760
and my initial state is that the cartIsVisible
89
00:04:36,760 --> 00:04:40,030
property is false, let's say.
90
00:04:40,030 --> 00:04:42,793
Of course you can name this property however you want.
91
00:04:43,680 --> 00:04:46,580
That will be the property which controls whether the cart
92
00:04:46,580 --> 00:04:50,163
is visible or not, as you can probably guess by its name.
93
00:04:51,210 --> 00:04:53,330
Then we need the reducers key,
94
00:04:53,330 --> 00:04:57,800
which is a map of all the reducers or to be precise
95
00:04:57,800 --> 00:05:00,750
it's a map of methods that represent
96
00:05:00,750 --> 00:05:04,170
all the different cases, the different actions
97
00:05:04,170 --> 00:05:07,160
we wanna handle with that reducer.
98
00:05:07,160 --> 00:05:10,350
And here, I actually only need one method.
99
00:05:10,350 --> 00:05:15,350
Let's say the toggle method, which receives the old state
100
00:05:15,390 --> 00:05:18,850
and where I then wanna set statecartIsVisible
101
00:05:18,850 --> 00:05:21,913
to the opposite of what it was.
102
00:05:22,790 --> 00:05:26,810
And we can write this mutating code here
103
00:05:26,810 --> 00:05:30,210
because you learned that when using Redux Toolkit,
104
00:05:30,210 --> 00:05:33,160
we are not really mutating the state,
105
00:05:33,160 --> 00:05:35,520
even though it looks like we do,
106
00:05:35,520 --> 00:05:40,260
but instead Redux Toolkit will kind of capture this code
107
00:05:40,260 --> 00:05:44,170
and use another third party library imer to ensure
108
00:05:44,170 --> 00:05:48,310
that this is actually translated to some immutable code
109
00:05:48,310 --> 00:05:51,120
which creates a new state object instead
110
00:05:51,120 --> 00:05:53,023
of manipulating the existing one.
111
00:05:54,260 --> 00:05:56,240
And that is our slice.
112
00:05:56,240 --> 00:06:00,280
Now I'll store it in a constant UI Slice
113
00:06:00,280 --> 00:06:03,610
and then export this as a default here.
114
00:06:03,610 --> 00:06:05,960
And actually that's not all I wanna export,
115
00:06:05,960 --> 00:06:09,110
I also wanna export the actions,
116
00:06:09,110 --> 00:06:11,500
so I'll create a new constant which I export,
117
00:06:11,500 --> 00:06:16,423
the UI Actions, which we get by accessing uislice.actions.
118
00:06:18,200 --> 00:06:20,980
Now, with that exported, in index JS,
119
00:06:20,980 --> 00:06:24,910
we wanna create our store and we do that by importing
120
00:06:24,910 --> 00:06:28,833
configure store from @reduxjstoolkit,
121
00:06:29,810 --> 00:06:34,720
and then we call configure store here and configure store,
122
00:06:34,720 --> 00:06:38,723
then once object, where we set up our route reducer.
123
00:06:41,100 --> 00:06:44,710
Now that can be a single reducer function
124
00:06:44,710 --> 00:06:47,560
or a map of reducers.
125
00:06:47,560 --> 00:06:50,110
And I'll go for the ladder even though at the moment
126
00:06:50,110 --> 00:06:52,180
we only have a single reducer,
127
00:06:52,180 --> 00:06:55,680
because we only have one slice, but since we'll later
128
00:06:55,680 --> 00:06:59,300
have another slice, I'll set this to a map immediately
129
00:06:59,300 --> 00:07:04,300
and add a key off UI, though that is up to you,
130
00:07:04,690 --> 00:07:06,440
but I'll name it UI.
131
00:07:06,440 --> 00:07:11,370
And then add an import where I import the UI Slice
132
00:07:11,370 --> 00:07:16,370
from ./uislice, and therefore as a value for UI,
133
00:07:16,720 --> 00:07:19,980
I point at UI Slice reducer,
134
00:07:19,980 --> 00:07:23,143
so the reducer created by UI Slice.
135
00:07:24,340 --> 00:07:27,020
Now that gives us our store here and therefore,
136
00:07:27,020 --> 00:07:32,020
we can export this store, like this,
137
00:07:32,150 --> 00:07:35,000
with the store exported, we need to provide us
138
00:07:35,000 --> 00:07:38,420
to the application for it to have an effect.
139
00:07:38,420 --> 00:07:41,770
And we can do this in the index JS file
140
00:07:41,770 --> 00:07:44,140
where we set up our route component
141
00:07:44,140 --> 00:07:47,763
where we render our route application component.
142
00:07:48,670 --> 00:07:53,210
There, we can import provider, the provider component
143
00:07:53,210 --> 00:07:57,590
from React Redux and wrap that around app.
144
00:07:57,590 --> 00:08:02,590
So wrap provider around app like this,
145
00:08:02,690 --> 00:08:04,840
and provide our store.
146
00:08:04,840 --> 00:08:07,870
Now for that of course we also need to import our store,
147
00:08:07,870 --> 00:08:10,413
so import store from ./storeindex.
148
00:08:12,640 --> 00:08:16,700
So from that index JS file and set the store prop
149
00:08:16,700 --> 00:08:20,090
off provider equal to that imported store.
150
00:08:20,090 --> 00:08:25,090
That's how we provide our Redux store QD entire application,
151
00:08:25,090 --> 00:08:29,003
so to all components that make up our application.
152
00:08:30,010 --> 00:08:34,600
And with that provided, we can now utilize Redux inside
153
00:08:34,600 --> 00:08:37,159
of these different components.
154
00:08:37,159 --> 00:08:41,530
So for example, in the layout component,
155
00:08:41,530 --> 00:08:44,870
in the layout folder, in the main header component there,
156
00:08:44,870 --> 00:08:47,020
we got this Cart button,
157
00:08:47,020 --> 00:08:49,670
this Cart button which when clicked,
158
00:08:49,670 --> 00:08:52,120
should show the cart area.
159
00:08:52,120 --> 00:08:53,330
Now to make that work,
160
00:08:53,330 --> 00:08:56,620
we actually need to go to that Cart button component,
161
00:08:56,620 --> 00:08:58,860
which we find in the cart folder,
162
00:08:58,860 --> 00:09:01,720
and then here we have our button.
163
00:09:01,720 --> 00:09:04,710
Now on that button, we now wanna add an on click listener
164
00:09:04,710 --> 00:09:07,860
to make sure that we can do something when it's clicked.
165
00:09:07,860 --> 00:09:11,480
And I'll add a new function in the Cart button component,
166
00:09:11,480 --> 00:09:15,133
the toggleCartHandler function,
167
00:09:16,020 --> 00:09:20,610
where I want to dispatch the logic for toggling the cart,
168
00:09:20,610 --> 00:09:23,520
so for showing or hiding the cart.
169
00:09:23,520 --> 00:09:26,640
And for this, of course, we need to dispatch an action
170
00:09:26,640 --> 00:09:29,080
which triggers this toggle method
171
00:09:29,080 --> 00:09:33,080
in our UI Slice reducers map.
172
00:09:33,080 --> 00:09:37,193
So in this a reducer functions map to be precise.
173
00:09:38,090 --> 00:09:40,940
And we can do this with help of the UI Actions,
174
00:09:40,940 --> 00:09:44,200
which we are exporting in Cart button.
175
00:09:44,200 --> 00:09:49,200
We can import UI Actions as a named import
176
00:09:50,190 --> 00:09:53,193
because we export it as a named export,
177
00:09:54,270 --> 00:09:57,690
imported from and then going up a level
178
00:09:57,690 --> 00:10:01,400
and another level diving into store UI Slice.
179
00:10:01,400 --> 00:10:03,640
From there, I import the actions
180
00:10:03,640 --> 00:10:05,700
and then toggleCartHandler,
181
00:10:05,700 --> 00:10:08,590
we can then dispatch our action.
182
00:10:08,590 --> 00:10:12,120
For this I'll bind toggleCartHandler to the click
183
00:10:12,120 --> 00:10:16,000
and to dispatch, we need access to the dispatch function.
184
00:10:16,000 --> 00:10:18,990
Now we do get access to that by importing
185
00:10:18,990 --> 00:10:23,990
the us dispatch hook from React Redux, as you learned.
186
00:10:24,750 --> 00:10:28,120
We can execute this hook in our component function
187
00:10:28,120 --> 00:10:30,650
and when we do so, this will give us access
188
00:10:30,650 --> 00:10:33,693
to the dispatch function provided by Redux.
189
00:10:34,720 --> 00:10:37,470
And we can not use this dispatch function here
190
00:10:37,470 --> 00:10:39,680
inside of toggleCartHandler
191
00:10:39,680 --> 00:10:42,630
and then simply dispatch the action
192
00:10:42,630 --> 00:10:47,630
which is created by the UI Actions, toggle action creator.
193
00:10:48,840 --> 00:10:50,560
We need to execute toggle
194
00:10:50,560 --> 00:10:52,860
as a method here because as you'll learned
195
00:10:52,860 --> 00:10:57,500
in the last core section, these auto-generated actions
196
00:10:57,500 --> 00:11:00,870
which you get here, are actually action creator methods,
197
00:11:00,870 --> 00:11:04,160
which you have to execute and when you execute them,
198
00:11:04,160 --> 00:11:06,960
they return action objects.
199
00:11:06,960 --> 00:11:09,780
So it's then this returned action object
200
00:11:09,780 --> 00:11:11,173
which we dispatch here.
201
00:11:12,170 --> 00:11:14,670
And with that, we should trigger toggleCartHandler
202
00:11:15,670 --> 00:11:17,683
whenever we click on the button.
203
00:11:18,710 --> 00:11:21,840
Now at the moment of course, if we have a look at UI Slice,
204
00:11:21,840 --> 00:11:24,750
we are then changing cartIsVisible,
205
00:11:24,750 --> 00:11:27,720
but we're not taking advantage of that right now.
206
00:11:27,720 --> 00:11:29,530
That's something we need to do.
207
00:11:29,530 --> 00:11:32,140
And for this, we should go to the app component
208
00:11:32,140 --> 00:11:35,920
because that is where we render the cart.
209
00:11:35,920 --> 00:11:38,870
And I now want to render it conditionally based
210
00:11:38,870 --> 00:11:41,653
on that UI Slice state value.
211
00:11:42,610 --> 00:11:44,750
So for that inside of app component,
212
00:11:44,750 --> 00:11:47,580
we need to extract data from Redux.
213
00:11:47,580 --> 00:11:49,670
And we do that with another hook
214
00:11:49,670 --> 00:11:52,540
which we import from React Redux.
215
00:11:52,540 --> 00:11:56,460
It's the use selector hook as you learned.
216
00:11:56,460 --> 00:11:59,870
We can import use selector and then execute
217
00:11:59,870 --> 00:12:03,850
that in our app component function, use selector like this,
218
00:12:03,850 --> 00:12:05,710
not use effect.
219
00:12:05,710 --> 00:12:10,260
We execute this and then you learned that to use selector,
220
00:12:10,260 --> 00:12:13,810
we need to pass a function which receives the Redux state
221
00:12:13,810 --> 00:12:16,170
automatically because this function
222
00:12:16,170 --> 00:12:19,490
which we pass to use selector will be executed
223
00:12:19,490 --> 00:12:21,480
by React Redux.
224
00:12:21,480 --> 00:12:24,200
So it receives the current state automatically
225
00:12:24,200 --> 00:12:26,220
and we should return the data
226
00:12:26,220 --> 00:12:29,000
which we wanna use in this component.
227
00:12:29,000 --> 00:12:31,164
And in this case, that is this
228
00:12:31,164 --> 00:12:33,710
cartIsVisible property value.
229
00:12:33,710 --> 00:12:36,690
But for this we'll need to drill into that state slice
230
00:12:36,690 --> 00:12:40,370
and since I'm setting up a map of reducers
231
00:12:40,370 --> 00:12:43,540
here in the index JS file in the store folder,
232
00:12:43,540 --> 00:12:45,890
we need to use this key to drill
233
00:12:45,890 --> 00:12:48,680
into that part of the state, so to say,
234
00:12:48,680 --> 00:12:52,233
and then use that property name in which we're interested.
235
00:12:53,350 --> 00:12:56,820
So here we need to access state.ui.cartIsVisible.
236
00:12:59,070 --> 00:13:01,230
That's how we extract that value
237
00:13:01,230 --> 00:13:03,250
and then we can store this in a constant.
238
00:13:03,250 --> 00:13:06,720
ShowCart could be the constant name
239
00:13:06,720 --> 00:13:09,270
and we can now use this to conditionally show
240
00:13:09,270 --> 00:13:12,633
or hide this cart component like this.
241
00:13:13,990 --> 00:13:18,830
If we do that and save this and restart the dev server
242
00:13:18,830 --> 00:13:21,870
to make sure that that's up and running,
243
00:13:21,870 --> 00:13:25,060
we should be able that once it is running again,
244
00:13:25,060 --> 00:13:29,360
that we actually click My Cart to show it and hide it.
245
00:13:29,360 --> 00:13:32,393
So that is working and that's the first part.
246
00:13:33,270 --> 00:13:37,100
Now the missing part and arguably the more difficult part,
247
00:13:37,100 --> 00:13:40,370
is that we now also wanna manage the content off the cart.
248
00:13:40,370 --> 00:13:43,890
So the cart items should be updated correctly when we click
249
00:13:43,890 --> 00:13:47,620
Add to Cart or when we click Plus or Minus here.
250
00:13:47,620 --> 00:13:49,860
And actually the batch of value here,
251
00:13:49,860 --> 00:13:53,020
in the My Cart button, should also be updated.
252
00:13:53,020 --> 00:13:55,250
So let's work on this now.
253
00:13:55,250 --> 00:13:58,910
For this all dive into the cart slice JS file,
254
00:13:58,910 --> 00:14:03,910
and again import create slice from Redux @reduxtoolkit.
255
00:14:08,620 --> 00:14:12,690
Then we create a slice here and configure it with an object.
256
00:14:12,690 --> 00:14:16,360
It gets a name, for example, cart,
257
00:14:16,360 --> 00:14:20,840
it gets an initial state and it will get some reducers.
258
00:14:20,840 --> 00:14:24,200
And I'll come back to the initial state in a second.
259
00:14:24,200 --> 00:14:25,790
Now, what is our initial state here?
260
00:14:25,790 --> 00:14:29,900
How should the state of this state slice look like?
261
00:14:29,900 --> 00:14:33,340
Which structure should, does state slice have?
262
00:14:33,340 --> 00:14:37,240
I think it makes sense to have some items, some cart items
263
00:14:37,240 --> 00:14:41,570
and that should probably be an array of items.
264
00:14:41,570 --> 00:14:45,160
We probably also wanna have a total quantity
265
00:14:45,160 --> 00:14:47,090
of items in the cart.
266
00:14:47,090 --> 00:14:50,370
And with that, I don't mean the length of this array,
267
00:14:50,370 --> 00:14:54,800
but the quantity of the items of that array summed up,
268
00:14:54,800 --> 00:14:57,060
and initially that's zero.
269
00:14:57,060 --> 00:14:59,180
And the total amount is probably also
270
00:14:59,180 --> 00:15:02,160
something which makes sense, so the total price
271
00:15:02,160 --> 00:15:05,210
which also should be zero initially.
272
00:15:05,210 --> 00:15:07,990
Though for this application we actually don't need this,
273
00:15:07,990 --> 00:15:10,590
because we're not showing the total price anywhere,
274
00:15:10,590 --> 00:15:12,760
so I guess we can omit this, but you could
275
00:15:12,760 --> 00:15:14,083
of course also add it.
276
00:15:15,010 --> 00:15:18,320
Now we also need functions in our reducer,
277
00:15:18,320 --> 00:15:22,130
so different actions which this part of our state
278
00:15:22,130 --> 00:15:23,380
should handle in the end.
279
00:15:24,220 --> 00:15:29,030
And here be clearly need a addItemToCart action
280
00:15:30,950 --> 00:15:35,950
and a removeItemFromCart action, I would argue.
281
00:15:36,020 --> 00:15:39,560
And it probably makes sense to start with adding items.
282
00:15:39,560 --> 00:15:42,580
So here we get a state and we'll also accept
283
00:15:42,580 --> 00:15:45,640
this action argument because this action,
284
00:15:45,640 --> 00:15:49,350
when it is dispatched, should carry extra information.
285
00:15:49,350 --> 00:15:53,010
We need to know which items should be added after all.
286
00:15:53,010 --> 00:15:56,090
So let's dive into this add item to cart functional.
287
00:15:56,090 --> 00:15:58,290
What should happen in there?
288
00:15:58,290 --> 00:16:00,830
Well, in there, we should probably extract the item
289
00:16:00,830 --> 00:16:04,140
from the action and there keep in mind that it will
290
00:16:04,140 --> 00:16:08,030
be the payload property which Redux Toolkit sets
291
00:16:08,030 --> 00:16:11,030
for you which contains any extra data
292
00:16:11,030 --> 00:16:13,080
you add it to the action.
293
00:16:13,080 --> 00:16:15,540
And here, I assume that this extra data
294
00:16:15,540 --> 00:16:19,760
is another object describing the item that should be added.
295
00:16:19,760 --> 00:16:22,380
Now we could just push it to disarray,
296
00:16:22,380 --> 00:16:24,960
but actually I wanna check if it's part
297
00:16:24,960 --> 00:16:28,260
of that array already and if it is, I wanna increase
298
00:16:28,260 --> 00:16:31,440
the quantity of the existing cart item instead
299
00:16:31,440 --> 00:16:33,913
of push it as an additional item.
300
00:16:34,890 --> 00:16:39,890
So therefore, I want to get my existing item let's say,
301
00:16:40,030 --> 00:16:42,300
by reaching out to the state items
302
00:16:42,300 --> 00:16:47,300
and finding an item in there where the item ID, let's assume
303
00:16:47,350 --> 00:16:51,910
that our items have IDs, is equal to the item ID
304
00:16:51,910 --> 00:16:53,800
of the item we're getting here.
305
00:16:53,800 --> 00:16:57,220
And since I now have a name clash, I'll name this here
306
00:16:57,220 --> 00:17:01,450
new item and refer to that here.
307
00:17:01,450 --> 00:17:04,653
So that's how I check if that item already exists.
308
00:17:05,609 --> 00:17:10,130
Now, if it does not exist, if existing item is falsy,
309
00:17:10,130 --> 00:17:12,540
so if it's not part of the array yet,
310
00:17:12,540 --> 00:17:13,980
then we wanna add it.
311
00:17:13,980 --> 00:17:18,770
So then we wanna go to our state items and push a new item.
312
00:17:18,770 --> 00:17:22,980
And that would be absolutely bad if you're using just Redux
313
00:17:22,980 --> 00:17:25,940
because push manipulates the existing array
314
00:17:25,940 --> 00:17:27,690
in the existing state.
315
00:17:27,690 --> 00:17:32,220
And that's a must not do, but with Redux Toolkit,
316
00:17:32,220 --> 00:17:35,340
as emphasized before, we don't have that problem
317
00:17:35,340 --> 00:17:38,890
because their Redux Toolkit internally ensures
318
00:17:38,890 --> 00:17:42,060
that this will not manipulate the existing state
319
00:17:42,060 --> 00:17:45,430
but that it instead transforms this into an operation
320
00:17:45,430 --> 00:17:49,270
which updates the state in an immutable way.
321
00:17:49,270 --> 00:17:53,720
So we can use push here when working with Redux Toolkit.
322
00:17:53,720 --> 00:17:56,540
And then we push a new object let's say,
323
00:17:56,540 --> 00:17:59,960
to our array where we have an item ID field
324
00:17:59,960 --> 00:18:04,960
which is, newItem.id, where we have a price field
325
00:18:05,210 --> 00:18:07,730
which let's say is, newItem.price,
326
00:18:07,730 --> 00:18:11,543
expecting a price field on all our products.
327
00:18:12,440 --> 00:18:17,300
Then the quantity let's say, which is one hard-coded,
328
00:18:17,300 --> 00:18:19,980
because if we add an item for the first time,
329
00:18:19,980 --> 00:18:21,513
the quantity will be one.
330
00:18:22,880 --> 00:18:25,230
And therefore we always have a total price
331
00:18:25,230 --> 00:18:27,580
which is also newItem.price.
332
00:18:27,580 --> 00:18:29,220
It's price times quantity,
333
00:18:29,220 --> 00:18:32,283
but since quantity is one, it's just the price.
334
00:18:33,140 --> 00:18:36,930
That could be an item which we push onto this array,
335
00:18:36,930 --> 00:18:40,390
maybe we should also add the title or the name,
336
00:18:40,390 --> 00:18:42,700
however you want to name it after product
337
00:18:42,700 --> 00:18:46,800
and I expect this on newItem.title.
338
00:18:46,800 --> 00:18:49,110
Now these field names are all up to you,
339
00:18:49,110 --> 00:18:51,700
because it will be your data, which you manage.
340
00:18:51,700 --> 00:18:54,160
You just need to ensure that you then reference
341
00:18:54,160 --> 00:18:57,910
and create objects that have the correct structure.
342
00:18:57,910 --> 00:19:00,120
So with that, we're pushing a new item
343
00:19:00,120 --> 00:19:02,580
onto our products array.
344
00:19:02,580 --> 00:19:05,000
But we also need to work on the else case,
345
00:19:05,000 --> 00:19:07,230
so the item does exist.
346
00:19:07,230 --> 00:19:10,650
If that's the case, we wanna update the existing item,
347
00:19:10,650 --> 00:19:14,740
again, not something you should do with just Redux
348
00:19:14,740 --> 00:19:18,820
but absolutely fine when working with Redux Toolkit.
349
00:19:18,820 --> 00:19:21,050
Then we reach out to the existing item
350
00:19:21,050 --> 00:19:25,400
and we update those fields on the existing item.
351
00:19:25,400 --> 00:19:28,100
The item ID does not need to be updated,
352
00:19:28,100 --> 00:19:30,220
the price also hasn't changed,
353
00:19:30,220 --> 00:19:32,160
but the quantity should be set
354
00:19:32,160 --> 00:19:35,780
to existing item quantity plus one.
355
00:19:35,780 --> 00:19:38,920
And therefore we can, of course, also write it like this,
356
00:19:38,920 --> 00:19:41,540
the total price should be set equal
357
00:19:41,540 --> 00:19:46,540
to total prize plus newItem.price.
358
00:19:46,660 --> 00:19:47,539
So we add the price and of course that should be
359
00:19:47,539 --> 00:19:52,539
existingItem.totalPrice.
360
00:19:54,130 --> 00:19:57,000
So we increased the existing total price
361
00:19:57,000 --> 00:19:59,793
by adding the item price again.
362
00:20:00,680 --> 00:20:03,340
And the name also hasn't changed.
363
00:20:03,340 --> 00:20:06,910
So we updated quantity and total price, and we're done.
364
00:20:06,910 --> 00:20:10,313
That's our addItemToCart function here.
29857
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.