Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,029 --> 00:00:03,880
Now this notification is there,
2
00:00:03,880 --> 00:00:05,050
and that's great.
3
00:00:05,050 --> 00:00:07,000
But it would be good if, eventually,
4
00:00:07,000 --> 00:00:08,920
it would disappear.
5
00:00:08,920 --> 00:00:10,890
And that's something which we can implement.
6
00:00:10,890 --> 00:00:13,600
And I want to make sure that it can be removed
7
00:00:13,600 --> 00:00:15,530
in two different ways.
8
00:00:15,530 --> 00:00:19,110
We can click it, and then it should be removed instantly.
9
00:00:19,110 --> 00:00:21,310
Alternatively, there should be a timer
10
00:00:21,310 --> 00:00:23,880
which removes it after three seconds
11
00:00:23,880 --> 00:00:27,150
if it's a success or error notification.
12
00:00:27,150 --> 00:00:28,680
Pending notifications
13
00:00:28,680 --> 00:00:32,210
should not be removed automatically, let's say.
14
00:00:32,210 --> 00:00:34,130
Now to implement this, we can dive
15
00:00:34,130 --> 00:00:36,080
into the notification component
16
00:00:36,080 --> 00:00:40,680
and start with the click case, simply by adding onClick
17
00:00:40,680 --> 00:00:43,020
and doing something when it is clicked.
18
00:00:43,020 --> 00:00:46,060
Now the question is what we wanna do if it's clicked,
19
00:00:46,060 --> 00:00:49,440
and I would say we wanna call hideNotification
20
00:00:49,440 --> 00:00:50,900
on our context.
21
00:00:50,900 --> 00:00:54,650
So I will just use context inside of this component.
22
00:00:54,650 --> 00:00:57,680
There's nothing that prevents us from doing that.
23
00:00:57,680 --> 00:01:00,100
Just because we show this component,
24
00:01:00,100 --> 00:01:01,490
because of context,
25
00:01:01,490 --> 00:01:05,000
does not mean that we can't use context in there.
26
00:01:05,000 --> 00:01:07,240
So I'll use the useContext hook,
27
00:01:07,240 --> 00:01:09,853
which you should import from React,
28
00:01:11,030 --> 00:01:14,020
and I'll import the NotificationContext
29
00:01:14,020 --> 00:01:17,030
from the store folder and there,
30
00:01:17,030 --> 00:01:19,130
notification-context file,
31
00:01:19,130 --> 00:01:21,563
to establish this connection here.
32
00:01:22,430 --> 00:01:25,940
Then here we have the notificationContext object,
33
00:01:25,940 --> 00:01:29,740
and now we can use that to hide the notification.
34
00:01:29,740 --> 00:01:33,151
Because in our context, there is this
35
00:01:33,151 --> 00:01:36,350
hideNotification function which we call,
36
00:01:36,350 --> 00:01:38,580
which we can call, which eventually
37
00:01:38,580 --> 00:01:41,440
will call this function here.
38
00:01:41,440 --> 00:01:42,730
Now thereafter, what we can do
39
00:01:42,730 --> 00:01:44,830
in the notification component,
40
00:01:44,830 --> 00:01:49,830
is we can just bind onClick to notificationCtx.
41
00:01:50,290 --> 00:01:52,660
So to this notificationContext object
42
00:01:52,660 --> 00:01:54,910
I'm getting from my context here,
43
00:01:54,910 --> 00:01:58,693
and then point at the hideNotification function.
44
00:01:59,670 --> 00:02:03,070
Don't execute it here, so no parentheses,
45
00:02:03,070 --> 00:02:05,930
but instead let React execute it for you
46
00:02:05,930 --> 00:02:07,350
when that click occurs.
47
00:02:07,350 --> 00:02:10,122
That's what we do by pointing at it here.
48
00:02:10,960 --> 00:02:13,190
And then with that, if we save this,
49
00:02:13,190 --> 00:02:16,240
if I click the notification, it disappears.
50
00:02:16,240 --> 00:02:18,480
Shows up again, disappears.
51
00:02:18,480 --> 00:02:20,520
So that's working.
52
00:02:20,520 --> 00:02:23,760
Now it should also disappear automatically.
53
00:02:23,760 --> 00:02:27,120
And for this, we can go back into the notificationContext.
54
00:02:27,120 --> 00:02:30,280
And there we might wanna set a timer
55
00:02:30,280 --> 00:02:34,410
whenever the notification starts showing up.
56
00:02:34,410 --> 00:02:37,300
And that is a perfect-use case for useEffect,
57
00:02:37,300 --> 00:02:40,070
because that allows us to listen to changes
58
00:02:40,070 --> 00:02:42,830
to the notification object, and then act
59
00:02:42,830 --> 00:02:44,930
when it does change.
60
00:02:44,930 --> 00:02:47,007
So here I'll then import useEffect,
61
00:02:48,840 --> 00:02:52,260
and add a new useEffect call here.
62
00:02:52,260 --> 00:02:55,823
And at the useEffect function enter the dependency array.
63
00:02:56,680 --> 00:02:59,302
Now in the useEffect function, I wanna check
64
00:02:59,302 --> 00:03:02,385
if activeNotification is set,
65
00:03:02,385 --> 00:03:04,083
and, important,
66
00:03:04,083 --> 00:03:06,416
if activeNotification status
67
00:03:07,400 --> 00:03:09,400
is equal to success
68
00:03:10,480 --> 00:03:15,443
or if activeNotification status is equal to error.
69
00:03:17,880 --> 00:03:20,060
And I'll wrap that in parentheses
70
00:03:20,060 --> 00:03:22,870
so that the first condition must always be met
71
00:03:22,870 --> 00:03:26,210
and then either of the two other conditions.
72
00:03:26,210 --> 00:03:28,640
So if we have an activeNotification,
73
00:03:28,640 --> 00:03:30,840
then we also perform those checks
74
00:03:30,840 --> 00:03:32,479
to make it into this IF block
75
00:03:32,479 --> 00:03:36,370
if we have an activeNotification with a status
76
00:03:36,370 --> 00:03:38,640
of success and error.
77
00:03:38,640 --> 00:03:40,290
If we have no activeNotification,
78
00:03:41,390 --> 00:03:43,640
or if the status is pending,
79
00:03:43,640 --> 00:03:45,950
we don't do anything.
80
00:03:45,950 --> 00:03:49,140
Now if we do have this kind of notification though,
81
00:03:49,140 --> 00:03:52,210
then I wanna set a timer that will eventually
82
00:03:52,210 --> 00:03:54,033
remove the notification though.
83
00:03:54,900 --> 00:03:59,900
So then here I'll set a timer with setTimeout,
84
00:04:01,340 --> 00:04:04,170
which will eventually do something.
85
00:04:04,170 --> 00:04:07,383
And it should do that after three seconds roughly.
86
00:04:08,410 --> 00:04:11,370
And what it should do, is that it should call
87
00:04:11,370 --> 00:04:14,880
setActiveNotification and set this to null.
88
00:04:14,880 --> 00:04:16,850
Or, to be precise,
89
00:04:16,850 --> 00:04:20,100
it should call hideNotificationHandler.
90
00:04:20,100 --> 00:04:22,290
We could, of course, also call that function.
91
00:04:22,290 --> 00:04:25,313
But I'll just set the activeNotification to null here.
92
00:04:27,010 --> 00:04:31,590
Now I'll return here, I'll return that cleanup function,
93
00:04:31,590 --> 00:04:36,460
which useEffect accepts, to clear that timer,
94
00:04:36,460 --> 00:04:38,900
so that timer, which has stored this constant,
95
00:04:38,900 --> 00:04:43,050
if useEffect reruns before the timer went off,
96
00:04:43,050 --> 00:04:45,750
so that we don't have multiple ongoing timers
97
00:04:45,750 --> 00:04:47,560
at the same time.
98
00:04:47,560 --> 00:04:50,850
So here I'm utilizing this cleanup function of useEffect.
99
00:04:50,850 --> 00:04:53,910
All that code here is still in this IF block, though.
100
00:04:53,910 --> 00:04:56,883
If we're not making it in there, useEffect does nothing.
101
00:04:58,260 --> 00:05:01,880
Now since we are relying on the activeNotification here,
102
00:05:01,880 --> 00:05:04,130
and since we want to rerun this effect,
103
00:05:04,130 --> 00:05:06,570
whenever the activeNotification changes,
104
00:05:06,570 --> 00:05:10,263
I'll add activeNotification as a dependency here.
105
00:05:11,870 --> 00:05:13,500
And with this implemented,
106
00:05:13,500 --> 00:05:15,760
it should disappear automatically.
107
00:05:15,760 --> 00:05:20,760
If I do register, we see pending, and then success,
108
00:05:21,700 --> 00:05:25,490
and after three seconds it disappears automatically.
109
00:05:25,490 --> 00:05:28,080
We also don't have any errors here,
110
00:05:28,080 --> 00:05:31,380
except for that 500 error from earlier.
111
00:05:31,380 --> 00:05:32,960
So that is working.
112
00:05:32,960 --> 00:05:36,080
And that is how we use this notification component
113
00:05:36,080 --> 00:05:40,190
with React context for app-wide state management
114
00:05:40,190 --> 00:05:42,450
and for giving the user some feedback
115
00:05:42,450 --> 00:05:44,313
about the sign-up process.
116
00:05:45,270 --> 00:05:47,470
Now I got a challenge for you.
117
00:05:47,470 --> 00:05:49,810
We also have this comments area,
118
00:05:49,810 --> 00:05:52,770
where the user should get some feedback.
119
00:05:52,770 --> 00:05:55,100
And here, I want you to work on that.
120
00:05:55,100 --> 00:05:58,570
When a new comment is added, so when we click submit here,
121
00:05:58,570 --> 00:06:00,340
I want you to make sure
122
00:06:00,340 --> 00:06:03,430
that we also show this notification
123
00:06:03,430 --> 00:06:05,770
with pending, error, and success.
124
00:06:05,770 --> 00:06:10,770
And, as a bonus task, I want you to show some loading text
125
00:06:11,000 --> 00:06:14,450
whilst we're waiting for the comments to be loaded.
126
00:06:14,450 --> 00:06:18,070
So there you should not use the notification,
127
00:06:18,070 --> 00:06:21,890
but just some loading text whilst we're waiting for that.
128
00:06:21,890 --> 00:06:25,930
That's my challenge for you, a little exercise for you.
129
00:06:25,930 --> 00:06:29,283
In the next lecture, we're going to implement that together.
10063
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.