Files
Heimdall/src/Heimdall.Web/Components/Pages/NotificationsPage.razor
T

88 lines
2.6 KiB
Plaintext
Raw Normal View History

@page "/notifications"
@using Heimdall.Application.DTOs
@attribute [Authorize]
@layout MainLayout
@inject HeimdallApiClient Api
@inject NotificationsHubClient Hub
@inject UiText Text
@inject ISnackbar Snackbar
<MudText Typo="Typo.h1" Class="mb-4">@Text["NotificationsPage_Title"]</MudText>
@if (_loading)
{
<MudProgressLinear Indeterminate="true" />
}
else if (_failed)
{
<MudAlert Severity="Severity.Error">@Text["NotificationsPage_Failed"]</MudAlert>
}
else if (_items.Count == 0)
{
<MudAlert Severity="Severity.Info">@Text["NotificationsPage_Empty"]</MudAlert>
}
else
{
<div class="d-flex justify-end mb-3">
<MudButton Variant="Variant.Outlined" Disabled="@(!_items.Any(x => !x.IsRead))" OnClick="MarkAllReadAsync">
@Text["NotificationsPage_MarkAllRead"]
</MudButton>
</div>
@foreach (var item in _items)
{
<MudPaper Class="pa-4 mb-2" Elevation="1">
<div class="d-flex justify-space-between align-start">
<div>
<MudText Typo="Typo.h6">@item.Title</MudText>
<MudText>@item.Message</MudText>
<MudText Typo="Typo.caption" Color="Color.Secondary">@item.CreatedAt.ToLocalTime().ToString("g")</MudText>
</div>
@if (!item.IsRead)
{
<MudButton Variant="Variant.Text" OnClick="@(() => MarkReadAsync(item.Id))">@Text["NotificationsPage_MarkRead"]</MudButton>
}
</div>
</MudPaper>
}
}
@code {
private List<NotificationDto> _items = [];
private bool _loading = true;
private bool _failed;
protected override async Task OnInitializedAsync() => await LoadAsync();
private async Task LoadAsync()
{
_loading = true;
_failed = false;
try
{
var result = await Api.GetNotificationsAsync();
_items = result?.Items.ToList() ?? [];
_failed = result is null;
}
catch (HttpRequestException) { _failed = true; }
finally { _loading = false; }
}
private async Task MarkReadAsync(Guid id)
{
if (await Api.MarkNotificationAsReadAsync(id))
{
await LoadAsync();
await Hub.RefreshUnreadCountAsync();
}
else Snackbar.Add(Text["NotificationsPage_UpdateFailed"], Severity.Error);
}
private async Task MarkAllReadAsync()
{
if (await Api.MarkAllNotificationsAsReadAsync())
{
await LoadAsync();
await Hub.RefreshUnreadCountAsync();
}
else Snackbar.Add(Text["NotificationsPage_UpdateFailed"], Severity.Error);
}
}