@using WebUI.Models
@using WebUI.Services
@inject NotificationService NotificationService
@rendermode InteractiveServer
@foreach (var notification in _Notifications)
{
@notification.Message
@notification.Timestamp.ToString("hh:mm:ss tt")
}
@code {
private readonly List _Notifications = new();
protected override void OnInitialized()
{
NotificationService.OnNotify += ShowNotification;
}
private void ShowNotification(Notification notification)
{
_ = InvokeAsync(async () =>
{
_Notifications.Add(notification);
StateHasChanged();
await Task.Delay(notification.DelayMs);
_Notifications.Remove(notification);
StateHasChanged();
});
}
private string GetCssClass(NotificationType type) => type switch
{
NotificationType.Success => "alert-success",
NotificationType.Error=> "alert-danger",
NotificationType.Warning => "alert-warning",
NotificationType.Info => "alert-info",
_ => "alert-secondary"
};
private void DismissNotification(Notification notification)
{
_Notifications.Remove(notification);
}
}