Files
Elternbeirat/Elternbeirat.Web/Features/Faqs/FaqList.razor.cs
T

64 lines
2.2 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 FAQs from PocketBase; content source is unavailable.")]
private static partial void LogSourceUnavailable(
ILogger logger,
Exception exception);
// German heading per topic. Unknown topics fall back to the raw key so a new
// topic in PocketBase still shows up rather than being silently dropped.
private static readonly Dictionary<string, string> TopicLabels =
new(StringComparer.Ordinal)
{
["mensa"] = "Mensa und Mittagessen",
["schliessfach"] = "Schließfächer",
["elterneuro"] = "Eltern-Euro",
["elternarbeit"] = "Elternarbeit und Mitmachen",
};
[Inject]
private PocketBaseClient PocketBase { get; set; } = null!;
[Inject]
private ILogger<FaqList> Logger { get; set; } = null!;
[CascadingParameter]
private HttpContext? HttpContext { get; set; }
private IReadOnlyList<IGrouping<string, Faq>> _groups = [];
// Set when the content source could not be reached or read. The markup then
// shows a "temporarily unavailable" note instead of "no questions", matching
// the 503 this sets on the response.
private bool _unavailable;
protected override async Task OnInitializedAsync()
{
try
{
var faqs = await PocketBase.GetFaqsAsync(HttpContext?.RequestAborted ?? CancellationToken.None);
_groups = [.. faqs.GroupBy(faq => faq.Topic)];
}
catch (PocketBaseUnavailableException exception)
{
// An empty page would claim there are no questions. The site works, its
// data store is down: 503 and an honest note.
LogSourceUnavailable(Logger, exception);
_unavailable = true;
SourceUnavailable.SetStatus(HttpContext);
}
}
private static string TopicLabel(string topic)
=> TopicLabels.GetValueOrDefault(topic, topic);
}