Skip to content

Commit b8b59e5

Browse files
committed
Update 05-checkout-with-validation
1 parent 570d56c commit b8b59e5

25 files changed

+223
-118
lines changed

docs/05-checkout-with-validation.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ To implement `PlaceOrder`, copy the method with that name from `Index.razor` int
2929
@code {
3030
async Task PlaceOrder()
3131
{
32-
var newOrderId = await HttpClient.PostJsonAsync<int>("orders", OrderState.Order);
32+
var response = await HttpClient.PostAsJsonAsync("orders", OrderState.Order);
33+
var newOrderId = await response.Content.ReadFromJsonAsync<int>();
3334
OrderState.ResetOrder();
3435
NavigationManager.NavigateTo($"myorders/{newOrderId}");
3536
}
@@ -311,6 +312,18 @@ The green/red styling is achieved by applying CSS classes, so you can change the
311312

312313
`InputText` isn't the only built-in input component, though it is the only one we need in this case. Others include `InputCheckbox`, `InputDate`, `InputSelect`, and more.
313314

315+
## Bonus challenge
316+
317+
If you're keen and have time, can you prevent accidental double-submission of the form?
318+
319+
Currently, if it takes a while for the form post to reach the server, the user could click submit multiple times and send multiple copies of their order. Try declaring a `bool isSubmitting` property that, when `true`, results in the *Place order* button being disabled. Remember to set it back to `false` when the submission is completed (successfully or not), otherwise the user might get stuck.
320+
321+
To check your solution works, you might want to slow down the server by adding the following line at the top of `PlaceOrder()` inside `OrdersController.cs`:
322+
323+
```cs
324+
await Task.Delay(5000); // Wait 5 seconds
325+
```
326+
314327
## Up next
315328

316329
Up next we'll add [authentication and authorization](https://github.com/dotnet-presentations/blazor-workshop/blob/master/docs/06-authentication-and-authorization.md)

save-points/05-checkout-with-validation/BlazingPizza.Client/App.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</Found>
55
<NotFound>
66
<LayoutView Layout="typeof(MainLayout)">
7-
<div class="main">Page not found</div>
7+
<div class="main">Sorry, there's nothing at this address.</div>
88
</LayoutView>
99
</NotFound>
1010
</Router>

save-points/05-checkout-with-validation/BlazingPizza.Client/BlazingPizza.Client.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="$(BlazorVersion)" />
1111
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="$(BlazorVersion)" PrivateAssets="all" />
12-
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="$(BlazorVersion)" />
13-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(AspNetCoreVersion)" />
12+
<PackageReference Include="System.Net.Http.Json" Version="$(SystemNetHttpJsonVersion)" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="$(BlazorVersion)" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

save-points/05-checkout-with-validation/BlazingPizza.Client/Pages/Checkout.razor

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</div>
1818
</div>
1919

20-
<button type="submit" class="checkout-button btn btn-warning">
20+
<button type="submit" class="checkout-button btn btn-warning" disabled="@isSubmitting">
2121
Place order
2222
</button>
2323

@@ -26,9 +26,13 @@
2626
</div>
2727

2828
@code {
29+
bool isSubmitting;
30+
2931
async Task PlaceOrder()
3032
{
31-
var newOrderId = await HttpClient.PostJsonAsync<int>("orders", OrderState.Order);
33+
isSubmitting = true;
34+
var response = await HttpClient.PostAsJsonAsync("orders", OrderState.Order);
35+
var newOrderId= await response.Content.ReadFromJsonAsync<int>();
3236
OrderState.ResetOrder();
3337
NavigationManager.NavigateTo($"myorders/{newOrderId}");
3438
}

save-points/05-checkout-with-validation/BlazingPizza.Client/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@
6161

6262
protected override async Task OnInitializedAsync()
6363
{
64-
specials = await HttpClient.GetJsonAsync<List<PizzaSpecial>>("specials");
64+
specials = await HttpClient.GetFromJsonAsync<List<PizzaSpecial>>("specials");
6565
}
6666
}

save-points/05-checkout-with-validation/BlazingPizza.Client/Pages/MyOrders.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@
4343

4444
protected override async Task OnParametersSetAsync()
4545
{
46-
ordersWithStatus = await HttpClient.GetJsonAsync<List<OrderWithStatus>>("orders");
46+
ordersWithStatus = await HttpClient.GetFromJsonAsync<List<OrderWithStatus>>("orders");
4747
}
4848
}

save-points/05-checkout-with-validation/BlazingPizza.Client/Pages/OrderDetails.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
try
5858
{
5959
invalidOrder = false;
60-
orderWithStatus = await HttpClient.GetJsonAsync<OrderWithStatus>($"orders/{OrderId}");
60+
orderWithStatus = await HttpClient.GetFromJsonAsync<OrderWithStatus>($"orders/{OrderId}");
6161
}
6262
catch (Exception ex)
6363
{

save-points/05-checkout-with-validation/BlazingPizza.Client/Shared/ConfigurePizzaDialog.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
protected async override Task OnInitializedAsync()
7171
{
72-
toppings = await HttpClient.GetJsonAsync<List<Topping>>("toppings");
72+
toppings = await HttpClient.GetFromJsonAsync<List<Topping>>("toppings");
7373
}
7474

7575
void ToppingSelected(ChangeEventArgs e)

save-points/05-checkout-with-validation/BlazingPizza.Client/Shared/MainLayout.razor

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
@inherits LayoutComponentBase
22

33
<div class="top-bar">
4-
<img class="logo" src="img/logo.svg" />
4+
<a class="logo" href="">
5+
<img src="img/logo.svg" />
6+
</a>
57

68
<NavLink href="" class="nav-tab" Match="NavLinkMatch.All">
79
<img src="img/pizza-slice.svg" />

save-points/05-checkout-with-validation/BlazingPizza.Client/_Imports.razor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
@using System.Net.Http
2+
@using System.Net.Http.Headers
3+
@using System.Net.Http.Json
24
@using Microsoft.AspNetCore.Authorization
35
@using Microsoft.AspNetCore.Components.Authorization
46
@using Microsoft.AspNetCore.Components.Forms
57
@using Microsoft.AspNetCore.Components.Routing
68
@using Microsoft.AspNetCore.Components.Web
9+
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
710
@using Microsoft.JSInterop
811
@using BlazingPizza.Client
912
@using BlazingPizza.Client.Shared

0 commit comments

Comments
 (0)