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:05,600
Now to explore working with those API routes
2
00:00:05,600 --> 00:00:09,120
let's go back to our standard index.js file.
3
00:00:09,120 --> 00:00:11,550
So to this standard homepage route
4
00:00:11,550 --> 00:00:15,310
which is not an API route, and in there
5
00:00:15,310 --> 00:00:18,290
I will add a basic form, which I'll not style
6
00:00:18,290 --> 00:00:22,600
in any way for the moment where I then want to add a input
7
00:00:22,600 --> 00:00:26,310
of type email, let's say, and above
8
00:00:26,310 --> 00:00:30,907
that a label where I say your email address
9
00:00:32,870 --> 00:00:36,210
with a htmlfor tag off email
10
00:00:37,300 --> 00:00:41,000
and then here an ideal email for accessibility.
11
00:00:41,000 --> 00:00:45,660
And then I'll put that into a div just to kind of put
12
00:00:45,660 --> 00:00:49,163
this into its own block and force a line break thereafter.
13
00:00:50,140 --> 00:00:54,170
And then I'll copy that div and create a never input
14
00:00:54,170 --> 00:00:59,170
with an id of message or let's name it, feedback.
15
00:01:01,520 --> 00:01:03,900
The type here is text however,
16
00:01:03,900 --> 00:01:07,840
and here we just say Your Feedback
17
00:01:07,840 --> 00:01:10,360
and actually let's use a text area
18
00:01:10,360 --> 00:01:12,500
that's more realistic I guess.
19
00:01:12,500 --> 00:01:14,400
So we have a text area with an id
20
00:01:14,400 --> 00:01:19,230
of feedback and let's say under a number of, five rows.
21
00:01:21,140 --> 00:01:25,483
And then we add a button to submit that form send feedback.
22
00:01:26,620 --> 00:01:28,190
If we adjust the homepage like
23
00:01:28,190 --> 00:01:32,690
this and we reload just local host free thousand
24
00:01:32,690 --> 00:01:34,420
we see this form here.
25
00:01:34,420 --> 00:01:35,740
Now it is ugly.
26
00:01:35,740 --> 00:01:37,140
You could add styling
27
00:01:37,140 --> 00:01:39,850
but this is totally not about the styling,
28
00:01:39,850 --> 00:01:42,180
this is just a dummy form, which I add
29
00:01:42,180 --> 00:01:46,980
so that I can show you how we can interact with API routes.
30
00:01:46,980 --> 00:01:49,050
We'll see a more beautiful example
31
00:01:49,050 --> 00:01:52,520
in the next course section when we apply what we learn
32
00:01:52,520 --> 00:01:56,083
in this section to our first course project.
33
00:01:57,120 --> 00:02:01,510
So, here we accept the email address and some user feedback.
34
00:02:01,510 --> 00:02:03,400
And of course, when that button is clicked
35
00:02:03,400 --> 00:02:07,960
the idea is that this feedback is sent to some database
36
00:02:07,960 --> 00:02:11,750
for example, that it's stored in some database.
37
00:02:11,750 --> 00:02:13,720
Now we shouldn't talk
38
00:02:13,720 --> 00:02:17,850
to a database from our front end application.
39
00:02:17,850 --> 00:02:20,090
It would be kind of difficult to implement
40
00:02:20,090 --> 00:02:21,590
and that would be difficult
41
00:02:21,590 --> 00:02:25,240
for a reason because it's highly insecure
42
00:02:25,240 --> 00:02:28,240
if we would try to add JavaScript code
43
00:02:28,240 --> 00:02:31,260
to this component to talk to a database
44
00:02:31,260 --> 00:02:35,280
because we would have to expose our database credentials
45
00:02:35,280 --> 00:02:37,450
in this front end code for this.
46
00:02:37,450 --> 00:02:39,950
And that would be highly insecure,
47
00:02:39,950 --> 00:02:41,560
attached you'll find an article
48
00:02:41,560 --> 00:02:45,250
and video where I talk about the dissident greater detail.
49
00:02:45,250 --> 00:02:47,000
So that's not what we wanna do.
50
00:02:47,000 --> 00:02:51,350
Instead that's the perfect scenario for an API route.
51
00:02:51,350 --> 00:02:54,870
We fetched that user input on the front-end
52
00:02:54,870 --> 00:02:57,240
and once the user then clicks that button
53
00:02:57,240 --> 00:03:00,610
we send the request queue to the API route.
54
00:03:00,610 --> 00:03:05,300
So to our own API, and then in that API route
55
00:03:05,300 --> 00:03:07,590
we could connect to a database.
56
00:03:07,590 --> 00:03:09,800
Because then there we can safely
57
00:03:09,800 --> 00:03:12,150
do so because the code written
58
00:03:12,150 --> 00:03:16,720
in the API route will not be exposed to the front end.
59
00:03:16,720 --> 00:03:20,090
It will not be exposed to our website visitors.
60
00:03:20,090 --> 00:03:23,883
So here we could securely talk to a database.
61
00:03:24,750 --> 00:03:29,280
And therefore now here in index.js
62
00:03:29,280 --> 00:03:31,443
I now wanna get that user input when the button
63
00:03:31,443 --> 00:03:34,530
is clicked and send the request.
64
00:03:34,530 --> 00:03:37,680
Now for this, I will work
65
00:03:37,680 --> 00:03:42,120
with references here to get references to my inputs.
66
00:03:42,120 --> 00:03:45,860
So I'll import { useRef } from 'react'
67
00:03:45,860 --> 00:03:48,580
to use this ref feature.
68
00:03:48,580 --> 00:03:51,453
And then I'll set up two references here.
69
00:03:52,630 --> 00:03:55,383
The first one is for the email input,
70
00:03:56,530 --> 00:03:58,050
and then the second one
71
00:03:58,050 --> 00:04:01,993
thereafter is for the feedback input.
72
00:04:03,120 --> 00:04:06,260
And once we got these references created, we can assign
73
00:04:06,260 --> 00:04:09,840
them to our elements with the special ref prop
74
00:04:09,840 --> 00:04:13,320
and that's just standard react code has nothing
75
00:04:13,320 --> 00:04:15,480
to do with API routes.
76
00:04:15,480 --> 00:04:18,810
And here I'll then just point at the emailInputRef
77
00:04:18,810 --> 00:04:22,260
and here I'll point at the {feedbackInputRef}.
78
00:04:23,900 --> 00:04:26,260
Now we need a handler for the form submission.
79
00:04:26,260 --> 00:04:30,330
So I'll add a function here, submit form handler,
80
00:04:30,330 --> 00:04:31,543
the name is up to you.
81
00:04:33,040 --> 00:04:36,170
Where we get an event object automatically
82
00:04:36,170 --> 00:04:40,970
because I'll bind that to the onSubmit prop on the form.
83
00:04:40,970 --> 00:04:43,360
So it will be triggered when that built
84
00:04:43,360 --> 00:04:46,880
in submit event is triggered when that is fired.
85
00:04:46,880 --> 00:04:49,400
And then we get access to that event object
86
00:04:49,400 --> 00:04:51,500
and we need access because here I wanna
87
00:04:51,500 --> 00:04:56,500
call preventDefault to prevent that browser default
88
00:04:56,730 --> 00:05:00,620
where the page would basically be reloaded
89
00:05:00,620 --> 00:05:04,360
because the browser sends a request automatically.
90
00:05:04,360 --> 00:05:07,430
I don't want that browser behavior here.
91
00:05:07,430 --> 00:05:09,480
Instead I wanna step in and send
92
00:05:09,480 --> 00:05:12,513
the request myself through JavaScript.
93
00:05:13,650 --> 00:05:18,470
For this year we can now extract the email text
94
00:05:18,470 --> 00:05:22,070
by reaching out to the emailInputRef, and then there
95
00:05:22,070 --> 00:05:25,760
since it's a Ref.current, that's always how refs work.
96
00:05:25,760 --> 00:05:29,200
And then .value because in .current
97
00:05:29,200 --> 00:05:32,950
a pointer at this input object is saved.
98
00:05:32,950 --> 00:05:36,900
And this input object just as the text area object
99
00:05:36,900 --> 00:05:40,350
in JavaScript always has a value property
100
00:05:40,350 --> 00:05:44,140
which holds the current value of those inputs.
101
00:05:44,140 --> 00:05:46,663
So it holds the value entered by the user.
102
00:05:47,670 --> 00:05:49,270
That's the entered email.
103
00:05:49,270 --> 00:05:52,070
And then we can also get the entered feedback,
104
00:05:52,070 --> 00:05:53,310
of course by reaching out
105
00:05:53,310 --> 00:05:56,973
to the feedbackInputRef.current.value; as well.
106
00:05:58,420 --> 00:06:00,000
Now we could add validation
107
00:06:00,000 --> 00:06:03,270
and check if the email or feedback is empty
108
00:06:03,270 --> 00:06:06,060
we could do that here to then show an error message
109
00:06:06,060 --> 00:06:08,240
to the user, but I'll not do it here
110
00:06:08,240 --> 00:06:10,923
because it's not the focus of this section.
111
00:06:11,820 --> 00:06:14,300
Instead now we've got these two values
112
00:06:14,300 --> 00:06:15,870
and we might wanna send them
113
00:06:15,870 --> 00:06:19,970
to our API route here to then, accept them,
114
00:06:19,970 --> 00:06:23,570
extract them and save them in a database.
115
00:06:23,570 --> 00:06:25,360
And therefore in the next step
116
00:06:25,360 --> 00:06:27,803
we will work on that API route.
9260
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.