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

Make site resilient to basket API being down #9

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 14 additions & 9 deletions src/WebApp/Components/Layout/CartMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@code {
IDisposable? basketStateSubscription;
private IReadOnlyCollection<BasketItem>? basketItems;
private bool? basketUnavailable;

[CascadingParameter]
public required HttpContext HttpContext { get; set; }
Expand All @@ -30,14 +31,7 @@
// component to re-render with the updated data.
basketStateSubscription = Basket.NotifyOnChange(EventCallback.Factory.Create(this, UpdateBasketItemsAsync));

try
{
await UpdateBasketItemsAsync();
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized)
{
await LogOutService.LogOutAsync(HttpContext);
}
await UpdateBasketItemsAsync();
}

public void Dispose()
Expand All @@ -47,6 +41,17 @@

private async Task UpdateBasketItemsAsync()
{
basketItems = await Basket.GetBasketItemsAsync();
try
{
basketItems = await Basket.GetBasketItemsAsync();
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized)
{
await LogOutService.LogOutAsync(HttpContext);
}
catch
{
basketUnavailable = true;
}
}
}
30 changes: 26 additions & 4 deletions src/WebApp/Components/Pages/Cart/CartPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
<SectionContent SectionName="page-header-title">Shopping bag</SectionContent>

<div class='cart'>
@if (basketItems is null)
@if (basketUnavailable == true)
{
<p class="validation-message">
Your shopping bag is currently unavailable. Please try again later.
</p>
}
else if (basketItems is null)
{
<p>Loading...</p>
}
Expand Down Expand Up @@ -86,6 +92,7 @@

@code {
private IReadOnlyCollection<BasketItem>? basketItems;
private bool? basketUnavailable;

[SupplyParameterFromForm]
public int? UpdateQuantityId { get; set; }
Expand All @@ -95,7 +102,14 @@

protected override async Task OnInitializedAsync()
{
basketItems = await Basket.GetBasketItemsAsync();
try
{
basketItems = await Basket.GetBasketItemsAsync();
}
catch
{
basketUnavailable = true;
}
}

private decimal? TotalPrice => basketItems?.Sum(i => i.Quantity * i.UnitPrice);
Expand All @@ -112,7 +126,15 @@
{
var id = UpdateQuantityId!.Value;
var quantity = UpdateQuantityValue!.Value;
await Basket.SetQuantityAsync(id, quantity);
basketItems = await Basket.GetBasketItemsAsync();

try
{
await Basket.SetQuantityAsync(id, quantity);
basketItems = await Basket.GetBasketItemsAsync();
}
catch
{
basketUnavailable = true;
}
}
}
32 changes: 29 additions & 3 deletions src/WebApp/Components/Pages/Item/ItemPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
Add to shopping bag
</button>
}
else
else if (!isLoggedIn)
{
<button type="submit" title="Log in to purchase">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" xmlns="http://www.w3.org/2000/svg">
Expand All @@ -54,6 +54,10 @@
{
<p class="validation-message">There was an issue adding the item to your shopping bag. <br />Please try again.</p>
}
else if (isLoggedIn && basketUnavailable == true)
{
<p class="validation-message">Your shopping bag is currently unavailable. <br />Please try again later.</p>
}

@if (numInCart > 0)
{
Expand All @@ -75,6 +79,7 @@ else if (notFound)
private int numInCart;
private bool isLoggedIn;
private bool notFound;
private bool? basketUnavailable;

[Parameter]
public int ItemId { get; set; }
Expand All @@ -88,17 +93,32 @@ else if (notFound)

protected override async Task OnInitializedAsync()
{
isLoggedIn = HttpContext.User.Identity?.IsAuthenticated == true;

try
{
isLoggedIn = HttpContext.User.Identity?.IsAuthenticated == true;
item = await CatalogService.GetCatalogItem(ItemId);
await UpdateNumInCartAsync();
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
HttpContext.Response.StatusCode = 404;
notFound = true;
}

StateHasChanged();

if (!notFound && item is not null)
{
try
{
await UpdateNumInCartAsync();
basketUnavailable = false;
}
catch
{
// Ignore basket errors on page load
}
}
}

private async Task AddToCartAsync()
Expand All @@ -116,6 +136,8 @@ else if (notFound)
await BasketState.AddAsync(item);
await UpdateNumInCartAsync();

basketUnavailable = false;

if (WasLoggedOut)
{
// Remove the flag from the URL by forcing a full page navigation
Expand All @@ -131,6 +153,10 @@ else if (notFound)
Nav.NavigateTo(loginUrl, HttpContext, forceFullNavigation: true);
return;
}
catch
{
basketUnavailable = true;
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/WebApp/Extensions/NavigationManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public static void NavigateTo(this NavigationManager navigationManager, string u
}

// Workaround to force Blazor enhanced navigation to do a full redirect to an internal URL from an enhanced form handler
httpContext.Response.Headers["blazor-enhanced-nav-redirect-location"] = url;
if (!httpContext.Response.HasStarted)
{
httpContext.Response.Headers["blazor-enhanced-nav-redirect-location"] = url;
}
else
{
throw new InvalidOperationException("Cannot force a full redirect when response has already started, e.g. when Streaming Rendering is enabled.");
}
}
}
Loading