Skip to content

Commit

Permalink
Update eShopLite to not call basket checkout if no current basket (#1087
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards authored Nov 29, 2023
1 parent c6df9c3 commit a8ac29a
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions samples/eShopLite/MyFrontend/Components/Cart.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<div class="justify-content-end">
@if (basketIsAvailable)
{
<EditForm action="@Navigation.ToAbsolutePath(Navigation.Uri)" Model="this" FormName="checkout" OnSubmit="HandleCheckout" data-enhance>
<EditForm Model="this" FormName="checkout" OnSubmit="HandleCheckout" data-enhance>
<button type="submit" class="align-content-end cart-button">
<span class="fa-stack fa-lg cart-stack pa-4">
<i class="fa fa-shopping-cart fa-stack-4x"></i>
<i class="fa fa-stack-1x badge">
@itemsInCart
@(customerBasket?.TotalItemCount ?? 0)
</i>
</span>
</button>
Expand All @@ -18,27 +18,25 @@
</div>

@code {
CustomerBasket? customerBasket;
bool basketIsAvailable;
int itemsInCart = 0;

[Parameter]
public EventCallback<bool> BasketAvailabilityChanged { get; set; }

protected override async Task OnInitializedAsync()
{
var (basket, isAvailable) = await BasketClient.GetBasketAsync("user");
(customerBasket, basketIsAvailable) = await BasketClient.GetBasketAsync("user");

if (basket is not null)
{
itemsInCart = basket.TotalItemCount;
}
basketIsAvailable = isAvailable;
await BasketAvailabilityChanged.InvokeAsync(basketIsAvailable);
}

private async Task HandleCheckout()
{
await BasketClient.CheckoutBasketAsync("user");
if (customerBasket is not null)
{
await BasketClient.CheckoutBasketAsync("user");
}

// Preserve query string
Navigation.NavigateTo($"/{new Uri(Navigation.Uri).Query}");
Expand Down

0 comments on commit a8ac29a

Please sign in to comment.