Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,070 --> 00:00:04,930
So let's handle this submission state.
2
00:00:04,930 --> 00:00:07,370
And for this, we need useState
3
00:00:08,760 --> 00:00:10,990
and then manage a new piece of state
4
00:00:10,990 --> 00:00:12,793
here in the Cart component,
5
00:00:14,040 --> 00:00:19,040
and that is the isSubmitting state and setIsSubmitting.
6
00:00:20,320 --> 00:00:23,980
And, initially, that's false, but I wanna set it to true
7
00:00:23,980 --> 00:00:27,030
as soon as the submitOrderHandler gets called.
8
00:00:27,030 --> 00:00:29,960
So here I'll set isSubmitting,
9
00:00:29,960 --> 00:00:32,023
setIsSubmitting to true.
10
00:00:32,900 --> 00:00:36,540
Then I wanna wait for this fetch function to finish.
11
00:00:36,540 --> 00:00:39,610
And for this, we can await this
12
00:00:39,610 --> 00:00:42,070
and turn this into async function.
13
00:00:42,070 --> 00:00:44,140
Here we have no problem with that
14
00:00:44,140 --> 00:00:46,670
because this is not an effect function,
15
00:00:46,670 --> 00:00:48,810
but, instead, this is a regular function
16
00:00:48,810 --> 00:00:51,283
which gets triggered when we submit the form.
17
00:00:52,240 --> 00:00:56,310
So here we then await, and we get back our response,
18
00:00:56,310 --> 00:00:59,630
and we could now use the response to check the status code
19
00:00:59,630 --> 00:01:01,970
and potentially throw an error,
20
00:01:01,970 --> 00:01:04,170
which we then would have to handle
21
00:01:04,170 --> 00:01:07,210
to also show an error message.
22
00:01:07,210 --> 00:01:11,070
I will not do that here to keep this a bit shorter,
23
00:01:11,070 --> 00:01:13,970
and, instead, I'll assume that it always works.
24
00:01:13,970 --> 00:01:18,010
In reality, of course, and maybe as an extra practice,
25
00:01:18,010 --> 00:01:21,540
you might wanna implement error-handling as well
26
00:01:21,540 --> 00:01:24,040
just as we did it a couple of minutes ago
27
00:01:24,040 --> 00:01:27,230
with the hour fetch request for loading the meals
28
00:01:27,230 --> 00:01:29,780
or in the HTTP module.
29
00:01:29,780 --> 00:01:33,780
Here, to save some time, I will not implement it.
30
00:01:33,780 --> 00:01:36,800
Instead, we submit this, or we setIsSubmitting,
31
00:01:36,800 --> 00:01:38,560
then we await this,
32
00:01:38,560 --> 00:01:40,430
and therefore once this request is done
33
00:01:40,430 --> 00:01:45,430
and we got a response, we can setIsSubmitting back to false.
34
00:01:46,610 --> 00:01:50,600
But now I wanna make sure that we show some success message,
35
00:01:50,600 --> 00:01:53,120
and therefore I'll add another state,
36
00:01:53,120 --> 00:01:58,120
the didSubmit state and setDidSubmit.
37
00:01:59,890 --> 00:02:02,590
And, initially, that's also false,
38
00:02:02,590 --> 00:02:03,970
but this should be set to true
39
00:02:03,970 --> 00:02:07,250
as soon as we did submit our data.
40
00:02:07,250 --> 00:02:11,200
So here we can then setDidSubmit to true
41
00:02:12,650 --> 00:02:15,430
once we're done sending that request,
42
00:02:15,430 --> 00:02:20,430
and now we can work with these two states in our component.
43
00:02:20,640 --> 00:02:25,180
To be precise, I wanna swap the entire content of the modal
44
00:02:25,180 --> 00:02:27,643
when we're submitting and once we did submit.
45
00:02:28,600 --> 00:02:30,230
So I'll add a new constant,
46
00:02:30,230 --> 00:02:33,400
a new constant which I'll name cartModalContent.
47
00:02:37,540 --> 00:02:42,540
And in there I'll store all that code here.
48
00:02:42,640 --> 00:02:45,500
Now, since I now have multiple sibling items here
49
00:02:45,500 --> 00:02:47,280
inside of that constant,
50
00:02:47,280 --> 00:02:52,280
I need to wrap that with React.Fragment.
51
00:02:52,640 --> 00:02:55,570
So we wanna wrap this because, as you learned,
52
00:02:55,570 --> 00:02:58,770
sibling JSX code is not allowed.
53
00:02:58,770 --> 00:03:03,653
And for this, of course, we need to import React from react.
54
00:03:04,730 --> 00:03:09,280
With that, we can wrap that all with a fragment,
55
00:03:09,280 --> 00:03:11,640
and now we get the cartModalContent
56
00:03:11,640 --> 00:03:15,963
which I normally want to render here like this.
57
00:03:17,360 --> 00:03:19,910
But I'll now define another constant,
58
00:03:19,910 --> 00:03:22,747
the isSubmittingModalContent,
59
00:03:24,780 --> 00:03:27,030
and that's the content that should be shown
60
00:03:27,030 --> 00:03:29,113
if we are currently submitting.
61
00:03:30,140 --> 00:03:32,370
And here I'll just add a paragraph
62
00:03:32,370 --> 00:03:35,890
where I say Sending order data.
63
00:03:35,890 --> 00:03:38,283
Now, that should be shown if we are submitting.
64
00:03:39,690 --> 00:03:42,910
Therefore, I only wanna show the cartModalContent
65
00:03:42,910 --> 00:03:46,623
if we're not submitting, hence the exclamation mark.
66
00:03:47,810 --> 00:03:49,620
If we are submitting,
67
00:03:49,620 --> 00:03:52,827
I wanna show the isSubmittingModalContent.
68
00:03:54,180 --> 00:03:55,510
Now, last but not least,
69
00:03:55,510 --> 00:03:58,350
we can, of course, also be done with submitting.
70
00:03:58,350 --> 00:04:03,260
So I'll add another constant, didSubmitModalContent,
71
00:04:03,260 --> 00:04:06,520
and that's another paragraph where I say
72
00:04:06,520 --> 00:04:09,663
Successfully sent the order!
73
00:04:10,730 --> 00:04:12,680
And we could, of course, then also say something
74
00:04:12,680 --> 00:04:14,610
like You will be contacted
75
00:04:14,610 --> 00:04:16,880
once we're done preparing your order,
76
00:04:16,880 --> 00:04:18,649
or anything like that.
77
00:04:18,649 --> 00:04:21,480
Here I'll just say Successfully sent the order,
78
00:04:21,480 --> 00:04:24,577
and that should be showing up if we didSubmit,
79
00:04:26,010 --> 00:04:30,370
then the didSubmitModalContent should be rendered.
80
00:04:30,370 --> 00:04:32,980
And, therefore, we should also tweak these two lines
81
00:04:32,980 --> 00:04:34,920
and only show the cartModalContent
82
00:04:34,920 --> 00:04:39,763
if we're not submitting and we did not submit yet,
83
00:04:41,468 --> 00:04:43,280
didSubmit, like this.
84
00:04:43,280 --> 00:04:46,400
So if not submitting and not didSubmit,
85
00:04:46,400 --> 00:04:48,700
then show the cartModalContent.
86
00:04:48,700 --> 00:04:51,400
I always wanna show the isSubmittingModalContent
87
00:04:51,400 --> 00:04:53,540
if we are submitting.
88
00:04:53,540 --> 00:04:55,390
But otherwise, if we didSubmit,
89
00:04:55,390 --> 00:04:57,430
I wanna show the didSubmitModalContent
90
00:04:57,430 --> 00:05:00,870
maybe if we are not currently submitting.
91
00:05:00,870 --> 00:05:01,810
Though, in this app,
92
00:05:01,810 --> 00:05:06,060
we can't really be submitting and have submitted already.
93
00:05:06,060 --> 00:05:09,520
Nonetheless, having this extra check also won't hurt.
94
00:05:09,520 --> 00:05:11,310
I hope this logic is clear.
95
00:05:11,310 --> 00:05:13,900
With that in place, if we now save this,
96
00:05:13,900 --> 00:05:15,350
we got our cart here.
97
00:05:15,350 --> 00:05:17,883
If I click Order, I now need to fill in my data.
98
00:05:17,883 --> 00:05:19,793
Otherwise, I get errors.
99
00:05:23,090 --> 00:05:26,460
So let's enter something here.
100
00:05:26,460 --> 00:05:29,400
And if I now click Confirm, watch closely.
101
00:05:29,400 --> 00:05:31,793
We see our two other states:
102
00:05:32,853 --> 00:05:37,103
isSubmitting and now the Successfully sent the order state.
103
00:05:38,270 --> 00:05:40,180
Of course, we can now still close the modal
104
00:05:40,180 --> 00:05:41,703
by clicking somewhere else.
105
00:05:43,050 --> 00:05:46,100
We can, of course, also add a new button
106
00:05:46,100 --> 00:05:48,600
to the modal here if we want to.
107
00:05:48,600 --> 00:05:53,120
We can wrap this here into a fragment as well
108
00:05:53,120 --> 00:05:57,000
so that we can have two-sibling components:
109
00:05:57,000 --> 00:06:02,000
the paragraph and then next to that, for example, a button.
110
00:06:02,250 --> 00:06:06,557
So here we could grab our modalActions' div
111
00:06:08,350 --> 00:06:11,640
and insert this next to this paragraph,
112
00:06:11,640 --> 00:06:15,443
get rid of that Order button and just keep the Close button,
113
00:06:16,690 --> 00:06:21,280
and give this a class of button instead of button--alt,
114
00:06:21,280 --> 00:06:23,500
and onClick this will then close the modal.
115
00:06:23,500 --> 00:06:28,183
So if we now save this, if I, again, order this,
116
00:06:29,080 --> 00:06:30,880
enter some data here,
117
00:06:30,880 --> 00:06:33,761
oops, Max and Teststreet,
118
00:06:33,761 --> 00:06:36,453
12345, and Test,
119
00:06:37,530 --> 00:06:40,760
now I can close this with this button.
120
00:06:40,760 --> 00:06:44,160
So now we're showing these different states.
121
00:06:44,160 --> 00:06:47,240
Now, as a last step, I wanna clear the cart
122
00:06:47,240 --> 00:06:49,000
once we did submit
123
00:06:49,000 --> 00:06:50,840
because currently it's never cleared.
124
00:06:50,840 --> 00:06:53,490
Our items stay in the cart.
125
00:06:53,490 --> 00:06:58,480
So to clear that cart, we need to go to our cart-context,
126
00:06:58,480 --> 00:07:00,920
to the CartProvider to be precise,
127
00:07:00,920 --> 00:07:03,730
and there we'll need a new action.
128
00:07:03,730 --> 00:07:07,120
We do have actions for adding and removing single items,
129
00:07:07,120 --> 00:07:10,193
but now we need an action to clear the entire cart.
130
00:07:11,440 --> 00:07:14,650
So I'll add a new if check in this reducer here,
131
00:07:14,650 --> 00:07:17,140
which we added here in this cartReducer,
132
00:07:17,140 --> 00:07:20,823
and check for action.type being equal to CLEAR.
133
00:07:22,430 --> 00:07:25,230
And if we do receive this kind of action,
134
00:07:25,230 --> 00:07:29,313
then I wanna return a new state where the cart is empty.
135
00:07:30,250 --> 00:07:31,568
Now, for this, we can return
136
00:07:31,568 --> 00:07:35,123
the defaultCartState simply, like this.
137
00:07:36,800 --> 00:07:37,780
And, of course, therefore,
138
00:07:37,780 --> 00:07:40,870
technically, we could also omit this if check.
139
00:07:40,870 --> 00:07:43,040
If we do dispatch an action
140
00:07:43,040 --> 00:07:46,830
which is not handled by one of our other if checks,
141
00:07:46,830 --> 00:07:50,100
we would have returned the defaultCartState anyways,
142
00:07:50,100 --> 00:07:52,400
but doing it like this is a bit more explicit.
143
00:07:53,340 --> 00:07:54,420
Now, we need to make sure
144
00:07:54,420 --> 00:07:57,930
that this CLEAR action can be dispatched.
145
00:07:57,930 --> 00:08:01,170
And for this, we wanna add a new function here,
146
00:08:01,170 --> 00:08:06,170
the clearCartHandler, where we don't expect any parameters,
147
00:08:06,380 --> 00:08:08,950
but dispatch a new cart action
148
00:08:08,950 --> 00:08:12,833
where the type is set to CLEAR, like this.
149
00:08:13,880 --> 00:08:17,200
And now on the context, we need a new function.
150
00:08:17,200 --> 00:08:19,920
So in cart-context.js, I'll prepare it
151
00:08:19,920 --> 00:08:22,340
so that we get better auto-completion,
152
00:08:22,340 --> 00:08:24,063
the clearCart function.
153
00:08:26,190 --> 00:08:30,560
And in CartProvider, on this concrete cartContext object
154
00:08:30,560 --> 00:08:32,049
which we're constructing,
155
00:08:32,049 --> 00:08:34,929
we now need to add this clearCart function
156
00:08:34,929 --> 00:08:38,712
and point it to the clearCartHandler, like this.
157
00:08:40,090 --> 00:08:44,300
And now from inside the Cart component, we can execute that.
158
00:08:44,300 --> 00:08:46,433
So here in Cart.js,
159
00:08:48,000 --> 00:08:52,850
when we submit our order in the submitOrderHandler,
160
00:08:52,850 --> 00:08:54,870
once we're done here at the end,
161
00:08:54,870 --> 00:08:58,660
I want to use my cartCtx and call clearCart
162
00:08:59,750 --> 00:09:02,000
to clear the cart once we're done submitting.
163
00:09:03,070 --> 00:09:07,340
So if we now save this one more time and go back,
164
00:09:07,340 --> 00:09:12,063
now if I order here and enter something,
165
00:09:16,420 --> 00:09:21,420
if I confirm this, now my cart was cleared and it is empty.
166
00:09:21,890 --> 00:09:24,440
And, of course, on Firebase, on our back end,
167
00:09:24,440 --> 00:09:26,713
we see all those orders.
13282
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.