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,610
Let's add some logic
2
00:00:03,610 --> 00:00:05,689
to the EventsSearch component.
3
00:00:05,689 --> 00:00:06,800
We get this form,
4
00:00:06,800 --> 00:00:09,300
but at the moment when this button is pressed
5
00:00:09,300 --> 00:00:10,980
and the form is submitted,
6
00:00:10,980 --> 00:00:13,970
we're not doing anything with that submitted data.
7
00:00:13,970 --> 00:00:15,770
And that should change.
8
00:00:15,770 --> 00:00:18,350
Therefore, I'll start by adding a nested function
9
00:00:18,350 --> 00:00:20,290
in the EventsSearch component
10
00:00:20,290 --> 00:00:22,830
and I'll name this function submitHandler.
11
00:00:22,830 --> 00:00:25,000
That's the function that should be triggered
12
00:00:25,000 --> 00:00:27,560
when the form is submitted.
13
00:00:27,560 --> 00:00:29,150
So, on the form element,
14
00:00:29,150 --> 00:00:31,390
we can add the onSubmit prop
15
00:00:31,390 --> 00:00:34,300
and point at submitHandler like this.
16
00:00:34,300 --> 00:00:36,730
Don't execute it, just point at it.
17
00:00:36,730 --> 00:00:40,030
So, that submitHandler is executed by react
18
00:00:40,030 --> 00:00:42,150
when the form is submitted.
19
00:00:42,150 --> 00:00:45,470
Now, as you know, for example, from my react course,
20
00:00:45,470 --> 00:00:48,380
we automatically get an event object here,
21
00:00:48,380 --> 00:00:51,910
because we're listening to a default Dom event.
22
00:00:51,910 --> 00:00:56,740
And here we should call event dot preventDefault
23
00:00:56,740 --> 00:01:01,710
to prevent the browser default off sending an http request
24
00:01:01,710 --> 00:01:03,150
which I don't wanna do here
25
00:01:03,150 --> 00:01:05,069
and which would reload our page
26
00:01:05,069 --> 00:01:08,900
and therefore lose all our applications state.
27
00:01:08,900 --> 00:01:11,970
Instead, I wanna handle this submission with JavaScript
28
00:01:11,970 --> 00:01:15,120
and therefore we prevent that default.
29
00:01:15,120 --> 00:01:16,410
And once that is prevented,
30
00:01:16,410 --> 00:01:19,970
we can now read the selected year and month.
31
00:01:19,970 --> 00:01:22,730
Now for that, we could set up two way binding
32
00:01:22,730 --> 00:01:25,130
with use state and so on,
33
00:01:25,130 --> 00:01:28,140
but since I'm only interested in the values once
34
00:01:28,140 --> 00:01:29,700
when the form is submitted,
35
00:01:29,700 --> 00:01:31,530
I'll instead work with refs.
36
00:01:31,530 --> 00:01:35,050
Another key react feature which you should be aware of.
37
00:01:35,050 --> 00:01:38,420
If you're not, definitely take a react refresher,
38
00:01:38,420 --> 00:01:40,910
or take my react to complete guide course,
39
00:01:40,910 --> 00:01:44,793
where I dive into all react concepts in great depth.
40
00:01:45,870 --> 00:01:49,550
So, here we'll then import useRef from react,
41
00:01:49,550 --> 00:01:52,370
and then we can set up our refs here
42
00:01:52,370 --> 00:01:56,120
and we'll need two refs since we have two inputs.
43
00:01:56,120 --> 00:01:58,520
We'll have the yearInputRef
44
00:01:58,520 --> 00:02:01,962
and hence I'll create my first ref with useRef
45
00:02:01,962 --> 00:02:04,150
and the monthInputRef.
46
00:02:04,150 --> 00:02:06,683
And hence here I'll create this as well.
47
00:02:07,543 --> 00:02:09,660
And now that get these two refs created,
48
00:02:09,660 --> 00:02:13,180
we need to connect them to select elements.
49
00:02:13,180 --> 00:02:14,900
So, on the year select element,
50
00:02:14,900 --> 00:02:16,890
I'll add to the special ref prop
51
00:02:16,890 --> 00:02:19,800
and point at the yearInputRef,
52
00:02:19,800 --> 00:02:22,150
and on the month select element,
53
00:02:22,150 --> 00:02:26,523
I'll also add the ref prop and point at the monthInputRef.
54
00:02:27,610 --> 00:02:29,320
Now, the refs are connected
55
00:02:29,320 --> 00:02:31,690
and therefor now in this submitHandler,
56
00:02:31,690 --> 00:02:34,490
we can get the selectedYear
57
00:02:34,490 --> 00:02:37,700
by accessing yearInputRef dot current.
58
00:02:37,700 --> 00:02:40,350
You always use dot current to get access
59
00:02:40,350 --> 00:02:43,990
to the actual value the ref is connected to,
60
00:02:43,990 --> 00:02:46,110
so, in this case to the select element.
61
00:02:46,110 --> 00:02:50,070
And then since this gives us access to the select element,
62
00:02:50,070 --> 00:02:53,220
so, to the JavaScript object representing
63
00:02:53,220 --> 00:02:55,010
that select element,
64
00:02:55,010 --> 00:02:57,480
we can access the value property,
65
00:02:57,480 --> 00:03:00,780
which every select JavaScript object has.
66
00:03:00,780 --> 00:03:02,740
So, every JavaScript object,
67
00:03:02,740 --> 00:03:06,170
representing a select element has a value property.
68
00:03:06,170 --> 00:03:07,890
Therefore, we can access this here
69
00:03:07,890 --> 00:03:10,870
to get access to the selected value.
70
00:03:10,870 --> 00:03:14,060
And we repeat that for the selected month.
71
00:03:14,060 --> 00:03:17,330
So, with monthInputRef dot current dot value,
72
00:03:17,330 --> 00:03:19,860
we get access to the selected value
73
00:03:19,860 --> 00:03:21,893
on the month select element.
74
00:03:23,080 --> 00:03:26,370
And now we just need to kind of do something
75
00:03:26,370 --> 00:03:28,670
with those values.
76
00:03:28,670 --> 00:03:32,640
And I don't want to utilize them here in EventsSearch,
77
00:03:32,640 --> 00:03:34,400
even though we could do that,
78
00:03:34,400 --> 00:03:38,790
but instead I want to pass them to my AllEventsPage.
79
00:03:39,710 --> 00:03:44,710
And in there, I then want to programmatically navigate
80
00:03:44,940 --> 00:03:48,280
to that slug page here.
81
00:03:48,280 --> 00:03:51,100
And here we need programmatic navigation
82
00:03:51,100 --> 00:03:54,380
because you wanna do that once some action completed.
83
00:03:54,380 --> 00:03:57,810
In this case, once the form submission completed
84
00:03:57,810 --> 00:04:01,120
and we extracted all the form values.
85
00:04:01,120 --> 00:04:05,390
So, that's a typical example for programmatic navigation.
86
00:04:05,390 --> 00:04:07,390
Hence as a first step,
87
00:04:07,390 --> 00:04:09,870
I do expect that on the props
88
00:04:09,870 --> 00:04:12,380
of this EventsSearch component,
89
00:04:12,380 --> 00:04:16,250
I have a prop let's say named onSearch.
90
00:04:16,250 --> 00:04:17,410
This name is up to you
91
00:04:17,410 --> 00:04:20,829
but I'll name it onSearch following this convention
92
00:04:20,829 --> 00:04:23,430
because I expect that onSearch,
93
00:04:23,430 --> 00:04:26,310
the value for this prop is a function,
94
00:04:26,310 --> 00:04:28,500
which I therefor can execute here.
95
00:04:28,500 --> 00:04:32,220
And it should be a function which accepts two parameters,
96
00:04:32,220 --> 00:04:36,313
the selected year and the selected month.
97
00:04:37,340 --> 00:04:40,640
So, I'll call this function and now we need to make sure
98
00:04:40,640 --> 00:04:42,860
that such a function is passed
99
00:04:42,860 --> 00:04:47,130
into EventsSearch through the onSearch prop.
100
00:04:47,130 --> 00:04:50,710
So, in this index JS file in the events folder,
101
00:04:50,710 --> 00:04:53,140
so therefore on the AllEventsPage.
102
00:04:53,140 --> 00:04:56,063
We now need to set up this function here.
103
00:04:57,040 --> 00:05:01,000
Here we need to add a new function in this component.
104
00:05:01,000 --> 00:05:04,840
The findEventsHandler function, for example,
105
00:05:04,840 --> 00:05:08,370
which expects a year and the month in this order
106
00:05:08,370 --> 00:05:11,200
because that's how I'm then calling this function here.
107
00:05:11,200 --> 00:05:13,700
We're calling this function and we're passing in a year
108
00:05:13,700 --> 00:05:17,213
as a first argument and a month as a second argument.
109
00:05:18,150 --> 00:05:20,020
Hence, that's what I expect here.
110
00:05:20,020 --> 00:05:22,110
And then we do something with that.
111
00:05:22,110 --> 00:05:24,380
And it's now this function which is passed
112
00:05:24,380 --> 00:05:28,270
to the EventsSearch component through the onSearch prop
113
00:05:28,270 --> 00:05:30,500
which we just use in there.
114
00:05:30,500 --> 00:05:33,770
So, here I then pass at findEventsHandler.
115
00:05:33,770 --> 00:05:35,410
We don't execute it here.
116
00:05:35,410 --> 00:05:36,860
We just point edit.
117
00:05:36,860 --> 00:05:39,360
So that it's executed for us,
118
00:05:39,360 --> 00:05:42,530
by this EventsSearch component here
119
00:05:42,530 --> 00:05:44,530
with this line of code,
120
00:05:44,530 --> 00:05:48,883
after the form was submitted and we extracted our values.
121
00:05:50,200 --> 00:05:54,840
So, then here findEventsHandler will execute.
122
00:05:54,840 --> 00:05:58,583
And in here we now want to programmatically navigate.
123
00:05:59,860 --> 00:06:03,043
So for that, we can import useRouter
124
00:06:04,400 --> 00:06:09,400
from next slash router like this,
125
00:06:11,390 --> 00:06:15,300
and then call useRouter here outside
126
00:06:15,300 --> 00:06:19,460
of this inner function directly in the component function.
127
00:06:19,460 --> 00:06:22,390
All react hooks need to be called directly
128
00:06:22,390 --> 00:06:23,880
in your component function,
129
00:06:23,880 --> 00:06:26,543
not in any nested block statements.
130
00:06:27,460 --> 00:06:30,970
And that gives us access to the router like this.
131
00:06:30,970 --> 00:06:34,220
And on this router inside of the findEventsHandler,
132
00:06:34,220 --> 00:06:38,400
we can then call push to go to a different page.
133
00:06:38,400 --> 00:06:42,600
That's what you also learned in the previous course section.
134
00:06:42,600 --> 00:06:46,320
Now I'll construct the fullPath I wanna go to here
135
00:06:46,320 --> 00:06:48,290
with a template literal,
136
00:06:48,290 --> 00:06:51,990
and the fullPath will be slash events slash
137
00:06:51,990 --> 00:06:54,760
and now to dynamic segments so
138
00:06:54,760 --> 00:06:57,200
that we reach this slug here.
139
00:06:57,200 --> 00:07:00,820
And the first, the dynamic segment will be my year
140
00:07:00,820 --> 00:07:01,740
and then slash,
141
00:07:01,740 --> 00:07:04,903
and then the second dynamic segment which is month.
142
00:07:05,790 --> 00:07:07,200
Now, just to be clear,
143
00:07:07,200 --> 00:07:09,720
we could have more as segments here,
144
00:07:09,720 --> 00:07:13,240
and this slug route would still be triggered.
145
00:07:13,240 --> 00:07:15,870
It simply consumes an unlimited amount
146
00:07:15,870 --> 00:07:19,210
of segments after slash events.
147
00:07:19,210 --> 00:07:23,060
If we only have one segment, slug will not be triggered
148
00:07:23,060 --> 00:07:25,540
because we have a more specific route folder
149
00:07:25,540 --> 00:07:27,650
with the eventId file.
150
00:07:27,650 --> 00:07:30,120
And next JS is pretty smart about that.
151
00:07:30,120 --> 00:07:32,280
And it sees that for one argument,
152
00:07:32,280 --> 00:07:34,960
for one dynamic parameter after events,
153
00:07:34,960 --> 00:07:37,200
we have a specific page,
154
00:07:37,200 --> 00:07:41,250
but for two or more we have no specific pages
155
00:07:41,250 --> 00:07:44,520
and therefore then this slug will kick in.
156
00:07:44,520 --> 00:07:47,870
So for this path will reach the slug page here.
157
00:07:47,870 --> 00:07:51,093
And therefor, that's the fullPath I wanna navigate to.
158
00:07:52,080 --> 00:07:54,500
With all that if we now save this,
159
00:07:54,500 --> 00:07:58,507
if we now select, let's say May, 2021
160
00:07:58,507 --> 00:08:00,480
and I click on Find Events,
161
00:08:00,480 --> 00:08:05,380
we are taken to slash events slash 2021 slash five,
162
00:08:05,380 --> 00:08:07,920
which is May, 2021.
163
00:08:07,920 --> 00:08:11,340
And we are on that Filtered Events page.
164
00:08:11,340 --> 00:08:13,113
Just as expected.
165
00:08:14,130 --> 00:08:18,050
So, now as a last step before the final polishing,
166
00:08:18,050 --> 00:08:23,050
let's now load the proper events for this filter criteria.
12992
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.