Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,080 --> 00:00:03,190
Now, let's start
2
00:00:03,190 --> 00:00:05,670
with the newsletter-registration.
3
00:00:05,670 --> 00:00:10,100
For this we have the newsletter-registration component
4
00:00:10,100 --> 00:00:11,400
in the input folder
5
00:00:11,400 --> 00:00:14,760
and there we're sending this HTTP request.
6
00:00:14,760 --> 00:00:17,480
Now I wanna show the pending notification
7
00:00:17,480 --> 00:00:21,330
whilst we are sending and waiting for the response.
8
00:00:21,330 --> 00:00:23,740
I wanna show the success notification
9
00:00:23,740 --> 00:00:25,950
if sending succeeded,
10
00:00:25,950 --> 00:00:30,490
and I wanna show the error notification if sending failed.
11
00:00:30,490 --> 00:00:32,259
Now, to make all of that happen
12
00:00:32,259 --> 00:00:35,110
in the NewsletterRegistration component
13
00:00:35,110 --> 00:00:37,560
we need to connect to context.
14
00:00:37,560 --> 00:00:41,470
And we do that with the useContext hook, as you learned.
15
00:00:41,470 --> 00:00:44,050
So here we then call useContext
16
00:00:44,050 --> 00:00:48,540
and we simply import the NotificationContext.
17
00:00:48,540 --> 00:00:52,510
So again, not the provider, but the context itself
18
00:00:52,510 --> 00:00:57,510
from our store and there to notification-context file.
19
00:00:57,810 --> 00:01:00,220
And then we establish this connection
20
00:01:00,220 --> 00:01:03,690
as we did it before a couple of minutes ago.
21
00:01:03,690 --> 00:01:06,840
So here we then have our notificationContext.
22
00:01:06,840 --> 00:01:09,520
But here I'm not interested in the notification,
23
00:01:09,520 --> 00:01:11,990
here instead, I wanna call a function
24
00:01:11,990 --> 00:01:14,420
to show the notification.
25
00:01:14,420 --> 00:01:17,010
For example, here in the registrationHandler
26
00:01:17,010 --> 00:01:19,460
when we start sending this request.
27
00:01:19,460 --> 00:01:23,410
Right before we do send it, I want to show a notification
28
00:01:23,410 --> 00:01:25,850
that is in the pending status
29
00:01:25,850 --> 00:01:29,270
that says something like signing up.
30
00:01:29,270 --> 00:01:32,790
So for this, we can use the notificationContext object
31
00:01:32,790 --> 00:01:35,230
and call showNotification
32
00:01:35,230 --> 00:01:38,910
and pass a object with the notification data.
33
00:01:38,910 --> 00:01:42,893
And here all set a title of Signing up,
34
00:01:43,780 --> 00:01:48,780
a message of Registering for newsletter, something like this
35
00:01:52,490 --> 00:01:56,683
and set the status here to pending, because we are pending.
36
00:01:58,060 --> 00:02:00,170
Now, if we are successful,
37
00:02:00,170 --> 00:02:02,490
so in this last then block here
38
00:02:02,490 --> 00:02:07,470
then I want to set the notification to success.
39
00:02:07,470 --> 00:02:11,540
So in here I'll actually call showNotification again
40
00:02:11,540 --> 00:02:14,210
but then say, "Success."
41
00:02:14,210 --> 00:02:16,620
As a title and as a text here,
42
00:02:16,620 --> 00:02:21,620
we can say, "Successfully registered for newsletter."
43
00:02:23,800 --> 00:02:25,510
Something like this.
44
00:02:25,510 --> 00:02:28,663
And the status is success instead of pending.
45
00:02:29,870 --> 00:02:31,360
Now, in case of an error
46
00:02:31,360 --> 00:02:34,740
I also wanna show the appropriate notification
47
00:02:34,740 --> 00:02:37,420
and that's why I will add a catch method here
48
00:02:37,420 --> 00:02:39,510
where we get a potential error
49
00:02:39,510 --> 00:02:42,380
and here I also wanna show a notification
50
00:02:42,380 --> 00:02:47,380
but the status will be error, the title will be Error
51
00:02:47,450 --> 00:02:50,760
and the message could be retrieved from the error.
52
00:02:50,760 --> 00:02:53,410
Here we could check if we have a message property
53
00:02:53,410 --> 00:02:54,420
which we wanna use,
54
00:02:54,420 --> 00:02:57,740
and alternatively just say, "Something went wrong."
55
00:02:57,740 --> 00:03:00,290
So we show the error message if we have it,
56
00:03:00,290 --> 00:03:01,770
or we showed us fallback
57
00:03:01,770 --> 00:03:04,593
if there is no message attached to the error.
58
00:03:06,300 --> 00:03:08,700
Now, one problem we'll have here is
59
00:03:08,700 --> 00:03:13,700
that the fetch function works such that error HTTP codes.
60
00:03:15,450 --> 00:03:18,560
So 400 and 500 status codes
61
00:03:18,560 --> 00:03:22,620
will not cost is promise to fail.
62
00:03:22,620 --> 00:03:25,350
So we will not make it into this catch block
63
00:03:25,350 --> 00:03:27,760
if we have an error status code.
64
00:03:27,760 --> 00:03:29,210
And I would like to make it
65
00:03:29,210 --> 00:03:32,070
into this catch block if that's case.
66
00:03:32,070 --> 00:03:35,330
So to make sure that this is how everything works
67
00:03:35,330 --> 00:03:38,910
I will actually dive into this then block
68
00:03:38,910 --> 00:03:42,920
and check if response.ok.
69
00:03:42,920 --> 00:03:47,350
This is the extra field with this response object has.
70
00:03:47,350 --> 00:03:48,480
And if that's the case
71
00:03:48,480 --> 00:03:51,440
I know that we have a success status code.
72
00:03:51,440 --> 00:03:54,373
Then I wanna return response.jason.
73
00:03:55,210 --> 00:03:59,090
If that's not okay, though, if we make it past this if check
74
00:03:59,090 --> 00:04:01,190
then I know that we have an error.
75
00:04:01,190 --> 00:04:05,630
In this case, I also wanna get my response data
76
00:04:05,630 --> 00:04:09,730
but I also want to throw an error.
77
00:04:09,730 --> 00:04:13,030
I wanna reject that promise by throwing an error
78
00:04:13,030 --> 00:04:15,340
inside of the then block.
79
00:04:15,340 --> 00:04:18,209
And for data here, when using then catch
80
00:04:18,209 --> 00:04:22,140
instead of async await, we'll need a nest that promise
81
00:04:22,140 --> 00:04:25,390
because I'll need to get access to the response object
82
00:04:25,390 --> 00:04:26,480
while it's getting access
83
00:04:26,480 --> 00:04:29,580
to the past data at the same time.
84
00:04:29,580 --> 00:04:32,610
So here all then called response.jason as well
85
00:04:32,610 --> 00:04:33,870
but I'll not return it.
86
00:04:33,870 --> 00:04:37,280
But instead here in the then block,
87
00:04:37,280 --> 00:04:39,930
I noted we're already in the error case
88
00:04:39,930 --> 00:04:41,800
because we're past this if check,
89
00:04:41,800 --> 00:04:46,800
here I will then throw a new Error with a message
90
00:04:47,020 --> 00:04:48,670
which I get from my data.
91
00:04:48,670 --> 00:04:52,080
Here I'll check if I got a message in my response
92
00:04:52,080 --> 00:04:54,250
which I got back from the server
93
00:04:54,250 --> 00:04:57,510
and I'll use stat as an error message if I have one
94
00:04:57,510 --> 00:05:00,597
otherwise I'll set up fallback here as well,
95
00:05:00,597 --> 00:05:02,177
"Something went wrong."
96
00:05:03,640 --> 00:05:05,930
And now you're all returned,
97
00:05:05,930 --> 00:05:09,400
this promise chain, this nest of promise chain
98
00:05:09,400 --> 00:05:13,170
which will throw an error if things go wrong.
99
00:05:13,170 --> 00:05:16,060
And this will then cause the outer promise chain,
100
00:05:16,060 --> 00:05:20,430
the main promise chain started by fetch to reject as well
101
00:05:20,430 --> 00:05:23,580
with that error passed along so that'd be make it
102
00:05:23,580 --> 00:05:25,943
into this catch block if we do have an error.
103
00:05:27,070 --> 00:05:30,970
So with all that implemented, we can try if this works.
104
00:05:30,970 --> 00:05:34,400
If we save everything and I reload,
105
00:05:34,400 --> 00:05:39,400
if I sign up for my newsletter here and I click register
106
00:05:39,520 --> 00:05:41,740
we see the signing up notification
107
00:05:41,740 --> 00:05:44,090
and it changes to success.
108
00:05:44,090 --> 00:05:46,810
It never disappears, we'll work on this later,
109
00:05:46,810 --> 00:05:49,090
for the moment it's always there.
110
00:05:49,090 --> 00:05:51,780
Now let's test it with an error,
111
00:05:51,780 --> 00:05:56,400
and for this all dive into my db-util.js file
112
00:05:56,400 --> 00:05:59,320
and break my mongodb credentials again,
113
00:05:59,320 --> 00:06:01,640
so that connecting to the database
114
00:06:01,640 --> 00:06:03,890
and inserting data will fail.
115
00:06:03,890 --> 00:06:06,860
If I do that and I try the same request again
116
00:06:06,860 --> 00:06:10,700
we start with pending, but then we go to the error case
117
00:06:10,700 --> 00:06:13,730
and we see that error message at the bottom.
118
00:06:13,730 --> 00:06:14,850
Now it's of course up to you
119
00:06:14,850 --> 00:06:18,060
if you wanna show that error message here, I'm doing it
120
00:06:18,060 --> 00:06:21,328
and everything works the way it should work here.
121
00:06:21,328 --> 00:06:24,150
And with that all change the credentials back
122
00:06:24,150 --> 00:06:27,040
to work and credentials and we're good.
123
00:06:27,040 --> 00:06:28,620
Now of course it would also be good
124
00:06:28,620 --> 00:06:31,450
if the notification could be removed
125
00:06:31,450 --> 00:06:33,733
and if it would disappear eventually.
9882
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.