Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,210 --> 00:00:04,360
In this lecture, we're going to refactor our code base with actions.
2
00:00:04,380 --> 00:00:10,110
Currently, I'm looking at the register function we created in the register form component.
3
00:00:10,140 --> 00:00:13,410
Two requests are being made inside the function.
4
00:00:13,440 --> 00:00:16,730
One request is for the authentication service.
5
00:00:16,740 --> 00:00:18,630
Another is for the database.
6
00:00:18,630 --> 00:00:22,530
Both requests are necessary for registering the user.
7
00:00:22,620 --> 00:00:28,290
Aside from this, the rest of the function updates the properties for the alert messages.
8
00:00:28,560 --> 00:00:32,970
The code we've written in the function can be broken down into two parts.
9
00:00:33,390 --> 00:00:39,120
There's the logic for manipulating the component and another for manipulating the state.
10
00:00:39,150 --> 00:00:41,150
We should separate this logic.
11
00:00:41,160 --> 00:00:45,360
The code for manipulating the state will be put into an action function.
12
00:00:45,360 --> 00:00:47,910
We'll leave the rest inside the component.
13
00:00:48,180 --> 00:00:50,790
Let's begin using action functions.
14
00:00:50,790 --> 00:00:54,660
Switch to the store file where our state is managed.
15
00:00:56,720 --> 00:01:01,370
Inside the exported object, we're going to add a property called actions.
16
00:01:03,630 --> 00:01:07,740
The action's object is where we can define action functions.
17
00:01:07,800 --> 00:01:12,210
Inside this object, we're going to create our first action function.
18
00:01:12,210 --> 00:01:14,100
We'll call it register.
19
00:01:16,290 --> 00:01:19,380
On this function ad the async keyword.
20
00:01:21,470 --> 00:01:25,170
We're going to perform a synchronous requests inside the function.
21
00:01:25,190 --> 00:01:29,130
In addition, I'm using a regular function, not an arrow function.
22
00:01:29,150 --> 00:01:35,630
The developers of Peni recommend regular functions for one reason the this keyword gives us access to
23
00:01:35,630 --> 00:01:36,440
the state.
24
00:01:36,470 --> 00:01:42,380
If we were to use arrow functions, we would not be able to interact with a state from our actions.
25
00:01:42,410 --> 00:01:48,390
The register function will make a request to the authentication and store services.
26
00:01:48,410 --> 00:01:51,490
We've already written the code for making these requests.
27
00:01:51,500 --> 00:01:57,480
We can copy them over to this function, switch to the register form component file.
28
00:01:57,500 --> 00:02:04,010
The requests are made in the try blocks, cut both pieces of code inside the try blocks.
29
00:02:04,280 --> 00:02:06,050
Paste them over to the register.
30
00:02:06,050 --> 00:02:07,580
Function in the store.
31
00:02:09,780 --> 00:02:13,500
Here's what you should have in the register action function.
32
00:02:13,500 --> 00:02:18,610
We were calling the create user with email and password and add functions.
33
00:02:18,630 --> 00:02:24,060
We're going to remove the assignment to the user credentials variable because we don't need to log it
34
00:02:24,060 --> 00:02:24,840
anymore.
35
00:02:27,120 --> 00:02:33,000
If you're using Visual Studio code like me, you may be receiving errors because the authentication
36
00:02:33,000 --> 00:02:36,220
and users collection objects are not defined.
37
00:02:36,240 --> 00:02:37,950
We'll need to import them.
38
00:02:37,950 --> 00:02:44,430
We can copy the import statements from the register form component, switch back to it, cut and paste
39
00:02:44,430 --> 00:02:47,160
the import statement over to the store file.
40
00:02:49,220 --> 00:02:54,650
There's one last thing we will copy over to the register action function in the register.
41
00:02:54,650 --> 00:02:57,200
Function for the register form components.
42
00:02:57,200 --> 00:03:03,800
Cut the line of code where we update the user logged in state property pasted at the very end of the
43
00:03:03,800 --> 00:03:05,660
register action function.
44
00:03:07,870 --> 00:03:13,960
Like I said before, we can access the state of a store from within an action by using the this keyword.
45
00:03:16,120 --> 00:03:20,500
The next step is to update the argument values inside both functions.
46
00:03:20,530 --> 00:03:26,030
If we look carefully, we are attempting to access the values object from the component.
47
00:03:26,050 --> 00:03:29,400
However, we're not in the register form component.
48
00:03:29,410 --> 00:03:32,610
We don't have access to this data property anymore.
49
00:03:32,620 --> 00:03:35,200
We'll need to pass them on to the register.
50
00:03:35,200 --> 00:03:36,430
Action Function.
51
00:03:36,460 --> 00:03:42,880
Action functions must be matched to a component before they can be called switch to the register form
52
00:03:42,880 --> 00:03:43,650
component.
53
00:03:43,660 --> 00:03:46,430
First, we're going to clean things up a bit.
54
00:03:46,450 --> 00:03:49,000
There are two empty tree blocks.
55
00:03:49,000 --> 00:03:50,590
We don't need both of them.
56
00:03:50,590 --> 00:03:54,580
We're going to remove the second try catch block all together.
57
00:03:56,860 --> 00:04:00,960
The variable user credentials is not being used anymore.
58
00:04:00,970 --> 00:04:04,660
We can remove the variable definition and the log statement.
59
00:04:06,830 --> 00:04:09,800
Time to map the action at the top of the script.
60
00:04:09,800 --> 00:04:15,050
BLOCK Replace the map writable state function with the map actions function.
61
00:04:17,100 --> 00:04:22,650
Since we're not manipulating the store from within the components, we don't need the function for writing
62
00:04:22,680 --> 00:04:23,580
to the state.
63
00:04:23,610 --> 00:04:30,300
The map actions function will assist us with mapping the action functions to the component in the computed
64
00:04:30,300 --> 00:04:31,050
object.
65
00:04:31,050 --> 00:04:35,520
Replace the map writable state function with the map actions function.
66
00:04:37,720 --> 00:04:43,360
And the second argument the array must contain a list of functions to map to the component.
67
00:04:43,390 --> 00:04:44,920
There's just one problem.
68
00:04:44,920 --> 00:04:50,260
The name of the function in the store conflicts with the name of the register method from within the
69
00:04:50,260 --> 00:04:51,040
component.
70
00:04:51,070 --> 00:04:56,950
Luckily, the map actions function supports aliases like the other mapping function.
71
00:04:56,980 --> 00:04:59,110
Replace the array with an object.
72
00:05:01,220 --> 00:05:05,750
The alias for the register function will be called create user.
73
00:05:07,940 --> 00:05:09,860
Before calling this function.
74
00:05:09,860 --> 00:05:16,310
Actions are not meant to be mapped to the computed object and said they must be mapped to the methods
75
00:05:16,310 --> 00:05:17,090
object.
76
00:05:17,120 --> 00:05:18,200
Move the map.
77
00:05:18,200 --> 00:05:22,460
Actions function from the computed object to the methods object.
78
00:05:24,540 --> 00:05:30,180
We can safely remove the computed object from the components configuration options.
79
00:05:32,230 --> 00:05:40,330
Let's scroll to the try block inside this block called the this dot create user function with the O8
80
00:05:40,330 --> 00:05:41,110
keyword.
81
00:05:43,150 --> 00:05:47,830
We can pass on data to an action function like we would any other function.
82
00:05:47,830 --> 00:05:50,530
Let's pass on the values arguments.
83
00:05:52,640 --> 00:05:57,200
Let's accept the data being passed into the function back in the store.
84
00:05:57,230 --> 00:06:02,720
Update the register functions argument list to accept the values argument.
85
00:06:04,660 --> 00:06:10,390
We've successfully created an action function that will register the user in both the authentication
86
00:06:10,390 --> 00:06:12,430
and store services.
87
00:06:12,640 --> 00:06:18,370
It's accessible to every component in the application by using pinions mapping functions.
88
00:06:18,520 --> 00:06:21,160
We've refactored a lot of code so far.
89
00:06:21,190 --> 00:06:22,820
Head on over to the browser.
90
00:06:22,840 --> 00:06:25,990
We'll register a new user into the application.
91
00:06:31,740 --> 00:06:36,660
Well, want to check the Firebase console to verify that the user was created.
92
00:06:36,690 --> 00:06:39,810
Let's switch over to the database and refresh it.
93
00:06:42,020 --> 00:06:45,210
We'll find a new document in the users collection.
94
00:06:45,230 --> 00:06:48,440
It has the same information I've inputted into the form.
95
00:06:48,470 --> 00:06:49,170
Great.
96
00:06:49,190 --> 00:06:52,190
Let's head back to the user store file.
97
00:06:52,220 --> 00:06:55,480
There's one thing I want to mention about the action function.
98
00:06:55,490 --> 00:06:59,990
You'll notice I'm not wrapping the requests with a try catch block.
99
00:07:00,020 --> 00:07:05,390
While it's not necessarily wrong to handle errors in an action function, we don't need to.
100
00:07:05,420 --> 00:07:09,350
In this case, we can let individual components handle it.
101
00:07:09,380 --> 00:07:12,650
This strategy allows for more flexibility.
102
00:07:12,650 --> 00:07:16,460
If components need to handle errors differently, they can.
103
00:07:16,640 --> 00:07:18,140
That's all there is to it.
104
00:07:18,140 --> 00:07:22,910
By using actions, we have a reusable function for registering the user.
105
00:07:22,940 --> 00:07:28,310
The register action is available globally to every component in the application.
106
00:07:28,700 --> 00:07:32,110
Hopefully I have convinced you to use actions.
107
00:07:32,120 --> 00:07:35,390
The best part about actions is that they're reusable.
108
00:07:35,420 --> 00:07:40,880
If we had an admin dashboard, we don't have to worry about writing the request twice.
109
00:07:40,880 --> 00:07:45,110
We can dispatch the action as we did in the register function.
10541
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.