0.0.0 • Published 7 years ago

redux-scoped-actions v0.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

redux-scoped-actions

build status

Namespacing utilities for redux-actions.

Why

People asked for this in redux-actions, but we didn't want to include it in core.

Install

$ npm i --save redux-scoped-actions

If you don’t use npm, you may grab the latest UMD build from unpkg (either a development or a production build). The UMD build exports a global called window.ReduxScopedActions if you add it to your page via a <script> tag. We don’t recommend UMD builds for any serious application, as most of the libraries complementary to Redux are only available on npm.

Usage

const actionCreators = namespaceActions( createActions({ 'APP/COUNTER/INCREMENT': amount => ({amount}), 'APP/COUNTER/DECREMENT': amount => ({amount: -amount}), 'APP/NOTIFY': (username, message) => ({message: ${username}: ${message}}), LOGIN: username => ({username}), }, 'LOGOUT') )

expect(actionCreators.app.counter.increment(1)).to.deep.equal({type: 'APP/COUNTER/INCREMENT', amount: 1}) expect(actionCreators.app.counter.decrement(1)).to.deep.equal({type: 'APP/COUNTER/DECREMENT', amount: -1}) expect(actionCreators.app.notify('yangmillstheory', 'Hello, World!')).to.deep.equal({ type: 'APP/NOTIFY', message: 'yangmillstheory: Hello, World!', }) expect(actionCreators.login('yangmillstheory')).to.deep.equal({ type: 'LOGIN', payload: {username: 'yangmillstheory'}, }) expect(actionCreators.logout()).to.deep.equal({type: 'LOGOUT'})

  </p>
</details>

<details>
  <summary>Destructured</summary>
  <p>
```js
import namespaceActions from 'redux-scoped-actions'

const {
  app: {
    counter: { increment, decrement },
    notify,
  },
  login, logout,
} = namespaceActions(
  createActions({
    'APP/COUNTER/INCREMENT': amount => ({amount}),
    'APP/COUNTER/DECREMENT': amount => ({amount: -amount}),
    'APP/NOTIFY': (username, message) => ({message: `${username}: ${message}`}),
    LOGIN: username => ({username}),
  }, 'LOGOUT')
)

expect(increment(1)).to.deep.equal({type: 'APP/COUNTER/INCREMENT', amount: 1})
expect(decrement(1)).to.deep.equal({type: 'APP/COUNTER/DECREMENT', amount: -1 })
expect(notify('yangmillstheory', 'Hello, World!')).to.deep.equal({
  type: 'APP/NOTIFY',
  message: 'yangmillstheory: Hello, World!',
})
expect(login('yangmillstheory')).to.deep.equal({
  type: 'LOGIN',
  payload: {username: 'yangmillstheory'},
})
expect(logout()).to.deep.equal({type: 'LOGOUT'})

License

MIT © 2016, Victor Alvarez