Skip to content

Outline of rules system #1

@recidive

Description

@recidive

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}))
  }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions