Skip to content

Commit

Permalink
feat: Show error page when no blog post was foumd
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Feb 27, 2024
1 parent 16ff395 commit 3ed6d67
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@
@inject IOptions<ApplicationConfiguration> AppConfiguration
@inject IOptions<ProfileInformation> ProfileInformation

@if (BlogPost is null)
@if (isLoading)
{
<Loading></Loading>
}
else if (!isLoading && BlogPost is null)
{
<div class="m-auto text-center" id="no-blog-post-error">
<h1 class="fs-1">404 - o((⊙﹏⊙))o</h1>
<br/>
<p>I really looked hard but I couldn't find the page you are looking for.</p>
<p>Go back to <a href="/">safety</a></p>
</div>
}
else
{
<PageTitle>@BlogPost.Title</PageTitle>
Expand Down Expand Up @@ -77,14 +86,18 @@ else
[Parameter]
public string Slug { get; set; }

private bool isLoading;

private string OgDataImage => BlogPost.PreviewImageUrlFallback ?? BlogPost.PreviewImageUrl;
private string BlogPostCanoncialUrl => $"blogPost/{BlogPost.Id}";

private BlogPost BlogPost { get; set; }

protected override async Task OnParametersSetAsync()
{
isLoading = true;
BlogPost = await BlogPostRepository.GetByIdAsync(BlogPostId);
isLoading = false;
}

protected override async Task OnAfterRenderAsync(bool firstRender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public async Task ShouldSetStructuredData()
structuredData.Instance.PreviewFallbackImage.Should().Be("image2");
}

[Fact]
public void ShouldShowErrorPageWhenBlogPostNotFound()
{
using var ctx = new TestContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
RegisterComponents(ctx);

var cut = ctx.RenderComponent<ShowBlogPostPage>();

cut.FindAll(".blogpost-content").Should().HaveCount(0);
cut.FindAll("#no-blog-post-error").Should().HaveCount(1);
}

private void RegisterComponents(TestContextBase ctx, ILocalStorageService localStorageService = null)
{
ctx.Services.AddScoped(_ => Repository);
Expand Down

0 comments on commit 3ed6d67

Please sign in to comment.