Are finally ready to save our new employee to firebase Let's jump right into it inside of our employee
create action Creator and we want to see something to firebase any time we want to work with firebase
in any way shape or form.
We need to make sure that we import the firebase library.
So at the top of this file I'm going to add an import statement to import import firebase from firebase
like so.
Now here comes the fun part the fun part so to speak.
I'm going to clean up this consulate right here.
We know the actual crater is being called with the correct data.
So I don't think we really need anymore.
This next part like I said quite fun with firebase depending on how you look at it.
We're going to throw a little bit of code on here again.
Talk about what it's doing.
I'm going to call firebase dot database dot ref.
I'm going to pass in a string user's user id employees.
So there's a little bit of interesting code right here.
This says good access to our firebase database.
Right.
So like the location where you actually store our data and then make a reference to user's user id employees.
So this string right here is a little bit misleading because it looks like it might be a U or L or something
like that right.
Well in fact it is a path to our Jason data store as I passed through the different gates on that we
have.
So remember some data structure we had said that we would have a top level collection of users and then
a user ID and then a collection of employees.
And so that is exactly what the string is doing right here it is saying find my collection of users
like find a key users find a key of user ID and then find a key of employees.
Now the one kind of a misleading part here is I put just a hard coded user id in here.
We don't actually want this to be a hard coded user id want this to be the user id of the currently
authenticated user like that currently logged in user.
That is what's going to give us back over our days on schema.
This intermediate key right here of like User 1 2 3 we're going to use their ID as this intermediate
key right here.
Again if this like data structure here sounds really confusing as soon as we see this inside firebase
itself I think it's going to make a lot more sense.
So let's finish up with this actually saving the data here.
Check it out.
Inside a firebase.
And I think it's going to start to make a lot more sense.
So we have to actually get used to or get access to the currently authenticated user in our application
because we need their ID to shove in right here as the user ID to get access to the currently authenticated
user.
We can call can get our current user from firebase start off like so.
So if we reference firebase doth dot current user thats the currently authenticated user is our user
model and this current user object right here has a new ID property which is the idea of that current
user which is exactly what we need.
So we need to take that current users use ID or essentially their ID and stuff it in in place of this
user id variable right here to do that.
We're going to use a little bit of template strings or string interpolation depending what you want
to call it which is a feature of ESX.
So I'm going to find my single quotes and either side the string I'm going to delete it on both sides
and I'm going to replace them with back ticks.
So back ticks are a button on your keyboard that is to the left of the one.
So you're going to want to end up at the back tick right here.
Then we want to inject that ID in place of this user ID right here.
So I'm going to remove user ID and then replace it with dollar sign curly brace.
And again this is a part of the A-6 any javascript variable that we placed inside of here is going to
be injected into this string and we're gonna end up with like the fully qualified string of users slash
user id whatever it might be like user 1 to 3 Flash employees.
So inside if you're put current user ID.
Boom that's it.
So just to be really clear the final product of that string right there would end up being something
that looks like you know users.
One two three four five six slash employees like that.
That is the final result of that string.
Look at after we get a reference to this path right here on firebase this is just like a reference to
a very specific location in our data store.
We want to actually take some operation on that reference.
We want to actually like do something at this point in time to add new data to this little slice of
our firebase data store.
We can call the DOT push method on that reference so don't push.
We can pass it an object and then anything inside of here will be saved to that reference or to this
little spot inside our database.
So we want to push inside of there a name the phone number and the shift.
And again I think that we would probably be really well-served to test this out at this point see what
it looks like inside of our firebase console.
I think it's really going to help us to figure out what's going on here.
So I'm going to open up my simulator and going to refresh
log in
go.
And now we'll go to add.
I'm going to set Jane 5 5 5 5 5 5.
We can set a shift or we can just leave a default Monday that should definitely be functioning now and
then I'll go ahead and click Create.
So we still get the big red error message on here.
Actions must be plain objects.
Again that is because we are not treating this like a proper action Creator and we are not returning
actual action here but we should have at least made this request over to firebase.
So now I should really check my console and see if we've got any data so I'll flip back over my console
and immediately it looks like I've already gotten an update here so I can now see on my root level I've
now got a collection of users.
If I expand it this is my current user ID.
So this would be like in my diagram that I had here this would be equivalent to like you know User 1
to 3.
This is the ID of the currently logged in user.
So I can expand this that user has a collection of employees and inside of it there is an employee with
ID dash Katie T-P blah blah blah and their name is Jane with the phone number 555 in a shift of Monday.
Yes that is exactly what we want it perfect.
This is great progress.
Just to be really clear now.
Hopefully it makes a little bit more sense.
What's going on with those rules that we have added so this user ID right here.
This is the currently authenticated user.
So the rules that we set up means if any user logs in with out an ID of engaged and blah blah blah they
should not have access to any of the data sitting inside of this little bucket right here.
So only in the user with ngi web law the law is going to have access to this collection of employees
and any other data that we add in here as well.
All right.
This is great progress.
Let's take a quick break and then it looks like we've still got this big red error message here.
We still need to make sure that we navigate our user back to the employee screen.
So let's take care of that in the next section