From 477e0bc32da61e74f8b75a675240edc9f9fb9eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Pilgaard=20Gr=C3=B8ndahl?= Date: Sun, 20 Aug 2023 23:24:48 +0200 Subject: [PATCH] work on displaying unread chats --- src/web/Client/Features/Chat/ChatService.cs | 4 +++- .../Features/Chat/LargeChatComponent.razor | 19 +++++++++++++++---- src/web/Server/Features/Chat/ChatApi.cs | 6 ++++-- src/web/Shared/Chat/ChatDto.cs | 4 +++- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/web/Client/Features/Chat/ChatService.cs b/src/web/Client/Features/Chat/ChatService.cs index 587dc845..f89ed43e 100644 --- a/src/web/Client/Features/Chat/ChatService.cs +++ b/src/web/Client/Features/Chat/ChatService.cs @@ -48,11 +48,13 @@ public async ValueTask> GetChats(string userId) var newChats = HandleApiResponse(await _chatClient.GetChats(userId, cachedChats?.Count ?? 0)); if (cachedChats is null) { - return newChats; + return newChats.OrderByDescending(chat => chat.HasUnreadMessages).ToList(); } cachedChats.AddRange(newChats); + cachedChats = cachedChats.OrderByDescending(chat => chat.HasUnreadMessages).ToList(); + return cachedChats; } diff --git a/src/web/Client/Features/Chat/LargeChatComponent.razor b/src/web/Client/Features/Chat/LargeChatComponent.razor index f504c39b..6dc2aad5 100644 --- a/src/web/Client/Features/Chat/LargeChatComponent.razor +++ b/src/web/Client/Features/Chat/LargeChatComponent.razor @@ -24,7 +24,16 @@ - @chat.GetDisplayName(_currentUser.Id) + + @if (chat.HasUnreadMessages) + { + @chat.GetDisplayName(_currentUser.Id) (@chat.UnreadMessageCount} + } + else + { + @chat.GetDisplayName(_currentUser.Id) + } + @@ -63,9 +72,9 @@ >, UnauthorizedHttpResult>> ( LastMessageSentUtc = chat.LastMessageSentUtc, StartedUtc = chat.StartedUtc, Recipients = chat.Recipients.Select(recipient => recipient.ToUserSlim()).ToList(), - IsUnread = context.UnreadMessages.Any(unreadMessage => unreadMessage.ChatId == chat.Id && - unreadMessage.RecipientId == userId) + UnreadMessageCount = context.UnreadMessages + .Count(unreadMessage => + unreadMessage.ChatId == chat.Id && + unreadMessage.RecipientId == userId) }) .AsSingleQuery() .ToListAsync(cancellationToken); diff --git a/src/web/Shared/Chat/ChatDto.cs b/src/web/Shared/Chat/ChatDto.cs index 7838db47..3904cca4 100644 --- a/src/web/Shared/Chat/ChatDto.cs +++ b/src/web/Shared/Chat/ChatDto.cs @@ -12,11 +12,13 @@ public class ChatDto /// public string? DisplayName { get; init; } - public bool IsUnread { get; init; } = false; + public int UnreadMessageCount { get; init; } public List Messages { get; set; } = new(); public List Recipients { get; init; } = new(); public DateTime LastMessageSentUtc { get; init; } public DateTime StartedUtc { get; init; } = DateTime.UtcNow; + + public bool HasUnreadMessages => UnreadMessageCount > 0; }