We're adding tokens everywhere which is important but for orders, we don't just want to append the token,
we also want to make sure that orders really belong to the logged in user.
Now the good thing is we're storing the userId which Firebase generated in our Redux store.
So mapping our orders to users isn't too hard,
we get access to our full store here, so we can get the userId by calling getState.auth.userId,
that's all, this gives us the userId of the currently logged in user
and now we can inject this here and replace the hardcoded U1 with userId.
Of course not just for adding orders but also for fetching orders because we want to fetch the orders
of the currently logged in user.
So here, let's also receive that second argument, get our userId and here exchange single quotes
for back ticks so that we can use this dynamic convenient injection here and then replace or interpolate
as it's called this string here by adding the userId. So this now also makes sure that the orders
are stored correctly
but of course it's not just the orders, the products also should be stored such. We should make sure that
when we create a product, we actually do add our owner ID to it,
that's something we're currently not doing and that's something which we should do.
So here, I actually also want to get the userId here in create product
and when I then send my request to create a new product, I don't just want to set title, description and so
on,
I also want to set an owner ID field and the exact name is up to you, which is equal to the userId
of the logged in user
and that's now something brand new,
now we really start mapping products to a user, something we only did in a dummy way before. This also
means by the way that when we dispatch create product here, I want to pass a new piece of information,
I want to add the owner ID there as well and also point at the userId here
and that means that in the products reducer, we need to respect this
and in there, instead of hardcoding this ID here, we can now get this from our action.productData,
from owner ID there because that's what I just added there.
So here products, I added owner ID, I added this key to product data, to the action I dispatch,
now in the reducer, we have to use that, extract it here and use that as an owner ID of the product we
create locally in Redux.
Of course this also means that here, when I get the products that belong to that user, I also want
to make sure that I filter correctly for the correct ID.
So when we call set products here, I want to make sure that user products are really filtered down to
the logged in user, so in the products action creator, when we fetch all products, here of course I do want
to fetch all products but there, I also want to be able to get my state so that I can get my userId
from the state because then here when we create the loaded products, I don't just want to forward those,
I also want to add a new user products key to my action object where I take the loaded products and
here I filter them and I have a look at each product and if the owner ID of the product, if that is
equal to the userId, then I want to keep it because then it's a product created by the currently logged
in user,
otherwise I'll not keep it.
It's still part of the loaded product which I pass to product but not part of the products I passed
to user products
and that allows me to go back to the reducer and there in set products, user products is initialized
not by filtering here but by setting it equal to action user products which now takes the filtered products
filtered in the action creator with the logic we just wrote where we take the userId. This make sure
that we only see the products we really created and we don't see the product of other users.
Now of course by the way, we can now also make sure that in our products store here, we don't start with
our dummy products but we actually start with an empty array
in both cases here. This also means of course that now if we save this and we login, we should
be able to view our previous product,
here it is
but if I go to admin, we don't see it there and
that makes a lot of sense because this product was created earlier in the course and therefore it has
no owner ID. This product has no owner ID, so therefore it's not included in any users product which
is great because now I have a nice example that there are no products yet,
maybe we should also show a message here.
As a side note in orders we also don't see anything
and maybe we should show a message here as well because orders for U1, that's now not a valid user
ID anymore,
so these orders are never loaded for any user and that's good because that's how it should be.
So now with that, I'll just tweak these pages a little bit to show a message if we can't load any data.
So here in the user product screen, if user products length is equal to zero, then I don't want to return
my flat list but instead I'll return a view here where I have a text where I say no products found, maybe
start creating some and we'll need to set up some styles there,
we also need to import the view and text
otherwise we can't display this
and now I'll just do some quick and dirty inline styling here with flex one, justify content center and
align items center, of course you could also assign your own font here to the text and so on,
I'll just do it like this and then copy this and move it over to the orders screen,
make sure you inport view and text there as well
and then here we can also add this check, if user products length is empty, of course here we should check
orders length, not user products length but if that is empty, I say no orders found,
maybe start ordering some products like that
and with that, we should be seeing that text if nothing is found.
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
we go to the orders page,
we also see the text here.
So now it's working as it should.
If I now create a new product on the other hand, for example a scarf let's say with a nice little a URL
which I selected for $55.99, a woollen scarf,
if I do that and submit this, this works fine and it also shows up here under admin, also shows up under
products, we could of course also filter it out there if you wanted to but why not,
why don't we have it here too and
if I add this to the cart and I click order now, now here under orders, we see it and on Firebase, we also
see that under orders now we have a node with our userId with our orders.
So this is now really working in the way it should
here on this application and therefore now here, we have a setup with which we can work.