lbry-desktop/ui/redux/reducers/notifications.js
infinite-persistence 1e4c79cf06 Don't clear all notifications when only 1 is clicked.
## Issue:
Closes 5515: All videos marked as read when clicking a single notification from notification list

## Change:
- Augment `doReadNotifications` to only clear the given IDs. If the argument is `null` or is not a valid array (e.g. when used as a click handlers, the click event object is passed in), all notifications will be cleared.

- Augment `NOTIFICATION_READ_COMPLETED` to only clear the given IDs.

## Notes:
- Wasn't sure of the API will fail if the ID is invalid, so I start from `unreadNotifications` first, then only filtering it further with the given ID. Otherwise, we could just skip the `unreadNotifications` filtering.
2021-03-08 11:41:04 -05:00

129 lines
3.3 KiB
JavaScript

// @flow
import * as ACTIONS from 'constants/action_types';
import { handleActions } from 'util/redux-utils';
const defaultState: NotificationState = {
notifications: [],
fetchingNotifications: false,
toasts: [],
errors: [],
};
export default handleActions(
{
// Toasts
[ACTIONS.CREATE_TOAST]: (state: NotificationState, action: DoToast) => {
const toast: Toast = action.data;
const newToasts: Array<Toast> = state.toasts.slice();
newToasts.push(toast);
return {
...state,
toasts: newToasts,
};
},
[ACTIONS.DISMISS_TOAST]: (state: NotificationState) => {
const newToasts: Array<Toast> = state.toasts.slice();
newToasts.shift();
return {
...state,
toasts: newToasts,
};
},
// Notifications
[ACTIONS.NOTIFICATION_LIST_STARTED]: (state, action) => {
return {
...state,
fetchingNotifications: true,
};
},
[ACTIONS.NOTIFICATION_LIST_COMPLETED]: (state, action) => {
const { notifications } = action.data;
return {
...state,
notifications,
fetchingNotifications: false,
};
},
[ACTIONS.NOTIFICATION_LIST_FAILED]: (state, action) => {
return {
...state,
fetchingNotifications: false,
};
},
[ACTIONS.NOTIFICATION_READ_COMPLETED]: (state, action) => {
const { notifications } = state;
const { notificationIds } = action.data;
const newNotifications =
notifications &&
notifications.map((notification) => {
if (notificationIds.includes(notification.id)) {
return { ...notification, is_read: true };
} else {
return { ...notification };
}
});
return {
...state,
notifications: newNotifications,
};
},
[ACTIONS.NOTIFICATION_READ_FAILED]: (state, action) => {
return {
...state,
};
},
[ACTIONS.NOTIFICATION_SEEN_COMPLETED]: (state, action) => {
const { notifications } = state;
const { notificationIds } = action.data;
const newNotifications = notifications.map((notification) => {
if (notificationIds.includes(notification.id)) {
return { ...notification, is_seen: true };
}
return notification;
});
return {
...state,
notifications: newNotifications,
};
},
[ACTIONS.NOTIFICATION_DELETE_COMPLETED]: (state, action) => {
const { notifications } = state;
const { notificationId } = action.data;
const newNotifications = notifications.filter((notification) => {
return notification.id !== notificationId;
});
return {
...state,
notifications: newNotifications,
};
},
// Errors
[ACTIONS.CREATE_ERROR]: (state: NotificationState, action: DoError) => {
const error: ErrorNotification = action.data;
const newErrors: Array<ErrorNotification> = state.errors.slice();
newErrors.push(error);
return {
...state,
errors: newErrors,
};
},
[ACTIONS.DISMISS_ERROR]: (state: NotificationState) => {
const newErrors: Array<ErrorNotification> = state.errors.slice();
newErrors.shift();
return {
...state,
errors: newErrors,
};
},
},
defaultState
);