Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outline of rules system #1

Closed
recidive opened this issue Sep 15, 2016 · 2 comments
Closed

Outline of rules system #1

recidive opened this issue Sep 15, 2016 · 2 comments
Labels

Comments

@recidive
Copy link

recidive commented Sep 15, 2016

Based on pair programming 14/09/2016 with @sebas5384:

const displayLoggedInMessageRule = {
  type: DISPLAY_ADMIN_LOGGED_IN_MESSAGE,

  // All conditions for all rules are called, need some kind of memoization.
  condition: (state, action) => {
    const { every, some } = feedOperators(state, action)

    return every([
      // Action is a condition
      ({state, action}) => action.type === LOGIN_SUCCESS,
      some([
        ({state, action}) => state.user.roles.indexOf('admin') !== -1,
        ({state, action}) => state.user.roles.indexOf('superadmin') !== -1
      ])
    ])
  },

  middleware: store => next => action => {
    next({
      type: DISPLAY_MESSAGE,
      payload: 'You are logged in!'
    })
    return next(action)
  }

}
const displayLoggedInMessageRule = {
  type: DISPLAY_ADMIN_LOGGED_IN_MESSAGE,

  // A list of action types to match before any conditions are computed.
  actions: [
    LOGIN_SUCCESS,
    LOGIN_SUCCESS_2
  ],

  condition: (state, action) => {
    const { every, some } = feedOperators(state, action)

    return some([
      ({state, action}) => state.user.roles.indexOf('admin') !== -1,
      ({state, action}) => state.user.roles.indexOf('superadmin') !== -1
    ])
  },

  middleware: store => next => action => {
    next({
      type: DISPLAY_MESSAGE,
      payload: 'You are logged in!'
    })
    return next(action)
  }

}
{
  middleware: combineRules([displayLoggedInMessageRule])
}
const feedOperators = (state, action) => ({
  every(conditions) {
    // TODO: memoization can happen here.
    return conditions.every(condition => condition({state, action}))
  }
  some(conditions) {
    return conditions.some(condition => condition({state, action}))
  }
})
@sebas5384 sebas5384 added the meta label Sep 15, 2016
@sebas5384
Copy link

sebas5384 commented Oct 6, 2016

@sebas5384
Copy link

Yay! we have a POC https://github.com/choko-org/redux-rules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants