All language subtitles for 017 Working on a More Complex Reducer Logic_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,150 --> 00:00:03,310 Now, to change the logic 2 00:00:03,310 --> 00:00:04,830 for adding cart items, 3 00:00:04,830 --> 00:00:06,960 we need to head back to the CartProvider 4 00:00:06,960 --> 00:00:09,540 and there to the cartReducer. 5 00:00:09,540 --> 00:00:11,960 At the moment, items are always added 6 00:00:11,960 --> 00:00:14,190 as new items in that array. 7 00:00:14,190 --> 00:00:15,963 Now, that needs to change. 8 00:00:17,440 --> 00:00:21,710 When we're adding a item, I always wanna calculate 9 00:00:21,710 --> 00:00:24,550 my updatedTotalAmount like this first. 10 00:00:24,550 --> 00:00:26,060 That is always correct 11 00:00:26,060 --> 00:00:29,463 no matter if a item is already part of the cart or not. 12 00:00:30,550 --> 00:00:33,887 But then, before I derive my updatedItems, 13 00:00:34,930 --> 00:00:36,690 I instead wanna check 14 00:00:36,690 --> 00:00:38,950 if a item is already part of the cart. 15 00:00:38,950 --> 00:00:43,950 And for this, I'll create a existingCartItemIndex constant, 16 00:00:44,330 --> 00:00:48,140 where I will reach out to my existing items in the cart 17 00:00:48,140 --> 00:00:49,887 and where I call findIndex, 18 00:00:50,951 --> 00:00:53,245 which is a built-in method in JavaScript 19 00:00:53,245 --> 00:00:56,840 which finds the index of an item in an array. 20 00:00:56,840 --> 00:00:59,960 Now, it takes a function which should return true 21 00:00:59,960 --> 00:01:02,930 if that's the item we're looking for, and false otherwise, 22 00:01:02,930 --> 00:01:04,760 because this function will be executed 23 00:01:04,760 --> 00:01:06,880 for every item in the array. 24 00:01:06,880 --> 00:01:08,560 And I wanna return true 25 00:01:08,560 --> 00:01:13,460 if item.id is equal to action.item.id, 26 00:01:13,460 --> 00:01:16,430 so if the item we're currently looking at in that array 27 00:01:16,430 --> 00:01:19,570 has the same id as the item we're adding 28 00:01:19,570 --> 00:01:21,963 with this action which was dispatched. 29 00:01:23,240 --> 00:01:24,760 Now, this will then return us 30 00:01:24,760 --> 00:01:27,773 the index of that item if it exists. 31 00:01:28,840 --> 00:01:32,740 So what we can do now is we can get the existingCartItem 32 00:01:32,740 --> 00:01:34,750 by reaching out to state.items 33 00:01:34,750 --> 00:01:38,217 and accessing that existingCartItemIndex. 34 00:01:41,480 --> 00:01:45,510 This will only work if we have that item already. 35 00:01:45,510 --> 00:01:48,400 Otherwise, we'll try to access an item which doesn't exist, 36 00:01:48,400 --> 00:01:50,553 and hence existingCartItem would be null. 37 00:01:51,450 --> 00:01:54,450 If the item already is part of the array, though, 38 00:01:54,450 --> 00:01:56,663 this item will be set to that item. 39 00:01:57,900 --> 00:02:02,240 And now we can add an updatedItem variable 40 00:02:03,130 --> 00:02:06,963 and an updatedItems variable, 41 00:02:07,920 --> 00:02:10,820 which clashes with this constant, but we'll fix this soon. 42 00:02:11,670 --> 00:02:13,670 And then we add another if check here 43 00:02:13,670 --> 00:02:16,530 where we check if existingCartItem is a thing, 44 00:02:16,530 --> 00:02:19,640 if this is truthy, which will only be the case 45 00:02:19,640 --> 00:02:21,463 if it's already part of the array. 46 00:02:23,030 --> 00:02:25,850 In this case, the updatedItem 47 00:02:27,240 --> 00:02:29,900 will be set equal to a new object 48 00:02:29,900 --> 00:02:32,610 where I copy the existingCartItem 49 00:02:32,610 --> 00:02:34,760 but where I update the amount 50 00:02:34,760 --> 00:02:37,910 because the amount now needs to change. 51 00:02:37,910 --> 00:02:40,650 If sushi was already part of the cart 52 00:02:40,650 --> 00:02:43,450 and we added two more sushis, 53 00:02:43,450 --> 00:02:45,550 then, of course, we need to update the amount 54 00:02:45,550 --> 00:02:48,593 of that existing sushi which is part of the cart. 55 00:02:49,680 --> 00:02:50,822 So here I then access 56 00:02:50,822 --> 00:02:54,840 existingCartItem.amount + action.item.amount, 57 00:02:54,840 --> 00:02:57,393 so the amount which was added by this action. 58 00:02:59,340 --> 00:03:03,680 And updatedItems is now equal to a new array 59 00:03:03,680 --> 00:03:06,600 where I copy the existing items 60 00:03:06,600 --> 00:03:08,970 so that I update this immutably 61 00:03:08,970 --> 00:03:12,300 without editing the old array in memory, 62 00:03:12,300 --> 00:03:16,870 hence I'm creating a new array where I copy the old objects. 63 00:03:16,870 --> 00:03:21,870 And then for that existingCartItemIndex, 64 00:03:22,520 --> 00:03:26,290 I overwrite this with the updatedItem. 65 00:03:26,290 --> 00:03:28,700 So I pick the old item, 66 00:03:28,700 --> 00:03:31,360 which we identified in the cart items array, 67 00:03:31,360 --> 00:03:34,793 and I overwrite it with this updatedItem. 68 00:03:35,960 --> 00:03:36,830 Now, that's what I do 69 00:03:36,830 --> 00:03:40,563 if a item is already part of the cart items array. 70 00:03:41,650 --> 00:03:43,190 Now, we also have the else case 71 00:03:43,190 --> 00:03:45,610 that a item is added for the first time 72 00:03:45,610 --> 00:03:47,900 to that cart items array. 73 00:03:47,900 --> 00:03:49,500 In that case, 74 00:03:49,500 --> 00:03:54,110 the updatedItem is simply a brand new item 75 00:03:54,110 --> 00:03:56,283 where I copy my action.item, 76 00:03:57,320 --> 00:03:59,433 and now updatedItems 77 00:04:00,580 --> 00:04:03,530 is simply set to state.items.concat, 78 00:04:03,530 --> 00:04:07,250 and then here I add this updatedItem. 79 00:04:07,250 --> 00:04:12,250 Or we keep this simpler and we keep on adding action.item, 80 00:04:12,330 --> 00:04:16,589 we don't set updatedItem here and remove updatedItem 81 00:04:18,250 --> 00:04:21,990 in here, therefore, and create this on the fly as a constant 82 00:04:21,990 --> 00:04:24,460 because now we only use updatedItem 83 00:04:24,460 --> 00:04:27,280 in that first part of the if block. 84 00:04:27,280 --> 00:04:29,833 And our else block is very lean now. 85 00:04:30,830 --> 00:04:34,210 With that, we set updatedItems to different things 86 00:04:34,210 --> 00:04:36,540 depending on whether the item is already part 87 00:04:36,540 --> 00:04:37,740 of the array or not. 88 00:04:37,740 --> 00:04:41,570 And either way, we then return our new state snapshot 89 00:04:41,570 --> 00:04:43,817 which picks up these updatedItems. 90 00:04:45,240 --> 00:04:47,203 I hope this logic makes sense. 91 00:04:48,190 --> 00:04:51,960 And with that, if we now reload, I can add sushi, 92 00:04:51,960 --> 00:04:53,600 and if I add it multiple times, 93 00:04:53,600 --> 00:04:56,230 and if I add four sushi at once, 94 00:04:56,230 --> 00:04:58,200 you see we only have one entry here 95 00:04:58,200 --> 00:05:00,620 with the appropriate amount, 96 00:05:00,620 --> 00:05:02,203 and that's exactly what I want. 97 00:05:03,770 --> 00:05:05,390 Now, if I add a second item, 98 00:05:05,390 --> 00:05:07,580 this is added as a new entry, of course, 99 00:05:07,580 --> 00:05:09,460 because it's a different item. 100 00:05:09,460 --> 00:05:13,490 But then, again, if I add more items of that, 101 00:05:13,490 --> 00:05:15,373 it just increases the counter. 102 00:05:16,500 --> 00:05:18,520 So, that is working. 103 00:05:18,520 --> 00:05:21,710 Now, let's make sure that the plus and minus buttons 104 00:05:21,710 --> 00:05:24,200 here in the cart also work. 105 00:05:24,200 --> 00:05:26,020 And for that, we also need to make sure 106 00:05:26,020 --> 00:05:27,683 that we can remove items. 8131

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