This repository has been archived on 2021-04-30. You can view files and clone it, but cannot push or open issues or pull requests.
blazing-workshop/BlazingPizza.Client/Pages/Checkout.razor

71 lines
1.9 KiB
Plaintext
Raw Normal View History

2020-12-17 15:52:43 +00:00
@page "/checkout"
@attribute [Authorize]
@inject OrderState OrderState
@inject OrdersClient OrdersClient
@inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime
<div class="main">
<EditForm Model="OrderState.Order.DeliveryAddress" OnValidSubmit="PlaceOrder">
<div class="checkout-cols">
<div class="checkout-order-details">
<h4>Bestellung überprüfen</h4>
<OrderReview Order="OrderState.Order" />
</div>
<div class="checkout-delivery-address">
<h4>Lieferaddresse...</h4>
<AddressEditor Address="OrderState.Order.DeliveryAddress" />
</div>
</div>
<button type="submit" class="checkout-button btn btn-warning" disabled="@isSubmitting">
Bestellen
</button>
<DataAnnotationsValidator />
</EditForm>
</div>
@code {
bool isSubmitting;
protected override void OnInitialized()
{
// In the background, ask if they want to be notified about order updates
_ = RequestNotificationSubscriptionAsync();
}
async Task RequestNotificationSubscriptionAsync()
{
var subscription = await JSRuntime.InvokeAsync<NotificationSubscription>("blazorPushNotifications.requestSubscription");
if (subscription != null)
{
try
{
await OrdersClient.SubscribeToNotifications(subscription);
}
catch (AccessTokenNotAvailableException ex)
{
ex.Redirect();
}
}
}
async Task PlaceOrder()
{
isSubmitting = true;
try
{
var newOrderId = await OrdersClient.PlaceOrder(OrderState.Order);
OrderState.ResetOrder();
NavigationManager.NavigateTo($"myorders/{newOrderId}");
}
catch (AccessTokenNotAvailableException ex)
{
ex.Redirect();
}
}
}