1 00:00:02,200 --> 00:00:07,590 We're adding tokens everywhere which is important but for orders, we don't just want to append the token, 2 00:00:07,600 --> 00:00:11,800 we also want to make sure that orders really belong to the logged in user. 3 00:00:11,800 --> 00:00:17,080 Now the good thing is we're storing the userId which Firebase generated in our Redux store. 4 00:00:17,080 --> 00:00:21,480 So mapping our orders to users isn't too hard, 5 00:00:21,520 --> 00:00:29,140 we get access to our full store here, so we can get the userId by calling getState.auth.userId, 6 00:00:29,140 --> 00:00:33,270 that's all, this gives us the userId of the currently logged in user 7 00:00:33,340 --> 00:00:40,030 and now we can inject this here and replace the hardcoded U1 with userId. 8 00:00:40,030 --> 00:00:46,840 Of course not just for adding orders but also for fetching orders because we want to fetch the orders 9 00:00:46,900 --> 00:00:49,060 of the currently logged in user. 10 00:00:49,060 --> 00:00:55,540 So here, let's also receive that second argument, get our userId and here exchange single quotes 11 00:00:55,540 --> 00:01:03,640 for back ticks so that we can use this dynamic convenient injection here and then replace or interpolate 12 00:01:03,640 --> 00:01:10,940 as it's called this string here by adding the userId. So this now also makes sure that the orders 13 00:01:10,940 --> 00:01:11,880 are stored correctly 14 00:01:11,900 --> 00:01:18,160 but of course it's not just the orders, the products also should be stored such. We should make sure that 15 00:01:18,160 --> 00:01:23,000 when we create a product, we actually do add our owner ID to it, 16 00:01:23,020 --> 00:01:26,960 that's something we're currently not doing and that's something which we should do. 17 00:01:27,100 --> 00:01:33,200 So here, I actually also want to get the userId here in create product 18 00:01:33,370 --> 00:01:39,380 and when I then send my request to create a new product, I don't just want to set title, description and so 19 00:01:39,400 --> 00:01:39,920 on, 20 00:01:39,970 --> 00:01:48,130 I also want to set an owner ID field and the exact name is up to you, which is equal to the userId 21 00:01:48,130 --> 00:01:49,720 of the logged in user 22 00:01:49,720 --> 00:01:51,610 and that's now something brand new, 23 00:01:51,610 --> 00:01:59,560 now we really start mapping products to a user, something we only did in a dummy way before. This also 24 00:01:59,560 --> 00:02:05,980 means by the way that when we dispatch create product here, I want to pass a new piece of information, 25 00:02:06,490 --> 00:02:11,520 I want to add the owner ID there as well and also point at the userId here 26 00:02:11,890 --> 00:02:16,310 and that means that in the products reducer, we need to respect this 27 00:02:16,540 --> 00:02:24,130 and in there, instead of hardcoding this ID here, we can now get this from our action.productData, 28 00:02:24,580 --> 00:02:28,200 from owner ID there because that's what I just added there. 29 00:02:28,210 --> 00:02:33,990 So here products, I added owner ID, I added this key to product data, to the action I dispatch, 30 00:02:34,090 --> 00:02:39,910 now in the reducer, we have to use that, extract it here and use that as an owner ID of the product we 31 00:02:39,910 --> 00:02:42,230 create locally in Redux. 32 00:02:42,340 --> 00:02:49,780 Of course this also means that here, when I get the products that belong to that user, I also want 33 00:02:49,780 --> 00:02:53,190 to make sure that I filter correctly for the correct ID. 34 00:02:53,800 --> 00:02:58,960 So when we call set products here, I want to make sure that user products are really filtered down to 35 00:02:58,960 --> 00:03:07,540 the logged in user, so in the products action creator, when we fetch all products, here of course I do want 36 00:03:07,540 --> 00:03:16,190 to fetch all products but there, I also want to be able to get my state so that I can get my userId 37 00:03:16,400 --> 00:03:21,920 from the state because then here when we create the loaded products, I don't just want to forward those, 38 00:03:21,950 --> 00:03:28,350 I also want to add a new user products key to my action object where I take the loaded products and 39 00:03:28,400 --> 00:03:37,590 here I filter them and I have a look at each product and if the owner ID of the product, if that is 40 00:03:37,650 --> 00:03:43,590 equal to the userId, then I want to keep it because then it's a product created by the currently logged 41 00:03:43,590 --> 00:03:44,290 in user, 42 00:03:44,340 --> 00:03:45,840 otherwise I'll not keep it. 43 00:03:45,840 --> 00:03:51,040 It's still part of the loaded product which I pass to product but not part of the products I passed 44 00:03:51,040 --> 00:03:52,510 to user products 45 00:03:52,860 --> 00:03:58,170 and that allows me to go back to the reducer and there in set products, user products is initialized 46 00:03:58,200 --> 00:04:04,860 not by filtering here but by setting it equal to action user products which now takes the filtered products 47 00:04:05,160 --> 00:04:12,290 filtered in the action creator with the logic we just wrote where we take the userId. This make sure 48 00:04:12,560 --> 00:04:18,260 that we only see the products we really created and we don't see the product of other users. 49 00:04:18,270 --> 00:04:25,310 Now of course by the way, we can now also make sure that in our products store here, we don't start with 50 00:04:25,310 --> 00:04:28,460 our dummy products but we actually start with an empty array 51 00:04:28,460 --> 00:04:37,440 in both cases here. This also means of course that now if we save this and we login, we should 52 00:04:37,440 --> 00:04:41,230 be able to view our previous product, 53 00:04:41,310 --> 00:04:42,020 here it is 54 00:04:42,030 --> 00:04:45,010 but if I go to admin, we don't see it there and 55 00:04:45,090 --> 00:04:50,340 that makes a lot of sense because this product was created earlier in the course and therefore it has 56 00:04:50,340 --> 00:04:58,650 no owner ID. This product has no owner ID, so therefore it's not included in any users product which 57 00:04:58,650 --> 00:05:01,980 is great because now I have a nice example that there are no products yet, 58 00:05:02,010 --> 00:05:04,190 maybe we should also show a message here. 59 00:05:04,320 --> 00:05:07,050 As a side note in orders we also don't see anything 60 00:05:07,050 --> 00:05:12,720 and maybe we should show a message here as well because orders for U1, that's now not a valid user 61 00:05:12,720 --> 00:05:13,450 ID anymore, 62 00:05:13,470 --> 00:05:18,680 so these orders are never loaded for any user and that's good because that's how it should be. 63 00:05:18,680 --> 00:05:23,840 So now with that, I'll just tweak these pages a little bit to show a message if we can't load any data. 64 00:05:23,870 --> 00:05:32,620 So here in the user product screen, if user products length is equal to zero, then I don't want to return 65 00:05:32,620 --> 00:05:41,090 my flat list but instead I'll return a view here where I have a text where I say no products found, maybe 66 00:05:41,110 --> 00:05:46,810 start creating some and we'll need to set up some styles there, 67 00:05:46,820 --> 00:05:49,520 we also need to import the view and text 68 00:05:49,520 --> 00:05:51,460 otherwise we can't display this 69 00:05:51,680 --> 00:05:59,060 and now I'll just do some quick and dirty inline styling here with flex one, justify content center and 70 00:05:59,090 --> 00:06:04,340 align items center, of course you could also assign your own font here to the text and so on, 71 00:06:04,340 --> 00:06:09,070 I'll just do it like this and then copy this and move it over to the orders screen, 72 00:06:09,170 --> 00:06:12,060 make sure you inport view and text there as well 73 00:06:12,110 --> 00:06:18,350 and then here we can also add this check, if user products length is empty, of course here we should check 74 00:06:18,410 --> 00:06:24,010 orders length, not user products length but if that is empty, I say no orders found, 75 00:06:24,110 --> 00:06:28,910 maybe start ordering some products like that 76 00:06:28,910 --> 00:06:31,940 and with that, we should be seeing that text if nothing is found. 77 00:06:31,940 --> 00:06:41,090 So if I now try this again and log in real quick and we go to the admin page, we see the text here and if 78 00:06:41,160 --> 00:06:42,630 we go to the orders page, 79 00:06:42,630 --> 00:06:44,540 we also see the text here. 80 00:06:44,640 --> 00:06:46,460 So now it's working as it should. 81 00:06:46,620 --> 00:06:55,470 If I now create a new product on the other hand, for example a scarf let's say with a nice little a URL 82 00:06:55,470 --> 00:07:03,190 which I selected for $55.99, a woollen scarf, 83 00:07:03,210 --> 00:07:12,060 if I do that and submit this, this works fine and it also shows up here under admin, also shows up under 84 00:07:12,060 --> 00:07:16,650 products, we could of course also filter it out there if you wanted to but why not, 85 00:07:16,650 --> 00:07:18,290 why don't we have it here too and 86 00:07:18,380 --> 00:07:26,840 if I add this to the cart and I click order now, now here under orders, we see it and on Firebase, we also 87 00:07:26,840 --> 00:07:32,460 see that under orders now we have a node with our userId with our orders. 88 00:07:32,490 --> 00:07:35,670 So this is now really working in the way it should 89 00:07:35,670 --> 00:07:39,960 here on this application and therefore now here, we have a setup with which we can work.