Suppress-able broadcasts
This commit is contained in:
@ -126,7 +126,21 @@ const localstorageMiddleware = (function(){
|
||||
if( !ui ) ui = {};
|
||||
Object.assign( ui, action.data );
|
||||
localStorage.setItem('ui', JSON.stringify(ui));
|
||||
break;
|
||||
break
|
||||
|
||||
case 'SUPPRESS_BROADCAST':
|
||||
var ui = JSON.parse(localStorage.getItem('ui'))
|
||||
if (!ui) ui = {}
|
||||
|
||||
var suppressed_broadcasts = (typeof(ui.suppressed_broadcasts) !== 'undefined' ? ui.suppressed_broadcasts : [])
|
||||
suppressed_broadcasts.push(action.key)
|
||||
|
||||
Object.assign(
|
||||
ui,
|
||||
{ suppressed_broadcasts: suppressed_broadcasts }
|
||||
);
|
||||
localStorage.setItem('ui', JSON.stringify(ui));
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -244,13 +244,18 @@ export function createBrowserNotification( data ){
|
||||
}
|
||||
}
|
||||
|
||||
export function createNotification(content, type = 'default', key = helpers.generateGuid()){
|
||||
export function createNotification(content, type = 'default', key = null, title = null, sticky = false){
|
||||
if (!key){
|
||||
key = helpers.generateGuid()
|
||||
}
|
||||
return {
|
||||
type: 'CREATE_NOTIFICATION',
|
||||
notification: {
|
||||
key: key,
|
||||
type: type,
|
||||
content: content
|
||||
title: title,
|
||||
content: content,
|
||||
sticky: sticky
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -302,12 +302,59 @@ const UIMiddleware = (function(){
|
||||
case 'CREATE_NOTIFICATION':
|
||||
|
||||
// start a timeout to remove this notification
|
||||
var timeout = setTimeout(
|
||||
function(){
|
||||
store.dispatch(uiActions.removeNotification(action.notification.key))
|
||||
},
|
||||
(action.notification.type == 'shortcut' ? 1000 : 3000)
|
||||
)
|
||||
if (!action.notification.sticky){
|
||||
var timeout = setTimeout(
|
||||
function(){
|
||||
store.dispatch(uiActions.removeNotification(action.notification.key))
|
||||
},
|
||||
(action.notification.type == 'shortcut' ? 1000 : 3000)
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
// If a broadcast, add to suppressed_broadcasts
|
||||
if (notifications[index].type == 'broadcast'){
|
||||
store.dispatch({
|
||||
type: 'SUPPRESS_BROADCAST',
|
||||
key: notifications[index].key
|
||||
})
|
||||
}
|
||||
|
||||
next(action)
|
||||
break
|
||||
|
||||
case 'BROADCASTS_LOADED':
|
||||
var suppressed_broadcasts = []
|
||||
if (typeof(store.getState().ui.suppressed_broadcasts) !== 'undefined'){
|
||||
suppressed_broadcasts = store.getState().ui.suppressed_broadcasts
|
||||
}
|
||||
|
||||
for (var i = 0; i < action.broadcasts.length; i++){
|
||||
var broadcast = action.broadcasts[i]
|
||||
|
||||
if (!suppressed_broadcasts.includes(broadcast.id)){
|
||||
store.dispatch(uiActions.createNotification(
|
||||
broadcast.message,
|
||||
'broadcast',
|
||||
broadcast.id,
|
||||
broadcast.title,
|
||||
true
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
next(action)
|
||||
break
|
||||
|
||||
@ -4,9 +4,6 @@ import * as helpers from '../../helpers'
|
||||
export default function reducer(ui = {}, action){
|
||||
switch (action.type) {
|
||||
|
||||
case 'BROADCASTS_LOADED':
|
||||
return Object.assign({}, ui, {broadcasts: action.broadcasts});
|
||||
|
||||
case 'LAZY_LOADING':
|
||||
return Object.assign({}, ui, { lazy_loading: action.start });
|
||||
|
||||
@ -719,14 +716,18 @@ export default function reducer(ui = {}, action){
|
||||
|
||||
case 'REMOVE_NOTIFICATION':
|
||||
var notifications = Object.assign([], ui.notifications)
|
||||
|
||||
function getByKey( notification ){
|
||||
return notification.key === action.key
|
||||
|
||||
if( action.index > -1 ){
|
||||
notifications.splice(index, 1)
|
||||
}
|
||||
var index = notifications.findIndex(getByKey)
|
||||
if( index > -1 ) notifications.splice(index, 1)
|
||||
|
||||
return Object.assign({}, ui, { notifications: notifications })
|
||||
return Object.assign({}, ui, {notifications: notifications})
|
||||
|
||||
case 'SUPPRESS_BROADCAST':
|
||||
var suppressed_broadcasts = (typeof(ui.suppressed_broadcasts) !== 'undefined' ? ui.suppressed_broadcasts : [])
|
||||
suppressed_broadcasts.push(action.key)
|
||||
return Object.assign({}, ui, {suppressed_broadcasts: suppressed_broadcasts})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user