Refactor and change logic to only update marker if we reached the last

read notification.
master
Thibaut Girka 4 years ago committed by ThibG
parent 66e9a77e36
commit 145ed37978
  1. 32
      app/javascript/flavours/glitch/reducers/notifications.js

@ -55,16 +55,16 @@ const notificationToMap = (state, notification) => ImmutableMap({
}); });
const normalizeNotification = (state, notification, usePendingItems) => { const normalizeNotification = (state, notification, usePendingItems) => {
const top = !shouldCountUnreadNotifications(state); const top = state.get('top');
if (usePendingItems || !state.get('pendingItems').isEmpty()) { if (usePendingItems || !state.get('pendingItems').isEmpty()) {
return state.update('pendingItems', list => list.unshift(notificationToMap(state, notification))).update('unread', unread => unread + 1); return state.update('pendingItems', list => list.unshift(notificationToMap(state, notification))).update('unread', unread => unread + 1);
} }
if (top) { if (shouldCountUnreadNotifications(state)) {
state = state.set('lastReadId', notification.id);
} else {
state = state.update('unread', unread => unread + 1); state = state.update('unread', unread => unread + 1);
} else {
state = state.set('lastReadId', notification.id);
} }
return state.update('items', list => { return state.update('items', list => {
@ -77,7 +77,6 @@ const normalizeNotification = (state, notification, usePendingItems) => {
}; };
const expandNormalizedNotifications = (state, notifications, next, isLoadingRecent, usePendingItems) => { const expandNormalizedNotifications = (state, notifications, next, isLoadingRecent, usePendingItems) => {
const top = !(shouldCountUnreadNotifications(state));
const lastReadId = state.get('lastReadId'); const lastReadId = state.get('lastReadId');
let items = ImmutableList(); let items = ImmutableList();
@ -102,16 +101,17 @@ const expandNormalizedNotifications = (state, notifications, next, isLoadingRece
}); });
} }
if (top) { if (!next) {
if (!items.isEmpty()) { mutable.set('hasMore', false);
mutable.update('lastReadId', id => compareId(id, items.first().get('id')) > 0 ? id : items.first().get('id'));
} }
} else {
if (shouldCountUnreadNotifications(state)) {
mutable.update('unread', unread => unread + items.count(item => compareId(item.get('id'), lastReadId) > 0)); mutable.update('unread', unread => unread + items.count(item => compareId(item.get('id'), lastReadId) > 0));
} else {
const mostRecentId = items.first().get('id');
if (compareId(lastReadId, mostRecentId) < 0) {
mutable.set('lastReadId', mostRecentId);
} }
if (!next) {
mutable.set('hasMore', false);
} }
mutable.set('isLoading', false); mutable.set('isLoading', false);
@ -197,7 +197,13 @@ const updateVisibility = (state, visibility) => {
}; };
const shouldCountUnreadNotifications = (state) => { const shouldCountUnreadNotifications = (state) => {
return !(state.get('isTabVisible') && state.get('top') && state.get('mounted') > 0); const isTabVisible = state.get('isTabVisible');
const isOnTop = state.get('top');
const isMounted = state.get('mounted') > 0;
const lastReadId = state.get('lastReadId');
const lastItemReached = !state.get('hasMore') || lastReadId === '0' || (!state.get('items').isEmpty() && compareId(state.get('items').last().get('id'), lastReadId) <= 0);
return !(isTabVisible && isOnTop && isMounted && lastItemReached);
}; };
const recountUnread = (state, last_read_id) => { const recountUnread = (state, last_read_id) => {

Loading…
Cancel
Save