179 lines
5.4 KiB
Plaintext
179 lines
5.4 KiB
Plaintext
@page "/Notifications"
|
|
@attribute [Authorize]
|
|
@using salesbook.Shared.Components.Layout
|
|
@using salesbook.Shared.Components.Layout.Spinner
|
|
@using salesbook.Shared.Components.SingleElements
|
|
@using salesbook.Shared.Core.Dto.PageState
|
|
@using salesbook.Shared.Core.Entity
|
|
@using salesbook.Shared.Core.Interface.IntegryApi
|
|
@using salesbook.Shared.Core.Messages.Notification
|
|
@inject NotificationState Notification
|
|
@inject NewPushNotificationService NewPushNotificationService
|
|
@inject IJSRuntime JS
|
|
@inject IIntegryNotificationRestClient IntegryNotificationRestClient
|
|
|
|
<HeaderLayout Title="Notifiche" />
|
|
|
|
<div class="container">
|
|
@if (Loading)
|
|
{
|
|
<SpinnerLayout FullScreen="true" />
|
|
|
|
}
|
|
else
|
|
{
|
|
if (Notification.ReceivedNotifications.IsNullOrEmpty() && Notification.UnreadNotifications.IsNullOrEmpty() && Notification.NotificationsRead.IsNullOrEmpty())
|
|
{
|
|
<NoDataAvailable Text="Nessuna notifica meno recente" />
|
|
}
|
|
else
|
|
{
|
|
<div class="list" id="list">
|
|
@foreach(var notification in Notification.ReceivedNotifications)
|
|
{
|
|
<NotificationCard Unread="true" Notification="notification" />
|
|
}
|
|
|
|
@foreach(var notification in Notification.UnreadNotifications)
|
|
{
|
|
<NotificationCard Unread="true" Notification="notification" />
|
|
}
|
|
|
|
@foreach (var notification in Notification.NotificationsRead)
|
|
{
|
|
<NotificationCard Notification="notification" />
|
|
}
|
|
</div>
|
|
}
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
private DotNetObjectReference<Notifications>? _objectReference;
|
|
private bool Loading { get; set; } = true;
|
|
|
|
protected override Task OnInitializedAsync()
|
|
{
|
|
_objectReference = DotNetObjectReference.Create(this);
|
|
NewPushNotificationService.OnNotificationReceived += NewNotificationReceived;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
await JS.InvokeVoidAsync("initNotifications", _objectReference);
|
|
|
|
if (firstRender)
|
|
{
|
|
await LoadData();
|
|
Loading = false;
|
|
StateHasChanged();
|
|
}
|
|
}
|
|
|
|
private async Task LoadData()
|
|
{
|
|
var allNotifications = await IntegryNotificationRestClient.Get();
|
|
var allIds = allNotifications.Select(n => n.Id).ToHashSet();
|
|
|
|
Notification.ReceivedNotifications = Notification.ReceivedNotifications
|
|
.Where(r => !allIds.Contains(r.Id))
|
|
.ToList();
|
|
|
|
Notification.UnreadNotifications = allNotifications
|
|
.Where(x =>
|
|
x.WtbDeviceNotifications == null ||
|
|
x.WtbDeviceNotifications.Any(y => y.ReadDate == null))
|
|
.ToList();
|
|
|
|
Notification.NotificationsRead = allNotifications
|
|
.Where(x =>
|
|
x.WtbDeviceNotifications != null &&
|
|
x.WtbDeviceNotifications.All(y => y.ReadDate != null))
|
|
.ToList();
|
|
|
|
OrderNotificationList();
|
|
}
|
|
|
|
private void NewNotificationReceived(WtbNotification notification)
|
|
{
|
|
InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
[JSInvokable]
|
|
public async Task Delete(string id)
|
|
{
|
|
Loading = true;
|
|
_ = InvokeAsync(StateHasChanged);
|
|
|
|
if (!long.TryParse(id, out var notificationId)) return;
|
|
|
|
var removed = false;
|
|
|
|
if (Notification.ReceivedNotifications.RemoveAll(x => x.Id == notificationId) > 0)
|
|
removed = true;
|
|
else if (Notification.UnreadNotifications.RemoveAll(x => x.Id == notificationId) > 0)
|
|
removed = true;
|
|
else if (Notification.NotificationsRead.RemoveAll(x => x.Id == notificationId) > 0)
|
|
removed = true;
|
|
|
|
if (!removed) return;
|
|
|
|
OrderNotificationList();
|
|
Loading = false;
|
|
_ = InvokeAsync(StateHasChanged);
|
|
|
|
_ = Task.Run(() =>
|
|
{
|
|
_ = IntegryNotificationRestClient.Delete(notificationId);
|
|
});
|
|
}
|
|
|
|
[JSInvokable]
|
|
public async Task MarkAsRead(string id)
|
|
{
|
|
Loading = true;
|
|
_ = InvokeAsync(StateHasChanged);
|
|
|
|
var notificationId = long.Parse(id);
|
|
|
|
var wtbNotification = Notification.ReceivedNotifications
|
|
.FindLast(x => x.Id == notificationId);
|
|
|
|
if (wtbNotification == null)
|
|
{
|
|
wtbNotification = Notification.UnreadNotifications
|
|
.FindLast(x => x.Id == notificationId);
|
|
|
|
Notification.UnreadNotifications.Remove(wtbNotification!);
|
|
}
|
|
else
|
|
{
|
|
Notification.ReceivedNotifications.Remove(wtbNotification);
|
|
}
|
|
|
|
wtbNotification = await IntegryNotificationRestClient.MarkAsRead(notificationId);
|
|
Notification.NotificationsRead.Add(wtbNotification);
|
|
|
|
OrderNotificationList();
|
|
Loading = false;
|
|
StateHasChanged();
|
|
}
|
|
|
|
private void OrderNotificationList()
|
|
{
|
|
Notification.ReceivedNotifications = Notification.ReceivedNotifications
|
|
.OrderByDescending(x => x.StartDate).ToList();
|
|
|
|
Notification.UnreadNotifications = Notification.UnreadNotifications
|
|
.OrderByDescending(x => x.StartDate).ToList();
|
|
|
|
Notification.NotificationsRead = Notification.NotificationsRead
|
|
.OrderByDescending(x => x.StartDate).ToList();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_objectReference?.Dispose();
|
|
}
|
|
} |