-
-
Save aubinlrx/5d9bbbbace9a38d77794 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Dispatcher = require('the-dispatcher'), | |
Immutable = require('immutable'), | |
NotificationConstants = require('../constants/notification_constants'), | |
UserConstants = require('../constants/user_constants'), | |
UploadConstants = require('../constants/upload_constants'), | |
StylesheetConstants = require('../constants/stylesheet_constants'), | |
makeStore = require('makestore'); | |
/** | |
* The Notification Store does nothing but store recent actions | |
* in order to power notifications. | |
*/ | |
var _notifications = Immutable.Set([]); | |
var _errors = Immutable.List(); | |
var _successes = Immutable.List(); | |
var NotificationStore = makeStore({ | |
notifications: () => _notifications, | |
errors: () => _errors, | |
successes: () => _successes, | |
dispatcherIndex: Dispatcher.register(action => { | |
switch (action.actionType) { | |
case NotificationConstants.NOTIFICATION_DISMISS: | |
_notifications = Immutable.Set([]); | |
break; | |
case NotificationConstants.NOTIFICATION_ERROR_DISMISS: | |
_errors = _errors.shift(); | |
break; | |
case NotificationConstants.NOTIFICATION_ERROR: | |
_errors = _errors.push(action.error); | |
break; | |
case NotificationConstants.NOTIFICATION_PAYMENT_ERROR: | |
_errors = _errors.push(action.error); | |
// ASK USER TO PAY | |
break; | |
case NotificationConstants.NOTIFICATION_UPGRADE_ERROR: | |
_errors = _errors.push(action.error); | |
// ASK USER TO UPGRADE | |
break; | |
case NotificationConstants.NOTIFICATION_SUCCESS_DISMISS: | |
_successes = _successes.shift(); | |
break; | |
case StylesheetConstants.STYLESHEET_MIGRATE_SUCCESS: | |
_successes = _successes.push('Stylesheet successfully migrated to latest version.'); | |
break; | |
case UserConstants.USER_UPDATED: | |
_successes = _successes.push('Profile successfully updated.'); | |
break; | |
case UploadConstants.UPLOAD_SET: | |
_notifications = _notifications.union(Immutable.Set( | |
action.value | |
.filter(upload => !upload.complete) | |
.map(upload => `Uploading ${upload.dataset}`))); | |
break; | |
default: | |
// avoid emitting changes for other events. | |
return true; | |
} | |
NotificationStore.emitChange(action.actionType); | |
return true; | |
}) | |
}); | |
NotificationStore.setMaxListeners(1000); | |
module.exports = NotificationStore; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment