Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eShopLite: Don't checkout if there's no user basket #51

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions samples/eShopLite/eShopLite.Frontend/Components/Cart.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<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,28 +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}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the NavigateTo only be called if there is a customer basket as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still in a POST request so we need to perform the redirect (post-redirect-get).

Expand Down