Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,210 --> 00:00:05,300
So now we added a couple of items to our cart,
2
00:00:05,300 --> 00:00:07,860
it would now be nice if we could order them.
3
00:00:07,860 --> 00:00:09,420
For this we have the Order button
4
00:00:09,420 --> 00:00:12,330
but currently that's not doing anything.
5
00:00:12,330 --> 00:00:16,040
I want to make sure that when we click this Order button
6
00:00:16,040 --> 00:00:19,550
we kind of expand this modal,
7
00:00:19,550 --> 00:00:21,900
and we also show a form
8
00:00:21,900 --> 00:00:25,930
where the users should enter the address and the name,
9
00:00:25,930 --> 00:00:29,040
and thereafter we can confirm that input
10
00:00:29,040 --> 00:00:32,659
to send off that order to the backend.
11
00:00:32,659 --> 00:00:35,930
And therefore the first step is to add that form
12
00:00:35,930 --> 00:00:38,810
and to add validation to that form.
13
00:00:38,810 --> 00:00:41,750
For this, let's go back to the code
14
00:00:41,750 --> 00:00:45,060
and there into the Cart folder
15
00:00:45,060 --> 00:00:47,350
where we have this cart component
16
00:00:47,350 --> 00:00:51,070
which renders this modal with our cart data
17
00:00:51,070 --> 00:00:53,950
with the total and with those buttons.
18
00:00:53,950 --> 00:00:58,480
And here below the total above those buttons,
19
00:00:58,480 --> 00:01:03,133
I now wanna render the form where we enter our data.
20
00:01:04,150 --> 00:01:06,140
Before that I'll add a new component
21
00:01:06,140 --> 00:01:08,310
and I'll name it Checkout.js
22
00:01:08,310 --> 00:01:10,750
because it will be the check out form.
23
00:01:10,750 --> 00:01:15,070
And we'll also need a Checkout.module.css file
24
00:01:15,070 --> 00:01:16,410
for the styling.
25
00:01:16,410 --> 00:01:18,410
Though I'll provide the styling to you
26
00:01:18,410 --> 00:01:20,283
once we're done setting up that form.
27
00:01:21,200 --> 00:01:23,640
So here in the checkout component
28
00:01:23,640 --> 00:01:28,640
we now wanna create a new Functional Component.
29
00:01:29,440 --> 00:01:32,720
So we create our component as we always did.
30
00:01:32,720 --> 00:01:36,450
We can import React depends on your project setup
31
00:01:36,450 --> 00:01:38,050
if you need that or not.
32
00:01:38,050 --> 00:01:40,560
I don't need it here so I'll omit it,
33
00:01:40,560 --> 00:01:43,300
and it will already import classes
34
00:01:43,300 --> 00:01:46,863
from ./Checkout.module.css.
35
00:01:47,920 --> 00:01:50,290
Now in here we wanna return a form.
36
00:01:50,290 --> 00:01:53,070
We wanna return a form with the various inputs
37
00:01:53,070 --> 00:01:55,580
for the different values we wanna fetch.
38
00:01:55,580 --> 00:01:59,140
For this I'll wrap every input into a div
39
00:01:59,140 --> 00:02:04,113
with a className of classes.control,
40
00:02:04,990 --> 00:02:07,020
and in there we can have a label
41
00:02:08,169 --> 00:02:13,070
let's say for the name of the user.
42
00:02:13,070 --> 00:02:16,300
So that will be for a input with an idea of name.
43
00:02:16,300 --> 00:02:19,860
And then we have that input thereafter of type text
44
00:02:19,860 --> 00:02:23,390
with an ID of name should be matching that value here
45
00:02:23,390 --> 00:02:24,903
for accessibility.
46
00:02:25,850 --> 00:02:30,740
And we also add a never input
47
00:02:30,740 --> 00:02:34,510
where we fetched the address of the user.
48
00:02:34,510 --> 00:02:37,300
So we want to have to the street
49
00:02:37,300 --> 00:02:39,400
and that should also be able to type text.
50
00:02:40,680 --> 00:02:44,483
However, I'll give it an ID of street,
51
00:02:46,030 --> 00:02:51,030
and I add a new input where I also wanna get the postal code
52
00:02:52,700 --> 00:02:55,470
so that will get an ID of postal
53
00:02:55,470 --> 00:02:57,440
and will also still be of type text.
54
00:02:57,440 --> 00:02:59,890
Technically the postal code looks like a number
55
00:02:59,890 --> 00:03:04,480
but it should be text so that we can also start with zero
56
00:03:04,480 --> 00:03:08,660
without kind of treating this as a null value
57
00:03:08,660 --> 00:03:09,670
which I don't want.
58
00:03:09,670 --> 00:03:11,570
So that will also be text.
59
00:03:11,570 --> 00:03:14,430
And then last but not least let's also wrap the city
60
00:03:14,430 --> 00:03:16,020
which is also text.
61
00:03:16,020 --> 00:03:18,550
So a bunch of text data which we're fetching here.
62
00:03:18,550 --> 00:03:22,410
The name, the street, the postal code, and the city.
63
00:03:22,410 --> 00:03:26,320
And then we also need a button to confirm this.
64
00:03:26,320 --> 00:03:28,863
So I'll say confirm on that button.
65
00:03:30,190 --> 00:03:34,780
Now that we built this basic unstyled checkout component,
66
00:03:34,780 --> 00:03:37,120
we can go back to the Cart
67
00:03:37,120 --> 00:03:42,120
and here I will then import Checkout from ./Checkout
68
00:03:43,350 --> 00:03:45,760
so that new checkout component
69
00:03:45,760 --> 00:03:50,760
and render that here below the total amount.
70
00:03:51,420 --> 00:03:53,780
And for the moment I will always render it.
71
00:03:53,780 --> 00:03:56,270
We'll later make sure that it only shows up
72
00:03:56,270 --> 00:03:57,763
once we click on Order.
73
00:03:58,650 --> 00:04:01,250
If I now save this we have that form here
74
00:04:01,250 --> 00:04:03,500
with the Confirm button.
75
00:04:03,500 --> 00:04:06,340
Now, of course there are a couple of styling issues
76
00:04:06,340 --> 00:04:08,280
which we'll fix in a second,
77
00:04:08,280 --> 00:04:10,120
but before we fix those issues
78
00:04:10,120 --> 00:04:12,680
let's make sure that this form only shows up
79
00:04:12,680 --> 00:04:16,459
when we click Order, and that when it shows up
80
00:04:16,459 --> 00:04:19,963
we actually get rid of these two buttons down there.
81
00:04:21,170 --> 00:04:24,000
And instead we only showed a Confirm button
82
00:04:24,000 --> 00:04:26,700
and maybe also a Cancel button here.
83
00:04:26,700 --> 00:04:29,620
And that we then also have basic functions
84
00:04:29,620 --> 00:04:32,253
that get triggered when we click those buttons.
85
00:04:33,650 --> 00:04:36,480
So, let's make sure that this form only shows up
86
00:04:36,480 --> 00:04:39,120
once we did click on Order.
87
00:04:39,120 --> 00:04:41,060
Now for this in the cart component,
88
00:04:41,060 --> 00:04:42,800
we have this Order button
89
00:04:42,800 --> 00:04:44,890
and currently it's not doing anything.
90
00:04:44,890 --> 00:04:48,270
So of course here we can add onClick listener
91
00:04:48,270 --> 00:04:51,050
the onClick prop to trigger some function
92
00:04:51,050 --> 00:04:52,690
when it is clicked.
93
00:04:52,690 --> 00:04:57,143
So here I'll add a new function orderHandler.
94
00:04:58,720 --> 00:05:00,900
And whenever this is clicked I wanna make sure
95
00:05:00,900 --> 00:05:03,460
that we show the checkout form.
96
00:05:03,460 --> 00:05:07,010
So we wire up the Order button to the orderHandler
97
00:05:07,010 --> 00:05:09,990
to the orderHandler function we just added.
98
00:05:09,990 --> 00:05:13,390
And then to show this checkout component conditionally
99
00:05:13,390 --> 00:05:14,783
we need to useState.
100
00:05:15,720 --> 00:05:19,703
So, let's import useState from React,
101
00:05:20,540 --> 00:05:23,663
and then set up some state for this cart component.
102
00:05:24,850 --> 00:05:28,097
And here we could name the state isCheckingOut
103
00:05:30,010 --> 00:05:31,400
or something like this.
104
00:05:31,400 --> 00:05:32,340
That sounds a bit weird.
105
00:05:32,340 --> 00:05:36,060
Let's maybe name it isCheckout like this
106
00:05:36,060 --> 00:05:37,660
and set IsCheckout.
107
00:05:38,630 --> 00:05:39,910
And initially that's false
108
00:05:39,910 --> 00:05:43,090
because initially we're not in checkout mode,
109
00:05:43,090 --> 00:05:44,280
but I wanna set it to true
110
00:05:44,280 --> 00:05:45,970
if we clicked on the orderHandler,
111
00:05:45,970 --> 00:05:49,393
so here I'll set IsCheckout to true then.
112
00:05:50,970 --> 00:05:53,670
And now we can use the IsCheckout state
113
00:05:53,670 --> 00:05:57,180
to conditionally render the checkout component.
114
00:05:57,180 --> 00:06:02,180
So we can check if isCheckout is truthy and use this syntax
115
00:06:02,320 --> 00:06:05,480
which you saw a lot in the course to render checkout
116
00:06:05,480 --> 00:06:07,423
if isCheckout is truthy.
117
00:06:08,650 --> 00:06:10,640
And with that if we save this
118
00:06:10,640 --> 00:06:12,890
initially there is nothing here,
119
00:06:12,890 --> 00:06:15,233
but if I click on Order we show that.
120
00:06:16,690 --> 00:06:19,070
Now we might be running into some space issues here
121
00:06:19,070 --> 00:06:20,710
if that gets longer and longer,
122
00:06:20,710 --> 00:06:23,263
but we'll work on the styling in a second.
123
00:06:24,200 --> 00:06:25,880
Before we work on the styling,
124
00:06:25,880 --> 00:06:29,310
let's hide these two buttons when we clicked on Order
125
00:06:29,310 --> 00:06:31,910
and let's also add a second button to the front therefore
126
00:06:31,910 --> 00:06:34,680
which still allows us to cancel.
127
00:06:34,680 --> 00:06:37,060
Now to hide these two buttons
128
00:06:37,060 --> 00:06:40,020
we of course just again need to render them conditionally.
129
00:06:40,020 --> 00:06:42,940
And I could therefore just add code
130
00:06:42,940 --> 00:06:45,770
where I check if isnotCheckout
131
00:06:45,770 --> 00:06:48,570
and only if that's the case we render that div.
132
00:06:48,570 --> 00:06:53,530
But to keep the JSX code down there a bit more structured
133
00:06:53,530 --> 00:06:56,230
I'll grab that div with those buttons
134
00:06:56,230 --> 00:07:00,370
and add a new constant here with my modalActions
135
00:07:02,027 --> 00:07:04,937
and store my div in there in the modalActions.
136
00:07:06,370 --> 00:07:08,100
And then it's the modalActions
137
00:07:08,100 --> 00:07:12,010
which I wanna render conditionally if not isCheckout.
138
00:07:12,010 --> 00:07:15,350
So that the JSX down there stays lean
139
00:07:15,350 --> 00:07:18,863
and we have that div with the actions up there.
140
00:07:21,670 --> 00:07:25,870
Okay, so now with that these buttons are gone.
141
00:07:25,870 --> 00:07:28,700
Now let's add a new button to that form.
142
00:07:28,700 --> 00:07:30,500
So for this in the checkout component
143
00:07:30,500 --> 00:07:35,500
I'll add a second button where I say cancel and important
144
00:07:36,300 --> 00:07:38,270
this button will be of type buttons
145
00:07:38,270 --> 00:07:40,630
so that it doesn't submit the form.
146
00:07:40,630 --> 00:07:42,963
Only this button will submit the form.
147
00:07:44,720 --> 00:07:47,990
And speaking off that I wanna handle both button clicks.
148
00:07:47,990 --> 00:07:51,360
If we click the Cancel button I wanna close the modal
149
00:07:51,360 --> 00:07:54,060
and that therefore means that I in the end wanna do
150
00:07:54,060 --> 00:07:57,450
the same thing as I did in the Cart component
151
00:07:57,450 --> 00:08:00,710
when this Close button was clicked.
152
00:08:00,710 --> 00:08:03,880
There we triggered props.onClose.
153
00:08:03,880 --> 00:08:07,310
So in the end I wanna trigger that same function now
154
00:08:07,310 --> 00:08:09,900
from inside the checkout component.
155
00:08:09,900 --> 00:08:12,370
Henceforth the Cancel button on,
156
00:08:12,370 --> 00:08:16,770
click, I expect to get some function through props
157
00:08:16,770 --> 00:08:20,160
which I can forward this event to.
158
00:08:20,160 --> 00:08:23,470
And I will simply expect the onCancel prop here.
159
00:08:23,470 --> 00:08:27,600
It's our component so we can expect whichever prop we want.
160
00:08:27,600 --> 00:08:30,250
And I will expect the onCancel prop
161
00:08:30,250 --> 00:08:32,330
which means that now when we use checkout
162
00:08:32,330 --> 00:08:37,159
in the Cart component, we need to set our onCancel prop.
163
00:08:37,159 --> 00:08:42,150
And as a value I wanna point at the onClose prop value
164
00:08:42,150 --> 00:08:45,430
to forward that event even further.
165
00:08:45,430 --> 00:08:48,440
And now of course we have some props drilling here.
166
00:08:48,440 --> 00:08:49,860
So we're passing props
167
00:08:49,860 --> 00:08:52,100
through multiple levels of components.
168
00:08:52,100 --> 00:08:56,320
And hence we could consider using context instead
169
00:08:56,320 --> 00:08:58,730
for managing the modal visibility.
170
00:08:58,730 --> 00:09:01,480
But I will stick to passing down props here
171
00:09:01,480 --> 00:09:04,140
because I don't wanna make that too complex here
172
00:09:04,140 --> 00:09:06,960
and kind of go into a totally different area
173
00:09:06,960 --> 00:09:08,600
and add another context.
174
00:09:08,600 --> 00:09:11,083
So I will simply pass down my props here.
175
00:09:12,506 --> 00:09:14,220
Now with that if I save this,
176
00:09:14,220 --> 00:09:18,373
if I click Cancel this modal is again closed.
177
00:09:19,590 --> 00:09:20,890
Okay, so that works.
178
00:09:20,890 --> 00:09:24,060
Now when we click Confirm the form will be submitted
179
00:09:24,060 --> 00:09:27,370
and here we then wanna validate the user input
180
00:09:27,370 --> 00:09:31,800
and show errors, show some feedback if something is invalid.
181
00:09:31,800 --> 00:09:33,910
And of course if it is valid
182
00:09:33,910 --> 00:09:37,743
we wanna submit it to the backend, to Firebase.
183
00:09:38,620 --> 00:09:41,950
So for the moment therefore I wanna focus on
184
00:09:41,950 --> 00:09:44,600
doing something on triggering some function
185
00:09:44,600 --> 00:09:48,410
and hence in Checkout.js we can add a new function here
186
00:09:49,425 --> 00:09:54,425
confirmHandler where we get an event object
187
00:09:54,770 --> 00:09:56,530
and we will get that automatically
188
00:09:56,530 --> 00:10:00,300
because I bind this function to the onSubmit event
189
00:10:00,300 --> 00:10:01,393
on the form.
190
00:10:02,830 --> 00:10:04,840
And then you learned that we should call
191
00:10:04,840 --> 00:10:09,050
event.preventDefault to ensure that the browser default
192
00:10:09,050 --> 00:10:13,050
which would be to send an HTTP request is prevented
193
00:10:13,050 --> 00:10:15,563
so that this request is not sent.
194
00:10:17,340 --> 00:10:20,950
And with that this button will trigger this form submission
195
00:10:20,950 --> 00:10:23,870
and will therefore trigger this function.
196
00:10:23,870 --> 00:10:27,000
Now we need to validate the user input.
197
00:10:27,000 --> 00:10:30,190
And for that we need to read what the user entered
198
00:10:30,190 --> 00:10:32,720
and we then wanna show some feedback,
199
00:10:32,720 --> 00:10:35,760
and of course potentially if everything is alright
200
00:10:35,760 --> 00:10:39,430
send the cart data to the backend.
201
00:10:39,430 --> 00:10:40,470
That's step-by-step.
202
00:10:40,470 --> 00:10:43,250
In the next lecture we'll work on validating this
203
00:10:43,250 --> 00:10:45,103
and on showing some feedback.
16134
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.