62 lines
2.1 KiB
C#
62 lines
2.1 KiB
C#
using Elternbeirat.Contracts;
|
|
using Elternbeirat.PocketBase;
|
|
using Elternbeirat.Web.Shared;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace Elternbeirat.Web.Features.Faqs;
|
|
|
|
public partial class FaqList
|
|
{
|
|
[LoggerMessage(
|
|
Level = LogLevel.Error,
|
|
Message = "Could not load FAQ topics from PocketBase; content source is unavailable.")]
|
|
private static partial void LogSourceUnavailable(
|
|
ILogger logger,
|
|
Exception exception);
|
|
|
|
[Inject]
|
|
private PocketBaseClient PocketBase { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private ILogger<FaqList> Logger { get; set; } = null!;
|
|
|
|
[CascadingParameter]
|
|
private HttpContext? HttpContext { get; set; }
|
|
|
|
// The "faqs" page record supplies the heading and intro text, so an editor
|
|
// owns them in PocketBase rather than them being hard-coded here. Null until
|
|
// loaded, or when no such page exists.
|
|
private Page? _page;
|
|
|
|
private IReadOnlyList<FaqTopic> _topics = [];
|
|
|
|
// Set when the content source could not be reached or read. The markup then
|
|
// shows a "temporarily unavailable" note instead of "no topics", matching
|
|
// the 503 this sets on the response.
|
|
private bool _unavailable;
|
|
|
|
// The page body rendered from Markdown to HTML, ready to emit as a
|
|
// MarkupString in the markup.
|
|
private MarkupString Body => (MarkupString)Markdown.ToHtml(_page?.Body);
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
var token = HttpContext?.RequestAborted ?? CancellationToken.None;
|
|
var pages = await PocketBase.GetPagesAsync(token);
|
|
_page = pages.FirstOrDefault(
|
|
page => string.Equals(page.Slug, "faqs", StringComparison.OrdinalIgnoreCase));
|
|
_topics = await PocketBase.GetFaqTopicsAsync(token);
|
|
}
|
|
catch (PocketBaseUnavailableException exception)
|
|
{
|
|
// An empty page would claim there are no topics. The site works, its
|
|
// data store is down: 503 and an honest note.
|
|
LogSourceUnavailable(Logger, exception);
|
|
_unavailable = true;
|
|
SourceUnavailable.SetStatus(HttpContext);
|
|
}
|
|
}
|
|
}
|