1 00:00:01,120 --> 00:00:05,600 In this video we're going to start working on a second way of solving our overstretching issue. 2 00:00:05,620 --> 00:00:07,750 So minimalizing definitely worked pretty well. 3 00:00:07,750 --> 00:00:12,820 But like I said the downside is that we now can now call this action Creator only one time with each 4 00:00:12,880 --> 00:00:18,520 unique user ID and that effectively means that we can only fetch each user one time inside of application. 5 00:00:18,520 --> 00:00:23,080 Now it's incredibly likely that you will eventually work on an application where you are going to want 6 00:00:23,080 --> 00:00:26,050 to re fetch different resources over time. 7 00:00:26,290 --> 00:00:31,080 So this video we're going to take a look at a different way of solving this over fetching problem. 8 00:00:31,090 --> 00:00:36,430 Now the second way that we're going to solve this is going to be a little bit more obvious in nature. 9 00:00:36,430 --> 00:00:37,480 So here's the idea. 10 00:00:37,480 --> 00:00:43,060 We're going to create a new action creator called fecche posts and users and that's going to do exactly 11 00:00:43,060 --> 00:00:44,010 what you would think. 12 00:00:44,260 --> 00:00:47,120 There's just one little twist to it. 13 00:00:47,140 --> 00:00:51,450 So when ever we call fecche posts end users this is the logic that we're going to execute. 14 00:00:51,580 --> 00:00:54,250 We're going to first call fecche posts. 15 00:00:54,250 --> 00:00:58,390 We're then going to get a list of all of our posts from that list of posts. 16 00:00:58,430 --> 00:01:01,340 We'll find all of our unique user IDs. 17 00:01:01,390 --> 00:01:04,230 We will then iterate over all those unique user IDs. 18 00:01:04,450 --> 00:01:09,910 And then finally we're going to call that user which with each of those now just a second ago I mentioned 19 00:01:09,910 --> 00:01:14,930 that there is a little twist here and the twist is that we are not going to replace the fecche posts 20 00:01:14,980 --> 00:01:18,510 action creator and the fetch user action creator. 21 00:01:18,520 --> 00:01:23,380 So in other words fecche posts end users is not going to have some logic inside of it to make a network 22 00:01:23,380 --> 00:01:25,210 request over to our API. 23 00:01:25,510 --> 00:01:30,430 Instead we're going to keep all that network because logic inside of our two existing action creators 24 00:01:30,610 --> 00:01:32,920 fetch posts and fetch user. 25 00:01:32,980 --> 00:01:37,870 The idea here is that when you start working on your own projects you want to create action creators 26 00:01:37,870 --> 00:01:40,830 that are a small and compact as possible. 27 00:01:40,840 --> 00:01:41,800 Just think about it. 28 00:01:41,920 --> 00:01:47,860 If we made an application and the only action creator inside of it was fecche posts and users then maybe 29 00:01:47,860 --> 00:01:51,180 for our app as it stands right now we would be in a pretty good spot. 30 00:01:51,430 --> 00:01:56,440 But maybe at some point in time in the future we would want to put together some like maybe user profile 31 00:01:56,440 --> 00:02:02,380 page and in time that the user went to that profile page we would want to show details about some very 32 00:02:02,380 --> 00:02:04,090 particular user. 33 00:02:04,090 --> 00:02:09,670 In that case we would want to have an action creator that fetch just one user so we can get details 34 00:02:09,670 --> 00:02:12,570 about that particular user and show them on the screen. 35 00:02:12,890 --> 00:02:18,550 So when you start putting together these kind of big combo action creators that do multiple things. 36 00:02:18,550 --> 00:02:24,190 A very good approach is to first create some action creators that do very small things and essentially 37 00:02:24,190 --> 00:02:29,770 kind of wired them altogether into the more mega action creator so to speak that does everything for 38 00:02:29,770 --> 00:02:30,350 you. 39 00:02:30,770 --> 00:02:35,620 OK so with that in mind because we are going to reuse these action creators it's going to make life 40 00:02:35,620 --> 00:02:37,110 just a little bit challenging. 41 00:02:37,210 --> 00:02:40,300 But I'm very confident that you'll understand exactly what is going on. 42 00:02:40,600 --> 00:02:44,800 All right so we're going to flip back over to our code editor I'm still inside of my action creators 43 00:02:44,800 --> 00:02:45,640 file. 44 00:02:45,670 --> 00:02:51,620 The first thing we're going to do is refactor our Fettes user action creator back to its previous state. 45 00:02:51,640 --> 00:02:54,520 So back to the pre memorized version of it. 46 00:02:54,610 --> 00:03:00,710 Now before we do that refactor I'm going to copy it down underneath itself and I'll comment out that 47 00:03:00,710 --> 00:03:01,720 copy right there. 48 00:03:01,970 --> 00:03:05,750 So the reason we're making a little uncommented version right there is just you have a reference of 49 00:03:05,750 --> 00:03:08,470 the memorized version of this approach. 50 00:03:08,590 --> 00:03:13,480 So we're not going to refactor fetch user back into the non memorized version. 51 00:03:13,490 --> 00:03:15,100 This will be a pretty easy refactor. 52 00:03:15,110 --> 00:03:19,150 You can either hit undo a whole bunch or we can just do the re factor together right now. 53 00:03:19,160 --> 00:03:20,680 Honestly it won't be that bad. 54 00:03:20,900 --> 00:03:25,250 It's the first 10 going to do to return this to its previous state is going to be to remove the entire 55 00:03:25,250 --> 00:03:28,180 line that says Konst underscore Fettes user. 56 00:03:28,180 --> 00:03:30,450 So I'm going to delete all that. 57 00:03:30,470 --> 00:03:34,820 Next up I'm going to remove everything from the semi-colon right here all the way to underscore Fettes 58 00:03:34,820 --> 00:03:39,570 user and I will replace it with a curly brace like so. 59 00:03:40,710 --> 00:03:43,590 I will then mark the inner function that we return right here. 60 00:03:43,640 --> 00:03:49,700 As async And then finally I'm going to delete the closing parentheses at the very end. 61 00:03:50,550 --> 00:03:51,750 And that's it. 62 00:03:51,750 --> 00:03:57,120 So now we should be able to save this and then flip back over to application and not see any error messages 63 00:03:57,120 --> 00:03:58,360 or anything like that. 64 00:03:58,740 --> 00:03:59,820 So I am back here. 65 00:03:59,820 --> 00:04:01,890 I do not see any error messages. 66 00:04:01,890 --> 00:04:05,730 If you see an error message that means that you made a little typo during the refactor and you'll want 67 00:04:05,730 --> 00:04:07,350 to fix that up really quickly. 68 00:04:07,620 --> 00:04:12,990 Now you'll notice that because we are now working with the non minimalized version of our action creator 69 00:04:13,140 --> 00:04:16,670 we're back to our original state where we are making a ton of requests. 70 00:04:16,710 --> 00:04:18,210 All right let's take a quick pause right here. 71 00:04:18,240 --> 00:04:23,640 When we come back the next section we're going to start creating the fecche posts and users action creator.