Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,120 --> 00:00:03,320
So from the front end,
2
00:00:03,320 --> 00:00:05,340
we wanna send requests
3
00:00:06,674 --> 00:00:09,220
to our newly added comments API route.
4
00:00:09,220 --> 00:00:11,390
And it's the new comments component
5
00:00:11,390 --> 00:00:13,460
where I wanna start with this.
6
00:00:13,460 --> 00:00:17,330
Here, we have the send comment handler,
7
00:00:17,330 --> 00:00:20,250
and in there we get all the user input
8
00:00:20,250 --> 00:00:23,930
and we then have some client side validation
9
00:00:23,930 --> 00:00:25,410
which we shouldn't trust,
10
00:00:25,410 --> 00:00:27,150
but which is still there.
11
00:00:27,150 --> 00:00:30,370
And we then call on add comment on props
12
00:00:30,370 --> 00:00:32,354
which should be a function,
13
00:00:32,354 --> 00:00:36,670
where we pass an object with email name and text
14
00:00:36,670 --> 00:00:39,970
And on add comment, then in the end is a function here
15
00:00:39,970 --> 00:00:43,820
in the comments JSX file, this add comment handler
16
00:00:43,820 --> 00:00:45,760
which has passed as a function
17
00:00:45,760 --> 00:00:48,020
through the on add comment prop
18
00:00:48,020 --> 00:00:50,280
on the new comment component.
19
00:00:50,280 --> 00:00:52,690
And here we then receive that comments data
20
00:00:52,690 --> 00:00:54,733
which should be sent as a request.
21
00:00:56,070 --> 00:00:58,930
Now I actually do need to connect my
22
00:00:58,930 --> 00:01:02,173
send comment handler to the form.
23
00:01:03,090 --> 00:01:04,800
So here with on submit
24
00:01:04,800 --> 00:01:09,230
I point at send comment handler to connect that.
25
00:01:09,230 --> 00:01:12,323
And then in comments JS we wanna send a request.
26
00:01:13,290 --> 00:01:16,450
So here again, we can use the fetch function for it is
27
00:01:16,450 --> 00:01:20,430
and the route now is slash API slash
28
00:01:20,430 --> 00:01:22,270
and then it's comments,
29
00:01:22,270 --> 00:01:24,600
because of that comments folder
30
00:01:24,600 --> 00:01:27,880
slash and then this file.
31
00:01:27,880 --> 00:01:32,350
But of course, with a concrete value for the event ID.
32
00:01:32,350 --> 00:01:34,590
Now that event ID is something
33
00:01:34,590 --> 00:01:38,360
which we get through props here in the comments component.
34
00:01:38,360 --> 00:01:42,030
It's a Indian passed in by this page
35
00:01:42,030 --> 00:01:43,830
by this event ID page.
36
00:01:43,830 --> 00:01:48,830
So this Real page, not the API route, but this Real page.
37
00:01:49,070 --> 00:01:50,410
They're into comments.
38
00:01:50,410 --> 00:01:53,110
I pass in this event prop which points
39
00:01:53,110 --> 00:01:55,923
at the ID of the event we're loading here.
40
00:01:57,930 --> 00:02:01,504
Hence in the comments JSX file,
41
00:02:01,504 --> 00:02:03,170
we get this event ID prop
42
00:02:03,170 --> 00:02:06,420
and that is what we should add as a concrete value
43
00:02:06,420 --> 00:02:08,713
to the route here, to the path.
44
00:02:10,699 --> 00:02:12,850
Then it should be a post request.
45
00:02:12,850 --> 00:02:17,540
Hence we add this second argument to configure the request
46
00:02:17,540 --> 00:02:19,930
and set the method to post
47
00:02:19,930 --> 00:02:22,810
and the body then to our data.
48
00:02:22,810 --> 00:02:27,520
And here I'll use JSON stringify to send my comment data,
49
00:02:27,520 --> 00:02:29,510
which is this data package we get
50
00:02:29,510 --> 00:02:31,480
from the new comment component.
51
00:02:31,480 --> 00:02:35,370
Which contains email, name and text.
52
00:02:35,370 --> 00:02:37,970
And here I'm using these property names,
53
00:02:37,970 --> 00:02:41,474
email, name and text, and hence it's these property names
54
00:02:41,474 --> 00:02:44,874
which you should be looking for in your API route,
55
00:02:44,874 --> 00:02:47,890
Email, name, and text.
56
00:02:47,890 --> 00:02:51,820
So that the data can be retrieved correctly.
57
00:02:51,820 --> 00:02:54,110
Because I'm just forwarding that data
58
00:02:54,110 --> 00:02:57,403
with my HTTP request to the API route.
59
00:02:58,600 --> 00:03:02,730
Then I'll add these extra headers to clarify the
60
00:03:02,730 --> 00:03:04,930
the content type I'm sending here,
61
00:03:04,930 --> 00:03:09,303
application JSON and we should be good.
62
00:03:10,400 --> 00:03:13,050
Now we can again wait for a response
63
00:03:14,310 --> 00:03:15,430
and parse that
64
00:03:16,650 --> 00:03:18,180
and then as a next step
65
00:03:19,970 --> 00:03:22,550
thereafter get back some data
66
00:03:22,550 --> 00:03:26,273
which you for example console log, like this.
67
00:03:27,350 --> 00:03:29,360
With all that implemented,
68
00:03:29,360 --> 00:03:31,890
if we save everything and go back
69
00:03:31,890 --> 00:03:35,330
if I try to submit as empty form, I get an error here
70
00:03:35,330 --> 00:03:38,913
because I have some built in client side validation.
71
00:03:40,010 --> 00:03:44,110
If I disable this client side validation
72
00:03:44,110 --> 00:03:47,270
if I go to the new comments JSX file,
73
00:03:47,270 --> 00:03:50,703
and I comment out this code here,
74
00:03:52,350 --> 00:03:53,905
and I submit this, it is submitted,
75
00:03:53,905 --> 00:03:57,020
but now in the network tab,
76
00:03:57,020 --> 00:03:59,170
if we opened that and submit this
77
00:03:59,170 --> 00:04:02,740
we see that the request actually fails it's red
78
00:04:03,687 --> 00:04:06,680
and it has this 422 status code.
79
00:04:06,680 --> 00:04:09,510
Which we do send back from the API route,
80
00:04:09,510 --> 00:04:12,580
if our service side validation fails.
81
00:04:12,580 --> 00:04:15,160
And then that server side validation
82
00:04:15,160 --> 00:04:18,750
unlike the client side application can't be tricked.
83
00:04:18,750 --> 00:04:21,769
So it is good to add logic like this.
84
00:04:21,769 --> 00:04:24,945
Nonetheless, I'll bring back that client side validation
85
00:04:24,945 --> 00:04:26,807
because of course for the user experience,
86
00:04:26,807 --> 00:04:30,190
it's better if we already validate the data
87
00:04:30,190 --> 00:04:33,683
and show an error before we even send a request.
88
00:04:34,710 --> 00:04:38,530
So with that back, let's now enter some valid data here.
89
00:04:38,530 --> 00:04:42,567
Let's enter an email address and a name
90
00:04:42,567 --> 00:04:47,567
and some texts, this is my comment and click submit.
91
00:04:47,690 --> 00:04:51,970
And now this succeeds, we have the 201 status code.
92
00:04:51,970 --> 00:04:53,970
And in the response we see
93
00:04:55,300 --> 00:04:58,070
the message and this comment object
94
00:04:58,070 --> 00:05:02,040
with the dummy ID, which was created on the backend.
95
00:05:02,040 --> 00:05:04,940
We're also logging it into console here.
96
00:05:04,940 --> 00:05:08,480
So this works and if we have a look at the server side log
97
00:05:08,480 --> 00:05:11,311
so the terminal where we run npm run dev,
98
00:05:11,311 --> 00:05:15,090
we also see that object here.
99
00:05:15,090 --> 00:05:18,063
So that works and we do send that data.
100
00:05:18,920 --> 00:05:22,950
Now last but not least before we then add some real logic
101
00:05:22,950 --> 00:05:26,170
talking to a database to our API route,
102
00:05:26,170 --> 00:05:29,780
let's also fetch comments in the comments JSX file
103
00:05:29,780 --> 00:05:31,883
when we do show our comments.
104
00:05:32,930 --> 00:05:34,400
Now for showing the comments
105
00:05:34,400 --> 00:05:36,780
we have the toggle comments handler,
106
00:05:36,780 --> 00:05:40,520
and that is executed whenever this button here is clicked.
107
00:05:40,520 --> 00:05:43,260
And whenever we do show comments,
108
00:05:43,260 --> 00:05:46,680
I want to load the comments
109
00:05:46,680 --> 00:05:51,240
by sending a get request to these comments API route.
110
00:05:51,240 --> 00:05:55,703
So I wanna trigger this part of this handler functioning.
111
00:05:56,710 --> 00:05:59,519
And to achieve this we got various different ways.
112
00:05:59,519 --> 00:06:02,730
We could go into this toggle comments handler function
113
00:06:02,730 --> 00:06:05,950
for example, and here we changed the state
114
00:06:05,950 --> 00:06:09,630
but this will not be processed instantly by React.
115
00:06:09,630 --> 00:06:11,060
It will be scheduled.
116
00:06:11,060 --> 00:06:12,670
And then the next line thereafter
117
00:06:12,670 --> 00:06:14,980
the state will not be updated yet.
118
00:06:14,980 --> 00:06:19,080
So here we could check if show comments is true
119
00:06:20,670 --> 00:06:24,170
which means the current state is show comments.
120
00:06:24,170 --> 00:06:27,510
Then the button was clicked and dysfunction executes again.
121
00:06:27,510 --> 00:06:30,130
So the goal is to hide them now.
122
00:06:30,130 --> 00:06:32,930
And therefore we could check if not show comments
123
00:06:32,930 --> 00:06:34,320
which means to current status
124
00:06:34,320 --> 00:06:36,170
that the comments are not shown,
125
00:06:36,170 --> 00:06:37,840
then dysfunction executes
126
00:06:37,840 --> 00:06:40,790
and the new state is that they will be shown.
127
00:06:40,790 --> 00:06:42,790
So now we should fetch data
128
00:06:42,790 --> 00:06:44,532
and that's something we could do.
129
00:06:44,532 --> 00:06:49,060
Or alternatively, we use good old useEffect here
130
00:06:50,260 --> 00:06:53,550
and also import that from React
131
00:06:53,550 --> 00:06:55,790
and in here simply add
132
00:06:55,790 --> 00:06:57,810
show comments as a dependency
133
00:06:58,950 --> 00:07:00,800
and in the effect function
134
00:07:00,800 --> 00:07:03,870
we then check if show comments is true
135
00:07:03,870 --> 00:07:06,340
which means that comments are shown.
136
00:07:06,340 --> 00:07:10,120
And if that is true, we wanna fetch the comments.
137
00:07:10,120 --> 00:07:13,770
So then here we can send our request to
138
00:07:13,770 --> 00:07:18,680
slash API slash comments slash event ID.
139
00:07:18,680 --> 00:07:21,600
So this concrete event ID.
140
00:07:21,600 --> 00:07:23,350
We don't need to configure it
141
00:07:23,350 --> 00:07:27,160
because it will be a get request, not a post request.
142
00:07:27,160 --> 00:07:29,020
So the default is fine.
143
00:07:29,020 --> 00:07:33,443
And then we also get our data and parse that.
144
00:07:34,870 --> 00:07:38,020
So that we have the real data here eventually
145
00:07:38,020 --> 00:07:39,313
with which we can work.
146
00:07:40,400 --> 00:07:41,970
Now once we got that data,
147
00:07:41,970 --> 00:07:45,330
we wanna use the data to set the actual comments
148
00:07:45,330 --> 00:07:48,973
which should be rendered here with help of comment list.
149
00:07:49,860 --> 00:07:53,130
So for that we can manage another piece of state
150
00:07:53,130 --> 00:07:57,110
with use state, a list of comments,
151
00:07:57,110 --> 00:08:00,560
where I have my comments
152
00:08:00,560 --> 00:08:04,480
and set comments to update them, like this.
153
00:08:04,480 --> 00:08:06,850
And I wanna set comments once I get them.
154
00:08:06,850 --> 00:08:08,450
So here in this then block
155
00:08:08,450 --> 00:08:12,090
once we got a response, I'll set my comments.
156
00:08:12,090 --> 00:08:13,313
And then the API route
157
00:08:13,313 --> 00:08:16,380
will send back object with a comments key
158
00:08:16,380 --> 00:08:19,240
which then contains this array of comments.
159
00:08:19,240 --> 00:08:21,720
So when we work with that data
160
00:08:21,720 --> 00:08:25,090
I wanna set comments equal to data dot comments,
161
00:08:25,090 --> 00:08:27,220
to access that array of comments,
162
00:08:27,220 --> 00:08:30,400
which is set in that response data package,
163
00:08:30,400 --> 00:08:33,820
which has this comments key as we just saw.
164
00:08:33,820 --> 00:08:36,240
That then sets that comments state
165
00:08:36,240 --> 00:08:38,860
to those fetched comments.
166
00:08:38,860 --> 00:08:42,260
And that means that we can then use them to render them.
167
00:08:42,260 --> 00:08:46,640
So this comments state could then be passed to comment list.
168
00:08:46,640 --> 00:08:50,793
Let's say through items prop, like this,
169
00:08:52,200 --> 00:08:56,320
and now in comment list we need to work with this items prop
170
00:08:56,320 --> 00:08:59,250
And of course you can name that prop differently.
171
00:08:59,250 --> 00:09:02,160
In there I'm not expecting any props right now
172
00:09:02,160 --> 00:09:03,750
but we should change this,
173
00:09:03,750 --> 00:09:05,710
add the props parameter
174
00:09:05,710 --> 00:09:08,690
and extract items from props.
175
00:09:08,690 --> 00:09:10,590
And then we can use items to render
176
00:09:10,590 --> 00:09:12,783
these list items dynamically.
177
00:09:12,783 --> 00:09:15,170
So we just go through items
178
00:09:15,170 --> 00:09:19,000
and map every item into a list item
179
00:09:19,000 --> 00:09:21,260
with content like this.
180
00:09:21,260 --> 00:09:23,310
So cut this and add it here
181
00:09:23,310 --> 00:09:26,640
and get rid of these dummy list items down there.
182
00:09:26,640 --> 00:09:29,580
And then here add a key on every list item
183
00:09:29,580 --> 00:09:31,480
which should be item dot id
184
00:09:32,610 --> 00:09:35,880
here as a text we wanna output item dot text
185
00:09:37,640 --> 00:09:42,640
And for the address here, we wanna output item dot name.
186
00:09:43,640 --> 00:09:46,093
So the name of the user who wrote to comment.
187
00:09:47,012 --> 00:09:49,080
And with that we have our list items,
188
00:09:49,080 --> 00:09:50,260
which should be rendered
189
00:09:50,260 --> 00:09:52,580
and we can now check whether that works.
190
00:09:52,580 --> 00:09:54,380
Let's reload this page
191
00:09:54,380 --> 00:09:57,300
for a single event and click on show comments.
192
00:09:57,300 --> 00:09:59,880
And we see the comments showing up there
193
00:09:59,880 --> 00:10:01,860
and in the network tab,
194
00:10:01,860 --> 00:10:03,960
we can also see the request there.
195
00:10:03,960 --> 00:10:05,890
If I hide the comments
196
00:10:05,890 --> 00:10:08,310
and I click show again they are fetched again.
197
00:10:08,310 --> 00:10:10,250
And hence we see the request here
198
00:10:10,250 --> 00:10:13,990
and we see that it carries the comments in the response.
199
00:10:13,990 --> 00:10:15,640
I guess that's kind of obvious
200
00:10:15,640 --> 00:10:17,892
because we all also a rendered them successfully here.
201
00:10:17,892 --> 00:10:20,750
So it clearly works.
202
00:10:20,750 --> 00:10:25,750
And these are the core tasks which I gave you solved.
203
00:10:25,800 --> 00:10:27,980
We added the newsletter registration.
204
00:10:27,980 --> 00:10:31,532
And now also those comments feature for storing comments
205
00:10:31,532 --> 00:10:35,100
or for at least submitting the comments I guess
206
00:10:35,100 --> 00:10:37,770
and for fetching the dummy comments.
207
00:10:37,770 --> 00:10:40,580
Now it's not very realistic yet though
208
00:10:40,580 --> 00:10:43,330
because we are only working with dummy data
209
00:10:43,330 --> 00:10:46,980
and we're not storing the submitted comment anywhere.
210
00:10:46,980 --> 00:10:49,030
Therefore in the next step,
211
00:10:49,030 --> 00:10:52,300
let me show you an example of how we could talk
212
00:10:52,300 --> 00:10:55,860
to a real database to store our data there
213
00:10:55,860 --> 00:10:58,003
and to get our data from there.
16584
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.