pl-fe: fix duplicated notifications
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
@ -2,7 +2,9 @@ import { getNotificationStatus } from 'pl-fe/features/notifications/components/n
|
||||
|
||||
import { normalizeAccount } from './account';
|
||||
|
||||
import type { OrderedMap as ImmutableOrderedMap } from 'immutable';
|
||||
import type { Notification as BaseNotification } from 'pl-api';
|
||||
import type { MinifiedNotification } from 'pl-fe/reducers/notifications';
|
||||
|
||||
const STATUS_NOTIFICATION_TYPES = [
|
||||
'favourite',
|
||||
@ -15,16 +17,22 @@ const STATUS_NOTIFICATION_TYPES = [
|
||||
|
||||
const normalizeNotification = (notification: BaseNotification) => ({
|
||||
...notification,
|
||||
duplicate: false,
|
||||
account: normalizeAccount(notification.account),
|
||||
account_id: notification.account.id,
|
||||
accounts: [normalizeAccount(notification.account)],
|
||||
account_ids: [notification.account.id],
|
||||
});
|
||||
|
||||
const normalizeNotifications = (notifications: Array<BaseNotification>) => {
|
||||
const normalizeNotifications = (notifications: Array<BaseNotification>, stateNotifications?: ImmutableOrderedMap<string, MinifiedNotification>) => {
|
||||
const deduplicatedNotifications: Notification[] = [];
|
||||
|
||||
for (const notification of notifications) {
|
||||
const existingNotification = stateNotifications?.get(notification.id);
|
||||
|
||||
// Do not update grouped notifications
|
||||
if (existingNotification && (existingNotification.duplicate || existingNotification.account_ids.length)) continue;
|
||||
|
||||
if (STATUS_NOTIFICATION_TYPES.includes(notification.type)) {
|
||||
const existingNotification = deduplicatedNotifications
|
||||
.find(deduplicated =>
|
||||
@ -36,7 +44,7 @@ const normalizeNotifications = (notifications: Array<BaseNotification>) => {
|
||||
if (existingNotification) {
|
||||
existingNotification.accounts.push(normalizeAccount(notification.account));
|
||||
existingNotification.account_ids.push(notification.account.id);
|
||||
existingNotification.id += '+' + notification.id;
|
||||
deduplicatedNotifications.push({ ...normalizeNotification(notification), duplicate: true });
|
||||
} else {
|
||||
deduplicatedNotifications.push(normalizeNotification(notification));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user