Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,110 --> 00:00:03,040
So, in this
2
00:00:03,040 --> 00:00:05,800
NotificationContextProvider component,
3
00:00:05,800 --> 00:00:07,460
I wonna manage the state,
4
00:00:07,460 --> 00:00:10,690
which is then distributed through context
5
00:00:10,690 --> 00:00:12,830
to the various stakeholders,
6
00:00:12,830 --> 00:00:17,220
the various components that are interested in that state.
7
00:00:17,220 --> 00:00:19,853
And hence, I'll start by creating an useState.
8
00:00:20,856 --> 00:00:24,330
And that is the state that stores the notification
9
00:00:24,330 --> 00:00:27,010
that should be shown, if any.
10
00:00:27,010 --> 00:00:30,062
So here we can have the activeNotification,
11
00:00:30,062 --> 00:00:33,145
and a setActiveNotification function.
12
00:00:34,920 --> 00:00:37,300
And initially, that's undefined because initially,
13
00:00:37,300 --> 00:00:39,453
I have no active notification.
14
00:00:40,330 --> 00:00:42,210
But then I'll add two functions,
15
00:00:42,210 --> 00:00:46,290
the showNotificationHandler function,
16
00:00:46,290 --> 00:00:51,290
and the hideNotificationHandler function.
17
00:00:54,510 --> 00:00:57,220
These are the two functions I'll add here.
18
00:00:57,220 --> 00:00:59,307
In the showNotificationHandler,
19
00:00:59,307 --> 00:01:02,409
I wonna set my active notification
20
00:01:02,409 --> 00:01:05,545
and I do expect that I get information
21
00:01:05,545 --> 00:01:08,335
about the notification that should be shown
22
00:01:08,335 --> 00:01:10,428
as a parameter here.
23
00:01:10,428 --> 00:01:14,360
So here, I'll expect to get my notification data.
24
00:01:14,360 --> 00:01:18,040
And then I'll set my active notification to an object
25
00:01:18,040 --> 00:01:22,270
where the title is notificationData.title,
26
00:01:22,270 --> 00:01:25,970
where the message is notificationData.message,
27
00:01:25,970 --> 00:01:30,080
and where the status is notificationData.status.
28
00:01:30,080 --> 00:01:33,240
And since these objects are exactly equal,
29
00:01:33,240 --> 00:01:35,170
we can of course, take a shortcut
30
00:01:35,170 --> 00:01:37,970
and just set the active notification
31
00:01:37,970 --> 00:01:41,270
to that notification data we're getting here.
32
00:01:41,270 --> 00:01:42,960
To get correct auto completion,
33
00:01:42,960 --> 00:01:47,300
I'll then also update my showNotification function up there,
34
00:01:47,300 --> 00:01:50,210
which will soon use
35
00:01:50,210 --> 00:01:53,670
and expect notification data there as well.
36
00:01:53,670 --> 00:01:56,570
Again, we're not doing anything with it there,
37
00:01:56,570 --> 00:02:00,470
but this will help us with IDE auto completion later.
38
00:02:00,470 --> 00:02:02,730
The actual function is defined here
39
00:02:02,730 --> 00:02:05,453
and we'll connect it to the context soon.
40
00:02:06,830 --> 00:02:09,130
Now, in hideNotificationHandler,
41
00:02:09,130 --> 00:02:12,410
I wonna set my active notification to null.
42
00:02:12,410 --> 00:02:14,833
I wonna reset it and get rid of it.
43
00:02:16,070 --> 00:02:19,760
Now, the goal is to bundle my notification data
44
00:02:19,760 --> 00:02:24,240
and pointer set these functions together into one object,
45
00:02:24,240 --> 00:02:27,630
which we can then distribute as context
46
00:02:27,630 --> 00:02:29,960
to the other components in the app.
47
00:02:29,960 --> 00:02:33,550
So, I'll create a new constant here, my context.
48
00:02:33,550 --> 00:02:35,140
And that's our object,
49
00:02:35,140 --> 00:02:37,740
which now has the structure defined up here
50
00:02:37,740 --> 00:02:39,890
for this initial context.
51
00:02:39,890 --> 00:02:44,340
So here, I then wonna have a notification key
52
00:02:44,340 --> 00:02:48,173
and point at the activeNotification state here as a value.
53
00:02:49,060 --> 00:02:51,970
I'll add a showNotification key.
54
00:02:51,970 --> 00:02:55,180
And we defined that this should hold a function,
55
00:02:55,180 --> 00:02:56,842
so, here I will point at this
56
00:02:56,842 --> 00:02:59,600
showNotificationHandler function,
57
00:02:59,600 --> 00:03:03,823
which is a function off the proper structure and signature.
58
00:03:04,850 --> 00:03:07,510
And I'll add my hideNotification key
59
00:03:07,510 --> 00:03:11,273
and point at the hideNotificationHandler function.
60
00:03:13,950 --> 00:03:15,790
So, that's now my context object.
61
00:03:15,790 --> 00:03:18,970
And now we distribute this to all the components
62
00:03:18,970 --> 00:03:22,050
that are interested through the value prop
63
00:03:22,050 --> 00:03:24,093
on this provider component.
64
00:03:25,130 --> 00:03:27,260
We set that equal to context.
65
00:03:27,260 --> 00:03:29,740
And the cool thing is that whenever we change
66
00:03:29,740 --> 00:03:31,030
this state here,
67
00:03:31,030 --> 00:03:32,270
for example, by calling
68
00:03:32,270 --> 00:03:35,108
show or hideNotificationHandler in the end,
69
00:03:35,108 --> 00:03:39,330
this notification context provider component will re-render
70
00:03:39,330 --> 00:03:42,860
and will distribute the updated context object
71
00:03:42,860 --> 00:03:45,080
to interested components.
72
00:03:45,080 --> 00:03:47,143
And that's exactly what we need here.
5613
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.