indexiatech / re-notif

Redux & React based Notifications center.
http://indexiatech.github.io/re-notif
BSD 3-Clause "New" or "Revised" License
148 stars 45 forks source link

Feature discussion: reducer plugin #21

Closed jcheroske closed 8 years ago

jcheroske commented 8 years ago

I'm just getting started with this library, so please feel free to tell me if what I'm suggesting is an anti-pattern. Currently, in order to send a notification, a new action must be fired. This means that code must 1) know about this library, and 2) decide whether or not to send a notification. This means that notification-related code is scattered all over the place. One of the great things about redux is that, because all actions are given to all reducers, different reducers can respond to the same action. Redux-form leverages this beautifully through a reducer plugin. I believe this library could benefit from a similar architecture. Instead of various bits of code firing their success/failure actions and the notification actions, application code just fires its own actions. Then the reducer plugin would determine if a given action was notification-worthy. This would centralize notification logic, as the plugin might contain essentially a list of the actions, along with their payload -> notification mappings. So, maybe something like:

import { reducer as notifReducer } from 're-notif';

// plugin returns a notifSend config object to send a notification, or undefined.
// Maybe being able to return just a message string would be a nice convenience? 
const enhancedReducer = notifReducer.plugin(action => {
  switch (action.type) {
    case LOGIN_SUCCESS:
      return {
        message: `Welcome to ACME Widgets, ${action.payload.username}`
      }
  }
  return undefined;
});

const rootReducer = combineReducers({
  notifs: enhancedReducer,
  // ... more reducers here ...
});

What do people think?

Redux-form plugin docs: http://redux-form.com/6.0.0-alpha.15/docs/api/ReducerPlugin.md/

jcheroske commented 8 years ago

I'm closing this because I think I can accomplish everything I want (and more) by using a saga instead.