Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,220 --> 00:00:04,070
So let's now send a request
2
00:00:04,070 --> 00:00:05,963
once the form is submitted.
3
00:00:07,100 --> 00:00:10,400
Now this checkout component is already a pretty
4
00:00:10,400 --> 00:00:13,040
full component with a lot of code in there
5
00:00:13,040 --> 00:00:16,890
and they are for I'll not add the request logic in there
6
00:00:16,890 --> 00:00:19,850
but instead I want to kind of submit
7
00:00:19,850 --> 00:00:22,170
or ascend the submitted data
8
00:00:22,170 --> 00:00:27,160
back to the cart where we then also have the cart data
9
00:00:27,160 --> 00:00:29,270
which is good because we need that
10
00:00:29,270 --> 00:00:30,720
to send it to the server,
11
00:00:30,720 --> 00:00:33,490
to then send both the carts data
12
00:00:33,490 --> 00:00:35,290
so the items in the cart
13
00:00:35,290 --> 00:00:40,270
as well as the submitted user data to the server.
14
00:00:40,270 --> 00:00:42,380
Now, one important word here,
15
00:00:42,380 --> 00:00:45,820
on a server you should typically never trust
16
00:00:45,820 --> 00:00:48,420
the data you get from a client
17
00:00:48,420 --> 00:00:49,660
because of validation
18
00:00:49,660 --> 00:00:53,820
as we built it here can always be circumvented.
19
00:00:53,820 --> 00:00:55,970
For example, I as a visitor
20
00:00:55,970 --> 00:00:59,310
could edit the loaded JavaScript code.
21
00:00:59,310 --> 00:01:00,460
It's not hidden from me
22
00:01:00,460 --> 00:01:03,360
I can edit it attached to find an article
23
00:01:03,360 --> 00:01:06,450
and a video with more information on that.
24
00:01:06,450 --> 00:01:10,220
Therefore, you typically always wanna validate incoming data
25
00:01:10,220 --> 00:01:12,270
on the server as well.
26
00:01:12,270 --> 00:01:15,630
Now here, we only have a dummy backend with Firebase
27
00:01:15,630 --> 00:01:18,550
and whilst we could actually also add some service
28
00:01:18,550 --> 00:01:19,960
and logic to that,
29
00:01:19,960 --> 00:01:22,780
that's simply not the focus of this course
30
00:01:22,780 --> 00:01:24,750
since this course is about React
31
00:01:24,750 --> 00:01:27,250
which is a client side library.
32
00:01:27,250 --> 00:01:29,760
If you wanna dive into service side code,
33
00:01:29,760 --> 00:01:33,600
service side validation, writing your own server side code
34
00:01:33,600 --> 00:01:36,650
I of course also got courses on this, for example
35
00:01:36,650 --> 00:01:40,150
my Node JS course, but that's just a side note
36
00:01:40,150 --> 00:01:41,850
I just wanted to emphasize
37
00:01:41,850 --> 00:01:46,410
that you should not trust client side data like this.
38
00:01:46,410 --> 00:01:49,660
You for example, typically also would want to
39
00:01:49,660 --> 00:01:53,510
validate those submitted cart item prices
40
00:01:53,510 --> 00:01:56,870
with actual database data on the server.
41
00:01:56,870 --> 00:01:58,400
It's not something we do here
42
00:01:58,400 --> 00:02:00,980
but I want to make you aware of that.
43
00:02:00,980 --> 00:02:03,080
So therefore it's the cart component
44
00:02:03,080 --> 00:02:06,780
where that data should be submitted to the server.
45
00:02:06,780 --> 00:02:09,993
And for this I'll add a new function in that cart component,
46
00:02:11,039 --> 00:02:15,320
submit order handler that could be the name
47
00:02:15,320 --> 00:02:16,280
of that function
48
00:02:17,190 --> 00:02:22,150
and in here, I expect to get the user data
49
00:02:22,150 --> 00:02:24,470
as a argument, as a parameter.
50
00:02:24,470 --> 00:02:26,750
We already get access to the cart data
51
00:02:26,750 --> 00:02:28,700
through the cart context.
52
00:02:28,700 --> 00:02:30,580
So that won't be a problem
53
00:02:30,580 --> 00:02:33,770
and therefore, all we now gotta do is make sure
54
00:02:33,770 --> 00:02:37,090
that this submit order function is called
55
00:02:37,090 --> 00:02:39,160
from inside to the checkout component
56
00:02:39,160 --> 00:02:42,023
and that it receives that user data.
57
00:02:43,070 --> 00:02:46,400
So for that, I'll pass it to the checkout component
58
00:02:46,400 --> 00:02:47,990
through a prop.
59
00:02:47,990 --> 00:02:51,270
Let's say the on submit
60
00:02:51,270 --> 00:02:56,120
or on confirm prop, up to you it's your component
61
00:02:56,120 --> 00:02:59,303
where I point at the submit order handler.
62
00:03:00,390 --> 00:03:04,480
So on confirm as the prop name hence in checkout.
63
00:03:04,480 --> 00:03:06,700
In this confirm handler.
64
00:03:06,700 --> 00:03:10,000
Once we validated everything down here
65
00:03:10,000 --> 00:03:12,803
we now wanna call props.onConfirm
66
00:03:16,010 --> 00:03:18,810
so that prop name we're expecting here
67
00:03:18,810 --> 00:03:20,070
in the cart component.
68
00:03:20,070 --> 00:03:22,420
We wanna call props on confirm
69
00:03:22,420 --> 00:03:25,083
and now forward that user data,
70
00:03:25,950 --> 00:03:27,700
which should be an object
71
00:03:27,700 --> 00:03:30,840
because I'm not expecting multiple arguments
72
00:03:30,840 --> 00:03:32,830
for the name street, city and so on.
73
00:03:32,830 --> 00:03:36,940
But I am expecting just one argument here
74
00:03:36,940 --> 00:03:38,823
the collected user data.
75
00:03:40,210 --> 00:03:43,670
So let's group that user data into an object here
76
00:03:43,670 --> 00:03:46,580
where we set the name equal to the entered name,
77
00:03:46,580 --> 00:03:49,840
not the validity but just the name.
78
00:03:49,840 --> 00:03:52,850
Set a street field to the entered street
79
00:03:53,780 --> 00:03:57,200
set the city fields to the entered city
80
00:03:57,200 --> 00:04:01,220
and set the postal code
81
00:04:01,220 --> 00:04:04,450
to the entered postal code like this.
82
00:04:04,450 --> 00:04:07,890
And with that we pass the data from the checkout component
83
00:04:07,890 --> 00:04:09,583
to the cart component.
84
00:04:10,590 --> 00:04:14,400
In there we now wanna send the request to the backend
85
00:04:14,400 --> 00:04:16,740
and we wanna send both the user data
86
00:04:16,740 --> 00:04:19,269
as well as the cart data.
87
00:04:19,269 --> 00:04:22,840
Now for this, we can again use the fetch function
88
00:04:23,930 --> 00:04:26,990
and send the request to Firebase.
89
00:04:26,990 --> 00:04:29,790
So to our Firebase, URL here
90
00:04:31,220 --> 00:04:33,600
and then to a new Node of our choice.
91
00:04:33,600 --> 00:04:35,460
For example, order.json
92
00:04:36,910 --> 00:04:39,380
you don't need to create it in advance
93
00:04:39,380 --> 00:04:41,920
it will be treated dynamically.
94
00:04:41,920 --> 00:04:43,660
And the important thing now is
95
00:04:43,660 --> 00:04:46,770
that this should be a post request.
96
00:04:46,770 --> 00:04:49,480
So we need to pass this configuration object
97
00:04:49,480 --> 00:04:51,420
as a second argument to fetch
98
00:04:51,420 --> 00:04:53,363
and set method to post.
99
00:04:54,510 --> 00:04:57,270
We then also need to add to the body
100
00:04:57,270 --> 00:05:00,330
and set this to JSON.stringify
101
00:05:00,330 --> 00:05:02,930
since we need to send JSON data
102
00:05:02,930 --> 00:05:05,120
and pass in all our data
103
00:05:05,120 --> 00:05:07,803
and that's an object with the user data.
104
00:05:08,640 --> 00:05:10,750
So here we could set a user field
105
00:05:10,750 --> 00:05:13,360
and pass the user data we're getting here
106
00:05:13,360 --> 00:05:15,290
as a value for that field
107
00:05:15,290 --> 00:05:18,710
and then maybe add a ordered items field
108
00:05:18,710 --> 00:05:21,230
and that should then be our cart.
109
00:05:21,230 --> 00:05:23,530
Now for this we have our cart context
110
00:05:23,530 --> 00:05:25,040
where we get our items.
111
00:05:25,040 --> 00:05:29,190
So we can then set cart context items
112
00:05:29,190 --> 00:05:32,280
as a value for the ordered items.
113
00:05:32,280 --> 00:05:36,290
And that's the data we're then sending to the backend.
114
00:05:36,290 --> 00:05:39,410
Now just like this, without anything else
115
00:05:39,410 --> 00:05:41,210
it should already work.
116
00:05:41,210 --> 00:05:43,060
We're not handling the response yet.
117
00:05:43,060 --> 00:05:45,610
We're not handling errors or anything like that
118
00:05:45,610 --> 00:05:47,660
but we are sending a post request.
119
00:05:47,660 --> 00:05:49,840
So this should already work.
120
00:05:49,840 --> 00:05:51,040
If we save that
121
00:05:52,200 --> 00:05:54,020
and I do have some data entered here
122
00:05:54,020 --> 00:05:55,240
I do have some cart data
123
00:05:55,240 --> 00:05:58,930
and I click confirm that should have worked.
124
00:05:58,930 --> 00:06:01,600
And indeed, if we check the Firebase console
125
00:06:01,600 --> 00:06:03,960
you should see an order's Node now
126
00:06:03,960 --> 00:06:08,960
with a new auto generated ID for this newly added item.
127
00:06:09,060 --> 00:06:12,820
And then in there we have user with the user data we entered
128
00:06:12,820 --> 00:06:17,023
and ordered items with the cart data we submitted.
129
00:06:17,950 --> 00:06:19,620
So that is working.
130
00:06:19,620 --> 00:06:22,660
If it's not working, definitely double check your code
131
00:06:22,660 --> 00:06:24,000
compare it to mine
132
00:06:24,000 --> 00:06:27,170
because that should now be working.
133
00:06:27,170 --> 00:06:28,650
Now therefore, as a last step,
134
00:06:28,650 --> 00:06:31,840
I of course wanna change the user experience a little bit.
135
00:06:31,840 --> 00:06:35,550
I want to clear what we see on the screen here.
136
00:06:35,550 --> 00:06:37,720
If we clicked, confirm
137
00:06:37,720 --> 00:06:39,240
and show a loading spinner
138
00:06:39,240 --> 00:06:42,760
and then access message if the cart was submitted
139
00:06:42,760 --> 00:06:43,940
and of course the cart
140
00:06:43,940 --> 00:06:46,453
should then also be cleared thereafter.
10601
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.