Notifications as object for better targetting; Notifications closing vs removing for animation window

This commit is contained in:
James Barnsley
2018-02-28 08:44:38 +13:00
parent 7fa1fcf9e7
commit 2a95af89de
7 changed files with 62 additions and 54 deletions

View File

@ -188,13 +188,21 @@ export function createNotification(data){
title: null,
content: null,
description: null,
sticky: false
sticky: false,
closing: false
},
data
)
}
}
export function closeNotification(key){
return {
type: 'CLOSE_NOTIFICATION',
key: key
}
}
export function removeNotification(key){
return {
type: 'REMOVE_NOTIFICATION',

View File

@ -70,40 +70,41 @@ const UIMiddleware = (function(){
case 'CREATE_NOTIFICATION':
// start a timeout to remove this notification
// start a timeout to close this notification
if (!action.notification.sticky){
var timeout = setTimeout(
function(){
store.dispatch(uiActions.removeNotification(action.notification.key))
store.dispatch(uiActions.closeNotification(action.notification.key))
},
action.notification.duration * 1000
)
}
next(action)
break
next(action);
break;
case 'REMOVE_NOTIFICATION':
var notifications = Object.assign([], store.getState().ui.notifications)
function getByKey(notification){
return notification.key === action.key
}
var index = notifications.findIndex(getByKey)
// Save our index for the reducer to use. Saves us from re-finding by key
action.index = index
case 'CLOSE_NOTIFICATION':
var notifications = Object.assign({}, store.getState().ui.notifications);
// If a broadcast, add to suppressed_broadcasts
if (index > -1 && typeof(notifications[index]) !== 'undefined' && notifications[index].type == 'broadcast'){
if (notifications[action.key] && notifications[action.key].type == 'broadcast'){
store.dispatch({
type: 'SUPPRESS_BROADCAST',
key: notifications[index].key
key: action.key
})
}
next(action)
break
// start a timeout to remove this notification
// This gives us time to animate out the notification before we remove the data
var timeout = setTimeout(
function(){
store.dispatch(uiActions.removeNotification(action.key))
},
200
)
next(action);
break;
case 'BROADCASTS_LOADED':
var suppressed_broadcasts = []

View File

@ -98,18 +98,21 @@ export default function reducer(ui = {}, action){
**/
case 'CREATE_NOTIFICATION':
var notifications = [...ui.notifications, action.notification]
notifications = helpers.mergeDuplicates(notifications,'key')
return Object.assign({}, ui, { notifications: notifications })
var notifications = Object.assign({}, ui.notifications);
notifications[action.notification.key] = action.notification;
return Object.assign({}, ui, { notifications: notifications });
case 'CLOSE_NOTIFICATION':
var notifications = Object.assign({}, ui.notifications);
if (notifications[action.key]){
notifications[action.key].closing = true;
}
return Object.assign({}, ui, { notifications: notifications });
case 'REMOVE_NOTIFICATION':
var notifications = Object.assign([], ui.notifications)
if (action.index > -1){
notifications.splice(action.index, 1)
}
return Object.assign({}, ui, {notifications: notifications})
var notifications = Object.assign({}, ui.notifications);
delete notifications[action.key];
return Object.assign({}, ui, {notifications: notifications});