All language subtitles for 012 Getting Started with Fetching Data_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
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
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
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
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-PT Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
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,040 --> 00:00:04,180 Now what we learned about funks 2 00:00:04,180 --> 00:00:06,450 and funk action creators, 3 00:00:06,450 --> 00:00:08,860 so these action creator functions here. 4 00:00:08,860 --> 00:00:10,930 Now, did we learn about that, 5 00:00:10,930 --> 00:00:14,770 let's build a app action creator that fetches the cart 6 00:00:14,770 --> 00:00:17,150 when the application loads. 7 00:00:17,150 --> 00:00:19,880 Because at the moment we're only sending data 8 00:00:19,880 --> 00:00:22,830 but we never fetched data when the application load. 9 00:00:22,830 --> 00:00:27,610 And therefore, if we reload, all our state is still lost 10 00:00:27,610 --> 00:00:29,790 and that's of course not the goal. 11 00:00:29,790 --> 00:00:32,259 Now we can ride that code in the component 12 00:00:32,259 --> 00:00:33,840 or as action creator, 13 00:00:33,840 --> 00:00:36,840 but here I now wanna stick to action creators 14 00:00:36,840 --> 00:00:40,010 and hence we can add a second function cart slice. 15 00:00:40,010 --> 00:00:42,750 But since this file is now getting bigger and bigger, 16 00:00:42,750 --> 00:00:45,810 I'm a fan of creating a separate file for that. 17 00:00:45,810 --> 00:00:48,930 Let's maybe name it cart-actions.js, 18 00:00:48,930 --> 00:00:50,830 of course, the file name is up to you. 19 00:00:52,070 --> 00:00:55,590 I'll then copy this sendCartData function, 20 00:00:55,590 --> 00:00:57,780 cut it from cart-slice 21 00:00:57,780 --> 00:01:01,140 and added it cart-actions 22 00:01:01,140 --> 00:01:04,540 and now we need to import UI actions here. 23 00:01:04,540 --> 00:01:08,470 So in cart-slice, I'll get rid of that import, cut that 24 00:01:08,470 --> 00:01:11,720 and add that in cart-actions instead. 25 00:01:11,720 --> 00:01:13,880 And in app.js, we therefore now need 26 00:01:13,880 --> 00:01:15,030 to import sendCartsData 27 00:01:16,200 --> 00:01:18,653 from store/cart-actions. 28 00:01:19,650 --> 00:01:22,210 And then I'll add my app action creator function 29 00:01:22,210 --> 00:01:25,070 in this cart-actions file as well. 30 00:01:25,070 --> 00:01:28,080 So that could be the fetctCardData function 31 00:01:29,160 --> 00:01:30,610 and I'll also export that 32 00:01:30,610 --> 00:01:33,080 because we'll need it somewhere else as well. 33 00:01:33,080 --> 00:01:36,900 And just as before that will immediately return a function 34 00:01:36,900 --> 00:01:39,080 which gets dispatched as an argument 35 00:01:39,080 --> 00:01:41,330 and then does something else. 36 00:01:41,330 --> 00:01:45,080 Now here we could then also dispatch some notifications 37 00:01:45,080 --> 00:01:46,520 if we want to do that, 38 00:01:46,520 --> 00:01:48,700 but actually here I'll not do that. 39 00:01:48,700 --> 00:01:51,480 Instead I'll create a new function, fetchData, 40 00:01:55,090 --> 00:01:57,710 and have nested function which is async 41 00:01:57,710 --> 00:01:59,560 because I'll use the fetch API 42 00:01:59,560 --> 00:02:03,150 and I wanna wrap a fetchData with try catch it after 43 00:02:03,150 --> 00:02:06,130 that's why I'm putting it into a separate function. 44 00:02:06,130 --> 00:02:09,539 So then we await fetch action get a response here 45 00:02:09,539 --> 00:02:12,010 and wanna fetch by sending a request 46 00:02:12,010 --> 00:02:14,210 to that same URL again 47 00:02:15,140 --> 00:02:17,510 but this time it should be a get request. 48 00:02:17,510 --> 00:02:20,670 So we don't need to add this configuration object then 49 00:02:20,670 --> 00:02:22,380 as a second parameter, 50 00:02:22,380 --> 00:02:25,113 because I get request is the default, anyways. 51 00:02:26,880 --> 00:02:30,530 Instead this time I am now interested in the data, 52 00:02:30,530 --> 00:02:32,890 so in the result of calling await 53 00:02:33,890 --> 00:02:35,840 response.Json 54 00:02:35,840 --> 00:02:37,540 but of course, I also still wanna check 55 00:02:37,540 --> 00:02:40,900 if the response is maybe not okay for whatever reason 56 00:02:40,900 --> 00:02:43,740 and if that should be the case I'll throw a new error 57 00:02:44,630 --> 00:02:48,163 where I say, "could not fetch cart data" 58 00:02:49,260 --> 00:02:51,463 something like this. 59 00:02:52,930 --> 00:02:56,240 Now, if we make it pass this line of code here though, 60 00:02:56,240 --> 00:02:58,310 we have the data 61 00:02:58,310 --> 00:03:00,870 and I will then return it here, 62 00:03:00,870 --> 00:03:02,310 I'll return it here because 63 00:03:02,310 --> 00:03:05,150 that is not a separate nested function. 64 00:03:05,150 --> 00:03:08,743 Hence here I'll then try executing fetchData 65 00:03:09,850 --> 00:03:12,893 and catch any errors we might be getting. 66 00:03:14,720 --> 00:03:15,780 And if we get an error, 67 00:03:15,780 --> 00:03:18,760 we still might wanna show the error in notification. 68 00:03:18,760 --> 00:03:23,330 So I'll then dispatch this error notification action again 69 00:03:23,330 --> 00:03:24,193 down here, 70 00:03:25,060 --> 00:03:27,567 but I'll say, "fetching carts data failed." 71 00:03:28,660 --> 00:03:33,007 If we however, are the try block or await fetchData, 72 00:03:33,920 --> 00:03:35,760 I can do this 73 00:03:35,760 --> 00:03:38,893 if I turned this into an async function, which we can, 74 00:03:40,050 --> 00:03:41,630 Redux supports that, 75 00:03:41,630 --> 00:03:44,980 that dysfunction which we returned here is async. 76 00:03:44,980 --> 00:03:46,780 So we can use await here as well 77 00:03:46,780 --> 00:03:50,700 and that gives me my cart data then 78 00:03:50,700 --> 00:03:53,727 because I'm returning the data which we pass here 79 00:03:55,034 --> 00:03:58,950 and there for now I wanna use that cart data to set my cart. 80 00:03:59,890 --> 00:04:01,810 Now, the cart data we're fetching, 81 00:04:01,810 --> 00:04:05,350 we'll have that format which is stored on Firebase 82 00:04:05,350 --> 00:04:07,100 and that's the format which we're sending 83 00:04:07,100 --> 00:04:08,910 to Firebase of course. 84 00:04:08,910 --> 00:04:11,870 It will be this cart, which we're sending there 85 00:04:11,870 --> 00:04:14,050 so it is an object with the items key, 86 00:04:14,050 --> 00:04:17,540 with in an array inside and a total quantity key. 87 00:04:17,540 --> 00:04:20,089 Which is exactly the format we need here 88 00:04:20,089 --> 00:04:22,580 in our front-end as well. 89 00:04:22,580 --> 00:04:25,000 Which is no surprise because we are sending 90 00:04:25,000 --> 00:04:27,060 that Redux state snapshot 91 00:04:27,060 --> 00:04:29,610 as data to the back and at the end. 92 00:04:29,610 --> 00:04:33,330 So we automatically get back correctly formatted data here 93 00:04:33,330 --> 00:04:37,030 and we don't need to transform that Firebase data first 94 00:04:37,030 --> 00:04:41,330 because it has the format, we sent to Firebase earlier. 95 00:04:41,330 --> 00:04:44,476 That's different from other sections in the course, 96 00:04:44,476 --> 00:04:47,590 here we had to transform Firebase data 97 00:04:47,590 --> 00:04:51,970 because there we used post for a sending our data 98 00:04:51,970 --> 00:04:53,640 not put as we're doing here 99 00:04:53,640 --> 00:04:56,850 and hence we let Firebase create a list of data 100 00:04:56,850 --> 00:05:00,010 which turned out to be an object when we fetched it. 101 00:05:00,010 --> 00:05:03,620 You might remember that from the HTTP section in the course 102 00:05:03,620 --> 00:05:08,040 here with put we're sending our data snapshots to Firebase 103 00:05:08,040 --> 00:05:11,450 and Firebase will take it as it is and store it like it is 104 00:05:11,450 --> 00:05:12,830 without changing it. 105 00:05:12,830 --> 00:05:16,460 So when we then fetch it, we also have to correct structure 106 00:05:16,460 --> 00:05:19,720 and hence this cart data is correctly formatted 107 00:05:19,720 --> 00:05:24,720 and therefore now, we can use this replace cart reducer, 108 00:05:24,730 --> 00:05:28,030 which I provided to you earlier in this module 109 00:05:28,030 --> 00:05:30,280 to replace our front-end cart 110 00:05:30,280 --> 00:05:32,770 with the cart we're loading from Firebase 111 00:05:33,610 --> 00:05:35,680 and replace cart expects a payload 112 00:05:35,680 --> 00:05:37,280 which has a total quantity 113 00:05:37,280 --> 00:05:40,540 and an items field which is exactly the data structure 114 00:05:40,540 --> 00:05:42,750 we're fetching from Firebase. 115 00:05:42,750 --> 00:05:47,750 So, in cart-actions, we just wanna import 116 00:05:47,840 --> 00:05:50,620 our cart-actions 117 00:05:51,820 --> 00:05:54,500 from the cart slice, 118 00:05:54,500 --> 00:05:57,633 so these automatically generated actions now. 119 00:05:59,480 --> 00:06:04,480 And here, I then wanna dispatch cart-actions replace, 120 00:06:04,920 --> 00:06:07,580 not remove, replace cart 121 00:06:07,580 --> 00:06:10,280 and pass my carts data as a payload, 122 00:06:10,280 --> 00:06:13,563 which as mentioned has the correct structure already. 123 00:06:14,850 --> 00:06:17,410 Now we could have done all to show to success notification 124 00:06:17,410 --> 00:06:20,470 but I don't wanna do that, I just wanna use the cart data 125 00:06:20,470 --> 00:06:21,963 and we're good to go. 126 00:06:23,120 --> 00:06:27,820 With all that done, we now just need to call fetch cartData 127 00:06:27,820 --> 00:06:30,090 somewhere in our application 128 00:06:30,090 --> 00:06:33,950 to start fetching our cartData from Firebase. 129 00:06:33,950 --> 00:06:37,560 And again, app.js is probably a good place for that. 130 00:06:37,560 --> 00:06:40,500 Here we already have a user fact call 131 00:06:40,500 --> 00:06:43,270 and that we wanna start fetching data here 132 00:06:43,270 --> 00:06:45,680 if this application loads. 133 00:06:45,680 --> 00:06:48,530 We could do that here in this isInitial check 134 00:06:48,530 --> 00:06:51,000 because dad will only run if it's the first time 135 00:06:51,000 --> 00:06:52,470 that this effect runs. 136 00:06:52,470 --> 00:06:55,240 But I think it's cleaner to create a separate effect 137 00:06:55,240 --> 00:06:57,710 which simply doesn't have any dependencies 138 00:06:57,710 --> 00:06:59,910 and which therefore all they will only run 139 00:06:59,910 --> 00:07:04,500 if this component was rendered for the first time 140 00:07:04,500 --> 00:07:06,400 and since it's our main component 141 00:07:06,400 --> 00:07:08,230 this will only render once. 142 00:07:08,230 --> 00:07:12,030 And therefore here we can then dispatch 143 00:07:12,030 --> 00:07:15,950 this new cart-action created, this custom cart-action. 144 00:07:15,950 --> 00:07:19,060 So this fetchcartData action, 145 00:07:19,060 --> 00:07:20,950 I wanna dispatch this here 146 00:07:21,920 --> 00:07:25,630 and dispatch as a dependency for completeness sake 147 00:07:25,630 --> 00:07:27,800 but as I said, that will never change, 148 00:07:27,800 --> 00:07:30,030 so this effect will never rerun 149 00:07:30,030 --> 00:07:33,970 and that will dispatch this whenever our application starts. 150 00:07:33,970 --> 00:07:37,070 Hence if you saved this and reload you'll see 151 00:07:37,070 --> 00:07:39,140 that this gets populated. 152 00:07:39,140 --> 00:07:41,710 But you'll also notice that for some reason 153 00:07:41,710 --> 00:07:44,940 we also see sent cart data successfully here 154 00:07:46,100 --> 00:07:48,900 and we are sending a request to Firebase 155 00:07:48,900 --> 00:07:52,020 for storing the cart data again. 156 00:07:52,020 --> 00:07:54,130 So we're fetching it, as we can tell 157 00:07:54,130 --> 00:07:55,900 we have our cart data here 158 00:07:55,900 --> 00:07:58,360 and if I change it and reload, 159 00:07:58,360 --> 00:08:00,850 we see we fetch that change to data, 160 00:08:00,850 --> 00:08:03,650 but at the same time we seem to be sending it again. 161 00:08:03,650 --> 00:08:05,263 And do you know why? 162 00:08:06,350 --> 00:08:08,990 we are sending it again because in app.js 163 00:08:08,990 --> 00:08:11,780 we have this effect for sending the cart data 164 00:08:11,780 --> 00:08:14,940 which we'll execute whenever the carts changed. 165 00:08:14,940 --> 00:08:17,880 The problem is when we fetched the carts data, 166 00:08:17,880 --> 00:08:21,840 once fetching is done, we replaced a cart, right? 167 00:08:21,840 --> 00:08:23,230 We replaced a cart 168 00:08:24,110 --> 00:08:26,530 here with that fetched data, 169 00:08:26,530 --> 00:08:29,890 that changes the cart inside of Redux. 170 00:08:29,890 --> 00:08:32,049 Now, since that cart then changed 171 00:08:32,049 --> 00:08:36,110 we triggered this app effect and send the cart data again. 172 00:08:36,110 --> 00:08:37,883 So how can we now work around that? 13587

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