55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
using Elternbeirat.Contracts;
|
|
using Elternbeirat.PocketBase;
|
|
using Elternbeirat.Web.Features.Events;
|
|
using Elternbeirat.Web.Shared;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace Elternbeirat.Web.Features.Embeds;
|
|
|
|
public partial class EventsEmbed
|
|
{
|
|
// Number of upcoming events shown in the teaser; the full list is one link away.
|
|
private const int TeaserCount = 3;
|
|
|
|
[LoggerMessage(
|
|
Level = LogLevel.Warning,
|
|
Message = "Could not load events for an embed from PocketBase; the teaser is omitted.")]
|
|
private static partial void LogSourceUnavailable(
|
|
ILogger logger,
|
|
Exception exception);
|
|
|
|
[Inject]
|
|
private PocketBaseClient PocketBase { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private ILogger<EventsEmbed> Logger { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private TimeProvider Time { get; set; } = null!;
|
|
|
|
[CascadingParameter]
|
|
private HttpContext? HttpContext { get; set; }
|
|
|
|
private IReadOnlyList<Event> _upcomingEvents = [];
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
IReadOnlyList<Event> events;
|
|
try
|
|
{
|
|
events = await PocketBase.GetEventsAsync(HttpContext?.RequestAborted ?? CancellationToken.None);
|
|
}
|
|
catch (PocketBaseUnavailableException exception)
|
|
{
|
|
// Only part of the page fails: omit the teaser, keep the page (and its
|
|
// 200) intact. A missing teaser is degraded, not broken.
|
|
LogSourceUnavailable(Logger, exception);
|
|
return;
|
|
}
|
|
|
|
// Teaser: the soonest upcoming events, earliest first. An event earlier
|
|
// today still counts as upcoming.
|
|
_upcomingEvents = [.. UpcomingEvents.From(events, BerlinClock.Today(Time)).Take(TeaserCount)];
|
|
}
|
|
}
|