Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,050 --> 00:00:03,640
Were you successful?
2
00:00:03,640 --> 00:00:05,360
Let's do it together.
3
00:00:05,360 --> 00:00:08,330
And for this, let's maybe start in the App component
4
00:00:08,330 --> 00:00:10,040
and render the Auth component,
5
00:00:10,040 --> 00:00:12,150
if we're not authenticated
6
00:00:12,150 --> 00:00:16,570
and render UserProfile if we are authenticated.
7
00:00:16,570 --> 00:00:19,282
So for this, I'll also import UserProfile
8
00:00:19,282 --> 00:00:23,810
from ./components/UserProfile.
9
00:00:23,810 --> 00:00:25,060
And then here,
10
00:00:25,060 --> 00:00:27,950
we need to tap into our
11
00:00:27,950 --> 00:00:28,880
store
12
00:00:28,880 --> 00:00:31,350
and read a value from there.
13
00:00:31,350 --> 00:00:33,750
And we do that with useSelector.
14
00:00:33,750 --> 00:00:36,550
So I'll import useSelector
15
00:00:36,550 --> 00:00:39,340
from React-Redux
16
00:00:40,300 --> 00:00:42,800
and in the App component function
17
00:00:42,800 --> 00:00:46,800
we therefore call useSelector, pass a function to it
18
00:00:46,800 --> 00:00:50,070
a function which will always receive state as a argument
19
00:00:50,070 --> 00:00:55,070
that's passed in by Redux or by React Redux to be precise.
20
00:00:55,510 --> 00:00:58,980
And then here, I wanna return state.auth
21
00:01:00,311 --> 00:01:05,209
.auth because I picked auth as an identifier here.
22
00:01:05,209 --> 00:01:07,190
And then in my authSlice,
23
00:01:07,190 --> 00:01:09,803
I have this isAuthenticated property.
24
00:01:10,860 --> 00:01:15,263
So it's, state.auth.isAuthenticated.
25
00:01:16,580 --> 00:01:20,660
And that gives me the isAuth value.
26
00:01:20,660 --> 00:01:23,870
Of course, this constant can be named however you want.
27
00:01:23,870 --> 00:01:26,830
And we can use this to check if we're not
28
00:01:26,830 --> 00:01:28,330
hence the exclamation mark.
29
00:01:28,330 --> 00:01:31,990
If we're not authenticated
30
00:01:31,990 --> 00:01:35,064
then I wanna show the Auth component.
31
00:01:35,064 --> 00:01:36,250
Else,
32
00:01:36,250 --> 00:01:37,992
if isAuth is truthy
33
00:01:37,992 --> 00:01:40,183
I wanna render UserProfile.
34
00:01:43,010 --> 00:01:45,310
And if everything works, if we save
35
00:01:45,310 --> 00:01:47,993
we should still see that Auth form.
36
00:01:49,040 --> 00:01:52,040
Now we also got some conditional content to render here
37
00:01:52,040 --> 00:01:53,610
in the header and therefore
38
00:01:53,610 --> 00:01:55,920
we can go into the Header JS file
39
00:01:55,920 --> 00:01:57,990
and do the same thing here.
40
00:01:57,990 --> 00:02:00,760
We import useSelector
41
00:02:00,760 --> 00:02:02,680
from React-Redux
42
00:02:03,600 --> 00:02:04,710
and then here
43
00:02:06,540 --> 00:02:09,729
we create our isAuth property
44
00:02:09,729 --> 00:02:11,200
or constant I mean,
45
00:02:11,200 --> 00:02:15,330
call useSelector and use the same selector logic
46
00:02:15,330 --> 00:02:18,237
or diving into state.auth.isAuthenticated.
47
00:02:22,420 --> 00:02:25,730
And now we can use isAuth to conditionally render
48
00:02:25,730 --> 00:02:28,940
or not render those list items.
49
00:02:28,940 --> 00:02:30,230
And since in this case
50
00:02:30,230 --> 00:02:33,410
actually the entire navigation is only relevant.
51
00:02:33,410 --> 00:02:35,940
If we are authenticated, we can also just
52
00:02:35,940 --> 00:02:39,051
render the entire nav item conditionally.
53
00:02:39,051 --> 00:02:41,746
If isAuth is truthy
54
00:02:41,746 --> 00:02:42,829
then
55
00:02:42,829 --> 00:02:44,000
I want to
56
00:02:44,000 --> 00:02:44,833
render
57
00:02:44,833 --> 00:02:46,490
this nav
58
00:02:46,490 --> 00:02:47,323
like this.
59
00:02:48,540 --> 00:02:52,600
If we now save that and reload,
60
00:02:52,600 --> 00:02:54,040
we don't see anything here
61
00:02:54,040 --> 00:02:56,993
because initially we are not authenticated.
62
00:02:58,220 --> 00:03:00,257
Now I wanna the Login action
63
00:03:00,257 --> 00:03:02,830
if the Login button is clicked
64
00:03:02,830 --> 00:03:06,350
I actually won't even bother to read the user input.
65
00:03:06,350 --> 00:03:08,470
I will not validate it.
66
00:03:08,470 --> 00:03:12,281
You can do all of that, but that's not related to Redux.
67
00:03:12,281 --> 00:03:15,600
So I will just care about the form submission
68
00:03:15,600 --> 00:03:17,173
with the Login button,
69
00:03:18,550 --> 00:03:20,720
hence here at the Auth component
70
00:03:20,720 --> 00:03:25,170
we wanna add onSubmit listener on the form here
71
00:03:25,170 --> 00:03:29,010
and then add a new function in the Auth component,
72
00:03:29,010 --> 00:03:30,190
the submit,
73
00:03:30,190 --> 00:03:31,140
or the loginHandler
74
00:03:34,520 --> 00:03:36,580
where we get to the default event
75
00:03:36,580 --> 00:03:40,100
which we still should use to prevent the default
76
00:03:40,100 --> 00:03:43,260
to make sure React doesn't send a HTTP request
77
00:03:43,260 --> 00:03:47,160
or the browser sends a HTTP request to be precise.
78
00:03:47,160 --> 00:03:50,360
You'll learn about that in the forms section.
79
00:03:50,360 --> 00:03:52,610
And then with that wired up
80
00:03:52,610 --> 00:03:56,510
in that loginHandler, after preventing the default
81
00:03:56,510 --> 00:03:57,700
I wanna dispatch
82
00:03:57,700 --> 00:04:00,053
that Login action.
83
00:04:01,090 --> 00:04:03,757
Now for this we wanna use the auth actions
84
00:04:03,757 --> 00:04:05,660
which were exporting
85
00:04:05,660 --> 00:04:08,100
and use this patch.
86
00:04:08,100 --> 00:04:10,410
So in the Auth JS file
87
00:04:10,410 --> 00:04:11,869
we import
88
00:04:11,869 --> 00:04:13,978
useDispatch,
89
00:04:13,978 --> 00:04:16,470
from React-Redux.
90
00:04:16,470 --> 00:04:19,420
And in addition, I also wanna import
91
00:04:19,420 --> 00:04:22,000
from going up one level,
92
00:04:22,000 --> 00:04:23,819
store index.
93
00:04:23,819 --> 00:04:28,623
And from there, I wanna import the auth actions.
94
00:04:29,860 --> 00:04:32,982
And then here in the auth component, we execute
95
00:04:32,982 --> 00:04:37,343
useDispatch to get access to that dispatch function.
96
00:04:38,340 --> 00:04:39,700
And in the loginHandler
97
00:04:39,700 --> 00:04:42,860
we then use the Dispatch function to dispatch
98
00:04:42,860 --> 00:04:45,810
authActions.login
99
00:04:47,210 --> 00:04:50,600
we execute login because you learned that this is a
100
00:04:50,600 --> 00:04:55,080
action creator returning the actual action object
101
00:04:55,080 --> 00:04:56,723
which should be dispatched.
102
00:04:58,280 --> 00:05:00,169
If we do that and save everything.
103
00:05:00,169 --> 00:05:03,520
If we reload and click on Login
104
00:05:03,520 --> 00:05:05,640
we switched to the login state
105
00:05:05,640 --> 00:05:08,470
as we can tell by the changes on the UI,
106
00:05:08,470 --> 00:05:09,593
so that works.
107
00:05:10,750 --> 00:05:11,890
Last but not least,
108
00:05:11,890 --> 00:05:14,960
let's wire up the Logout button here,
109
00:05:14,960 --> 00:05:18,862
and a for this in the header where we got the logout button
110
00:05:18,862 --> 00:05:22,060
I'll add onClick listener on that button
111
00:05:23,100 --> 00:05:26,600
and then add a function here to the Header component
112
00:05:26,600 --> 00:05:31,600
the logoutHandler, where I wanna dispatch my action.
113
00:05:32,010 --> 00:05:34,090
So again, just as before
114
00:05:34,090 --> 00:05:38,140
for this import useDispatch and we import our
115
00:05:38,140 --> 00:05:41,680
authActions from
116
00:05:41,680 --> 00:05:43,633
store index,
117
00:05:45,240 --> 00:05:46,400
we then call
118
00:05:46,400 --> 00:05:50,040
useDispatch in the component function to get access
119
00:05:50,040 --> 00:05:52,930
to that dispatch function.
120
00:05:52,930 --> 00:05:55,730
And then the loginHandler, we then dispatch
121
00:05:55,730 --> 00:05:57,155
we call dispatch
122
00:05:57,155 --> 00:05:58,113
and
123
00:05:58,113 --> 00:05:58,970
to it,
124
00:05:58,970 --> 00:06:01,173
we pass authActions.logout
125
00:06:02,776 --> 00:06:04,796
execute it as a method again
126
00:06:04,796 --> 00:06:09,637
because that then creates that logout action object.
127
00:06:09,637 --> 00:06:11,581
Now we need to wire up the logoutHandler
128
00:06:11,581 --> 00:06:12,414
to onClick
129
00:06:14,501 --> 00:06:17,292
and we should be good to go.
130
00:06:17,292 --> 00:06:20,302
If I now click on Logout, I'm locked out.
131
00:06:20,302 --> 00:06:23,090
If I click on Login, I'm logged back in.
132
00:06:23,090 --> 00:06:26,120
So that is working and that is how we can work
133
00:06:26,120 --> 00:06:28,860
with multiple, totally different pieces
134
00:06:28,860 --> 00:06:31,673
of data in one and the same application.
135
00:06:31,673 --> 00:06:33,501
And that is something you will need
136
00:06:33,501 --> 00:06:38,160
in almost any React application you're going to build.
137
00:06:38,160 --> 00:06:41,420
You will always just have one Redux store
138
00:06:41,420 --> 00:06:45,740
but then typically multiple different state slices
139
00:06:45,740 --> 00:06:48,370
which manage totally different data.
140
00:06:48,370 --> 00:06:51,230
And as you see it, it's super easy to create
141
00:06:51,230 --> 00:06:54,933
and manage multiple slices when using Redux toolkit.
10074
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.