Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created May 19, 2015 19:56
Show Gist options
  • Select an option

  • Save tmcw/a64c72a979d9547647d8 to your computer and use it in GitHub Desktop.

Select an option

Save tmcw/a64c72a979d9547647d8 to your computer and use it in GitHub Desktop.

Revisions

  1. tmcw created this gist May 19, 2015.
    66 changes: 66 additions & 0 deletions notification_store.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    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;