All language subtitles for 014 Adding a Cart Reducer_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian Download
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,220 --> 00:00:04,360 For adding cart items, 2 00:00:04,360 --> 00:00:06,880 we need to go to the CartProvider 3 00:00:06,880 --> 00:00:10,700 because that's where we manage our cart data in the end. 4 00:00:10,700 --> 00:00:13,653 And here we've got this addItemToCartHandler. 5 00:00:14,540 --> 00:00:18,000 Now, whenever this function is called, 6 00:00:18,000 --> 00:00:21,620 we get the item that should be added to the cart. 7 00:00:21,620 --> 00:00:23,490 And then I basically wanna check 8 00:00:23,490 --> 00:00:27,140 if that item is already part of the cart. 9 00:00:27,140 --> 00:00:30,880 So if we, for example, already have sushi in the cart, 10 00:00:30,880 --> 00:00:34,360 and if that's the case, I wanna update the existing item. 11 00:00:34,360 --> 00:00:37,706 If it's not the case, I wanna add a new item. 12 00:00:37,706 --> 00:00:39,560 And of course, we wanna manage this 13 00:00:39,560 --> 00:00:41,740 as state in this component 14 00:00:41,740 --> 00:00:45,650 so that this component and therefore, the context 15 00:00:45,650 --> 00:00:49,160 and therefore, any components affected by the context 16 00:00:49,160 --> 00:00:54,160 are re-evaluated whenever the cart data changes. 17 00:00:54,280 --> 00:00:59,280 So here we wanna import useState or useReducer. 18 00:00:59,730 --> 00:01:04,090 Both are React hooks which allow us to manage state. 19 00:01:04,090 --> 00:01:07,010 And I will go for useReducer here 20 00:01:07,010 --> 00:01:10,810 because this will be a bit of a more complex state, 21 00:01:10,810 --> 00:01:12,149 which we're managing here 22 00:01:12,149 --> 00:01:14,670 because we'll have to check whether a meal 23 00:01:14,670 --> 00:01:17,280 is already part of the cart or not 24 00:01:17,280 --> 00:01:21,090 and for removing, we'll also have more complex logic. 25 00:01:21,090 --> 00:01:24,703 Hence I will use useReducer for managing the state here. 26 00:01:25,850 --> 00:01:27,619 And therefore, as a next step, 27 00:01:27,619 --> 00:01:30,640 outside of this component function, 28 00:01:30,640 --> 00:01:33,750 I'll add my cartReducer function 29 00:01:35,270 --> 00:01:37,290 outside of the component function 30 00:01:37,290 --> 00:01:40,760 because this reducer function won't need anything 31 00:01:40,760 --> 00:01:42,690 from that component. 32 00:01:42,690 --> 00:01:44,560 It won't need any surrounding data 33 00:01:44,560 --> 00:01:46,510 to find in this component. 34 00:01:46,510 --> 00:01:49,300 And it shouldn't be recreated all the time 35 00:01:49,300 --> 00:01:51,793 when the component is reevaluated. 36 00:01:53,820 --> 00:01:56,380 Now, you learned that in the reducer functions, 37 00:01:56,380 --> 00:01:58,280 you receive a state object 38 00:01:58,280 --> 00:02:02,820 and an action automatically by React. 39 00:02:02,820 --> 00:02:07,000 The action is dispatched by you later in your code 40 00:02:07,000 --> 00:02:10,717 and the state is simply the last state snapshot 41 00:02:10,717 --> 00:02:13,423 of the state managed by the reducer. 42 00:02:14,320 --> 00:02:17,550 And then as part of the reducer function, 43 00:02:17,550 --> 00:02:20,083 you have to return a new state snapshot. 44 00:02:22,200 --> 00:02:26,500 Now, for this, I'll define another constant outside 45 00:02:26,500 --> 00:02:28,100 of the component function 46 00:02:28,100 --> 00:02:30,600 and outside of the reducer function 47 00:02:30,600 --> 00:02:32,150 and that's my defaultCartState. 48 00:02:33,810 --> 00:02:35,690 Now, that's simply an object 49 00:02:35,690 --> 00:02:38,140 where I say that I have no items, 50 00:02:38,140 --> 00:02:42,303 so an empty array and a totalAmount of zero therefore. 51 00:02:46,298 --> 00:02:47,780 And then it's this defaultCartState, 52 00:02:47,780 --> 00:02:49,903 which I return here in cartReducer. 53 00:02:52,360 --> 00:02:55,240 Now, with that, in CartProvider 54 00:02:55,240 --> 00:02:57,660 at the very top of this component function, 55 00:02:57,660 --> 00:03:00,890 we can call useReducer, this React hook, 56 00:03:00,890 --> 00:03:02,630 which we're importing 57 00:03:02,630 --> 00:03:04,780 and now as a first argument, 58 00:03:04,780 --> 00:03:09,570 we point at our reducer function here. 59 00:03:09,570 --> 00:03:12,010 We don't execute it, we just point at it. 60 00:03:12,010 --> 00:03:14,763 It will be executed by React for us then. 61 00:03:15,670 --> 00:03:17,740 And we set an initial state 62 00:03:17,740 --> 00:03:20,090 and that's the defaultCartState. 63 00:03:20,090 --> 00:03:22,323 So I'm reusing that constant here. 64 00:03:24,330 --> 00:03:28,010 Now, you learned that useReducer returns an array 65 00:03:28,010 --> 00:03:30,840 with exactly two elements. 66 00:03:30,840 --> 00:03:32,720 Hence we can use array destructuring 67 00:03:32,720 --> 00:03:34,990 to pull these elements out of that array 68 00:03:34,990 --> 00:03:37,313 and store them in separate constants. 69 00:03:38,410 --> 00:03:43,250 The first array element is always your state snapshot. 70 00:03:43,250 --> 00:03:45,343 So I will name it cartState here. 71 00:03:46,490 --> 00:03:49,740 The second element is this function, 72 00:03:49,740 --> 00:03:54,740 which allows you to dispatch an action to the reducer. 73 00:03:54,860 --> 00:03:57,470 So I will name it dispatchCartAction. 74 00:03:57,470 --> 00:03:59,260 Both names are up to you 75 00:03:59,260 --> 00:04:01,920 because it's the position which matters here 76 00:04:01,920 --> 00:04:03,573 in array destructuring. 77 00:04:05,720 --> 00:04:07,730 Now, the cartState is now needed 78 00:04:07,730 --> 00:04:11,388 to construct this cartContext object. 79 00:04:11,388 --> 00:04:14,200 Instead of hard coding the empty array here, 80 00:04:14,200 --> 00:04:16,660 I will access cartState.items 81 00:04:17,660 --> 00:04:20,490 because we're now managing the items with state 82 00:04:20,490 --> 00:04:23,933 and the totalAmount with cartState.totalAmount. 83 00:04:26,490 --> 00:04:29,450 Now we can start dispatching actions 84 00:04:29,450 --> 00:04:32,720 and I wanna start with adding items to the cart. 85 00:04:32,720 --> 00:04:35,420 Here we can then dispatch a cart action 86 00:04:35,420 --> 00:04:37,810 and as I explained when I introduced 87 00:04:37,810 --> 00:04:41,020 the useReducer hook earlier in the course, 88 00:04:41,020 --> 00:04:44,418 it's totally up to you what an action is. 89 00:04:44,418 --> 00:04:47,170 It could just be a number or a text 90 00:04:47,170 --> 00:04:49,100 but typically it's an object, 91 00:04:49,100 --> 00:04:50,610 which has some property, 92 00:04:50,610 --> 00:04:53,450 which allows you to identify that action 93 00:04:53,450 --> 00:04:55,940 inside of your reducer function 94 00:04:55,940 --> 00:04:58,720 so that in that reducer function, 95 00:04:58,720 --> 00:05:02,580 you can run different pieces of code depending 96 00:05:02,580 --> 00:05:05,193 on which action type was dispatched. 97 00:05:06,170 --> 00:05:09,160 And I will use type as a property name 98 00:05:09,160 --> 00:05:10,890 for identifying the action 99 00:05:10,890 --> 00:05:13,250 but it's totally up to you how you name this. 100 00:05:13,250 --> 00:05:15,300 You could also name it identifier 101 00:05:15,300 --> 00:05:16,950 or whatever you want, 102 00:05:16,950 --> 00:05:18,770 but I will go with type 103 00:05:18,770 --> 00:05:20,330 and then follow this convention 104 00:05:20,330 --> 00:05:24,230 of having a string all caps identifier. 105 00:05:24,230 --> 00:05:26,670 Again, it's all just a convention 106 00:05:26,670 --> 00:05:28,960 but a very readable and common one, 107 00:05:28,960 --> 00:05:31,550 which is why I will stick to it. 108 00:05:31,550 --> 00:05:35,800 And then here we have let's say a type of ADD. 109 00:05:35,800 --> 00:05:39,320 It could also be ADD_ITEM or ADD_CART_ITEM 110 00:05:39,320 --> 00:05:40,680 or whatever you want 111 00:05:40,680 --> 00:05:44,053 but I'll keep it simple and short and just use ADD. 112 00:05:45,770 --> 00:05:49,510 Now, to add the item in the reducer function, 113 00:05:49,510 --> 00:05:53,170 I also wanna forward the item as part of the action. 114 00:05:53,170 --> 00:05:56,620 So I will add a second property to this action object 115 00:05:56,620 --> 00:05:58,140 and name it item. 116 00:05:58,140 --> 00:05:59,520 This name is also up to you 117 00:05:59,520 --> 00:06:02,210 and point at my item argument here 118 00:06:02,210 --> 00:06:04,450 so that I'm forwarding the item, 119 00:06:04,450 --> 00:06:08,040 which I expect to get here on this function 120 00:06:08,040 --> 00:06:10,163 to my reducer. 121 00:06:11,280 --> 00:06:13,820 And now we can go to the cartReducer 122 00:06:13,820 --> 00:06:17,313 and start adding logic for adding a cart item here. 123 00:06:18,190 --> 00:06:19,380 For this, I, first of all, 124 00:06:19,380 --> 00:06:20,450 wanna add an if check 125 00:06:20,450 --> 00:06:23,910 and check if the action.type is equal to ADD 126 00:06:23,910 --> 00:06:26,410 because I only wanna run this adding logic 127 00:06:26,410 --> 00:06:28,530 for this kind of action. 128 00:06:28,530 --> 00:06:31,140 At the moment, it is the only action type we have 129 00:06:31,140 --> 00:06:33,803 but later, we'll also have a remove action. 130 00:06:34,900 --> 00:06:37,690 If we want to, we can already dispatch this here, 131 00:06:37,690 --> 00:06:38,523 by the way. 132 00:06:38,523 --> 00:06:41,760 We can already add type: REMOVE here 133 00:06:41,760 --> 00:06:44,660 and forward the id, let's say, like this 134 00:06:44,660 --> 00:06:47,183 in the removeItemFromCartHandler. 135 00:06:48,230 --> 00:06:51,000 Now we would have, theoretically at least, 136 00:06:51,000 --> 00:06:52,340 two kinds of actions, 137 00:06:52,340 --> 00:06:54,170 which trigger this reducer 138 00:06:54,170 --> 00:06:55,450 and therefore, we wanna check 139 00:06:55,450 --> 00:06:58,283 for which type of action it was triggered. 140 00:07:00,040 --> 00:07:04,370 And now in here, I wanna update my cart items. 141 00:07:04,370 --> 00:07:08,870 We could simply add our item as a new item in that array 142 00:07:08,870 --> 00:07:11,110 but as I mentioned, I wanna group items 143 00:07:11,110 --> 00:07:13,210 for the same meal together 144 00:07:13,210 --> 00:07:17,220 and manage the amount on a per meal basis. 145 00:07:17,220 --> 00:07:20,060 I also wanna update the totalAmount, 146 00:07:20,060 --> 00:07:24,283 the total price for all aggregated cart items. 147 00:07:25,970 --> 00:07:28,740 Now, let's start simple and simply add a item 148 00:07:28,740 --> 00:07:31,610 as a item to the array though. 149 00:07:31,610 --> 00:07:36,610 And or this, we can simply create an array. 150 00:07:36,910 --> 00:07:38,600 Maybe name it updatedItems 151 00:07:39,670 --> 00:07:43,470 and set this equal to state.items. 152 00:07:43,470 --> 00:07:46,390 So the items in the current state snapshot, 153 00:07:46,390 --> 00:07:50,880 which we get as a first argument in the reducer by React. 154 00:07:50,880 --> 00:07:52,263 And call concat. 155 00:07:53,280 --> 00:07:55,860 Concat is a built-in method in JavaScript, 156 00:07:55,860 --> 00:07:57,950 which adds a new item to an array 157 00:07:57,950 --> 00:08:02,760 but unlike push, it doesn't edit the existing array 158 00:08:02,760 --> 00:08:05,100 but return a new array. 159 00:08:05,100 --> 00:08:06,780 And that's kind of important. 160 00:08:06,780 --> 00:08:09,650 You wanna update your state in an immutable way, 161 00:08:09,650 --> 00:08:12,080 which simply means you don't wanna edit 162 00:08:12,080 --> 00:08:14,190 your old state snapshot 163 00:08:14,190 --> 00:08:17,680 because of the reference value thing in JavaScript. 164 00:08:17,680 --> 00:08:20,870 That would mean that existing data in memory gets edited 165 00:08:20,870 --> 00:08:23,090 without React knowing about it, 166 00:08:23,090 --> 00:08:26,550 but instead, you wanna generate a brand new state object, 167 00:08:26,550 --> 00:08:27,860 which you return. 168 00:08:27,860 --> 00:08:30,370 And concat gives us a brand new array 169 00:08:30,370 --> 00:08:32,809 instead of editing the old array in memory, 170 00:08:32,809 --> 00:08:34,360 which is better. 171 00:08:34,360 --> 00:08:38,380 And we wanna concat this item. 172 00:08:38,380 --> 00:08:42,460 This action.item to be precise, which we're getting. 173 00:08:42,460 --> 00:08:46,800 And I simply expect to have all the data I need 174 00:08:46,800 --> 00:08:48,420 on that action.item. 175 00:08:48,420 --> 00:08:51,750 So the name, the amounts of items 176 00:08:51,750 --> 00:08:53,730 that should be added, the price, 177 00:08:53,730 --> 00:08:56,710 I expect to have all of that on the item, 178 00:08:56,710 --> 00:09:00,970 which reaches this addItemToCarthandler. 179 00:09:00,970 --> 00:09:03,500 Currently, we're not taking care about this yet 180 00:09:03,500 --> 00:09:05,740 in the other parts of the applications 181 00:09:05,740 --> 00:09:07,743 but we will do so in the future. 182 00:09:10,110 --> 00:09:13,210 Now, with that, we get the updatedItems. 183 00:09:13,210 --> 00:09:17,050 I also wanna get my newTotalAmount 184 00:09:19,510 --> 00:09:20,710 or my updatedTotalAmount 185 00:09:22,790 --> 00:09:26,310 and that should simply be the old totalAmount 186 00:09:26,310 --> 00:09:28,610 on the old state snapshot 187 00:09:28,610 --> 00:09:32,840 plus well, action.item.price 188 00:09:32,840 --> 00:09:35,173 times action.item.amount. 189 00:09:36,050 --> 00:09:38,480 So I expect to have an amount field 190 00:09:38,480 --> 00:09:41,520 on that item which we're getting and a price field. 191 00:09:41,520 --> 00:09:43,850 And of course, if we multiply these two, 192 00:09:43,850 --> 00:09:47,113 we know by how much our totalAmount needs to change. 193 00:09:48,610 --> 00:09:51,210 And with that, we can return our new state. 194 00:09:51,210 --> 00:09:53,930 At the moment, I'm not checking whether our item 195 00:09:53,930 --> 00:09:56,080 is already part of the array by the way. 196 00:09:56,080 --> 00:09:58,450 That's something we'll add in the future. 197 00:09:58,450 --> 00:10:02,040 For the moment, I will simply return a new state snapshot 198 00:10:02,040 --> 00:10:05,672 where I set items equals to updatedItems 199 00:10:05,672 --> 00:10:09,207 and totalAmount equal to updatedTotalAmount. 200 00:10:10,770 --> 00:10:15,180 And now we've got this basic cartReducer finished. 201 00:10:15,180 --> 00:10:17,820 Now we need to make sure as a next step 202 00:10:17,820 --> 00:10:20,330 that addItemToCartHandler 203 00:10:20,330 --> 00:10:24,070 is being called from some components in our application, 204 00:10:24,070 --> 00:10:27,521 like the Meals component where we have all these meal items 205 00:10:27,521 --> 00:10:28,900 and the MealItemForm 206 00:10:28,900 --> 00:10:31,700 with the appropriate data. 207 00:10:31,700 --> 00:10:34,660 So with an amount field on every item, 208 00:10:34,660 --> 00:10:36,680 with a price field on every item, 209 00:10:36,680 --> 00:10:38,780 with a name field on every item, 210 00:10:38,780 --> 00:10:41,180 with an ID field on every item. 211 00:10:41,180 --> 00:10:43,433 That's what I wanna work on next. 16260

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.