From 80456f4da8b8ac776509d0b4e06a37d437d4f675 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sun, 28 Apr 2024 23:54:00 +0200 Subject: [PATCH] Add sort by unread messages option Signed-off-by: Emmy D'Anello --- chat/static/chat.js | 29 +++++++++++++++++++++++++++-- chat/templates/chat/content.html | 4 ++++ locale/fr/LC_MESSAGES/django.po | 26 +++++++++++++++----------- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/chat/static/chat.js b/chat/static/chat.js index 0962f34..c4b8ab9 100644 --- a/chat/static/chat.js +++ b/chat/static/chat.js @@ -95,7 +95,7 @@ async function addChannel(channel, categoryLists) { categoryList.parentElement.classList.remove('d-none') let navItem = document.createElement('li') - navItem.classList.add('list-group-item') + navItem.classList.add('list-group-item', 'tab-channel') navItem.id = `tab-channel-${channel['id']}` navItem.setAttribute('data-bs-dismiss', 'offcanvas') navItem.onclick = () => selectChannel(channel['id']) @@ -115,6 +115,9 @@ async function addChannel(channel, categoryLists) { unreadBadge.classList.add('d-none') channelButton.appendChild(unreadBadge) + if (document.getElementById('sort-by-unread-switch').checked) + navItem.style.order = `${-channel.unread_messages}` + fetchMessages(channel['id']) } @@ -187,7 +190,8 @@ function markMessageAsRead(data) { } function updateUnreadBadges(unreadMessages) { - console.log(unreadMessages) + const sortByUnread = document.getElementById('sort-by-unread-switch').checked + for (let channel of Object.values(channels)) { let unreadMessagesChannel = unreadMessages[channel['id']] || 0 console.log(channel, unreadMessagesChannel) @@ -199,6 +203,9 @@ function updateUnreadBadges(unreadMessages) { unreadBadge.classList.remove('d-none') else unreadBadge.classList.add('d-none') + + if (sortByUnread) + document.getElementById(`tab-channel-${channel['id']}`).style.order = `${-unreadMessagesChannel}` } } @@ -392,6 +399,24 @@ function toggleFullscreen() { document.addEventListener('DOMContentLoaded', () => { document.addEventListener('click', removeAllPopovers) + document.getElementById('sort-by-unread-switch').addEventListener('change', event => { + const sortByUnread = event.target.checked + for (let channel of Object.values(channels)) { + let item = document.getElementById(`tab-channel-${channel['id']}`) + if (sortByUnread) + item.style.order = `${-channel.unread_messages}` + else + item.style.removeProperty('order') + } + + localStorage.setItem('chat.sort-by-unread', sortByUnread) + }) + + if (localStorage.getItem('chat.sort-by-unread') === 'true') { + document.getElementById('sort-by-unread-switch').checked = true + document.getElementById('sort-by-unread-switch').dispatchEvent(new Event('change')) + } + /** * Process the received data from the server. * @param data The received message diff --git a/chat/templates/chat/content.html b/chat/templates/chat/content.html index 2e1d8e1..0bf517f 100644 --- a/chat/templates/chat/content.html +++ b/chat/templates/chat/content.html @@ -9,6 +9,10 @@
+
+ + +