Files
Khwezi Mngoma c2d6fd1b17
continuous-integration/drone/pr Build is passing
Stable notifications page
2026-05-17 16:11:01 +02:00

144 lines
4.9 KiB
C#

using LiteCharms.Features.Models;
using LiteCharms.Features.Shop.Notifications;
using LiteCharms.Features.Shop.Notifications.Models;
namespace ShopAdmin.Components.Pages;
public partial class Notifications(NotificationService notificationService, IToastService ToastService)
{
protected IQueryable<Notification> NotificationQueryable
{
get
{
var query = NotificationItems.AsQueryable();
if (!string.IsNullOrWhiteSpace(SearchFilter))
{
query = query.Where(n =>
(n.Subject != null && n.Subject.Contains(SearchFilter, StringComparison.OrdinalIgnoreCase)) ||
(n.SenderAddress != null && n.SenderAddress.Contains(SearchFilter, StringComparison.OrdinalIgnoreCase)) ||
(n.RecipientAddress != null && n.RecipientAddress.Contains(SearchFilter, StringComparison.OrdinalIgnoreCase)));
}
return query;
}
}
protected List<Notification> NotificationItems { get; set; } = [];
protected PaginationState Pagination { get; set; } = new() { ItemsPerPage = 10 };
protected bool IsLoading { get; set; } = true;
protected string SearchFilter { get; set; } = string.Empty;
protected int MaxRecords { get; set; } = 100;
protected DateTime FromDate { get; set; } = DateTime.Today.AddDays(-30);
protected DateTime ToDate { get; set; } = DateTime.Today.AddDays(1);
protected string FromDateBind
{
get => FromDate.ToString("yyyy-MM-dd");
set
{
if (DateTime.TryParse(value, out var parsedDate))
{
FromDate = parsedDate;
}
}
}
protected string ToDateBind
{
get => ToDate.ToString("yyyy-MM-dd");
set
{
if (DateTime.TryParse(value, out var parsedDate))
{
ToDate = parsedDate;
}
}
}
protected int TotalCount => NotificationItems.Count;
protected int ErrorCount => NotificationItems.Count(n => n.HasError);
protected int PendingCount => NotificationItems.Count(n => !n.Processed && !n.HasError);
protected double SuccessRate => TotalCount > 0
? Math.Round(((double)(TotalCount - ErrorCount) / TotalCount) * 100, 1)
: 100.0;
protected GridSort<Notification> SortByOrigin => GridSort<Notification>.ByAscending(n => n.Platform);
protected GridSort<Notification> SortByPriority => GridSort<Notification>.ByDescending(n => n.Priority);
protected GridSort<Notification> SortByCreatedAt => GridSort<Notification>.ByDescending(n => n.CreatedAt);
protected GridSort<Notification> SortBySender => GridSort<Notification>.ByAscending(n => n.SenderAddress);
protected GridSort<Notification> SortBySubject => GridSort<Notification>.ByAscending(n => n.Subject);
protected GridSort<Notification> SortByRecipient => GridSort<Notification>.ByAscending(n => n.RecipientAddress);
protected GridSort<Notification> SortByStatus => GridSort<Notification>.ByAscending(n => n.HasError);
protected override async Task OnInitializedAsync() => await LoadNotificationDataAsync();
protected async Task HandleRefreshClickAsync()
{
await LoadNotificationDataAsync();
ToastService.ShowInfo("Console metrics and telemetry logs refreshed.");
}
private async Task LoadNotificationDataAsync()
{
IsLoading = true;
var dateRange = new DateRange()
{
From = DateOnly.FromDateTime(FromDate),
To = DateOnly.FromDateTime(ToDate),
MaxRecords = MaxRecords
};
var result = await notificationService.GetNotificationsAsync(dateRange);
if (result.IsSuccess) NotificationItems = [.. result.Value];
if (result.IsFailed)
{
var errorMsg = result.Errors.FirstOrDefault()?.Message ?? "Unable to load telemetry log data.";
ToastService.ShowError(errorMsg);
NotificationItems = [];
}
IsLoading = false;
}
protected string GetRowClass(Notification item)
{
var errorStatus = item.HasError ? "row-has-error" : "row-success";
var priorityStatus = $"row-priority-{item.Priority.ToString().ToLower()}";
return $"{errorStatus} {priorityStatus}";
}
protected async Task HandleRetryRowAsync(Notification item)
{
var updateRequest = new UpdateNotification
{
NotificationId = item.Id,
Processed = false,
HasError = false,
Errors = null
};
var result = await notificationService.UpdateNotificationAsync(updateRequest);
await LoadNotificationDataAsync();
if (result.IsSuccess) ToastService.ShowSuccess($"{item.Subject}, has been marked for reprocessing.");
if (result.IsFailed) ToastService.ShowError("Failed to update notification state execution.");
}
}