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

API to add custom actions to state nodes #60

Closed
EmrysMyrddin opened this issue Feb 6, 2018 · 3 comments
Closed

API to add custom actions to state nodes #60

EmrysMyrddin opened this issue Feb 6, 2018 · 3 comments

Comments

@EmrysMyrddin
Copy link
Collaborator

EmrysMyrddin commented Feb 6, 2018

It could be useful to allow users to define their own custom actions along with the ones already generated by k-redux-factory.

The actions could be then decorated with dispatch function, allowing the user to easily call it from the store

const store = createStore({
  data: simpleObject({ 
    actions: {
      load: () => ({ type: 'REQUEST_DATA' }),
      loaded: (data) => ({ type: 'DATA_LOADED', payload: { data } }),
    },
  }),
})

store.data.loaded()
store.data.load({ hello: 'world' })

Since we want to push the use of the redux-saga like API, we want to be able to dispatch very simple action like simple events. For this, our library can also help the user by generating actions for him. You just give a type name for your action and it generate the action creator for you. The action creators can take an optional payload that will be aded to the action.

const store = createStore({
  data: simpleObject({
    actions: {
      load: 'REQUEST_DATA',
      loaded: 'REQUEST_DATA',
      // Also allows to give a custom action creator implementation
      custom: () => ({ type: 'CUSTOM_ACTION' })
    },
  }),
},{
  listeners: [
    when(store.data.load.type, (action, store) => store.data.loaded({ hello: 'world' }))
    // Dispatch { type: 'DATA_LOADED', payload: { hello: 'world' } }
  ]
})

I'm not entirely sure of the API. In particular, I'm not convinced by the store.data.load.type to get the type name of the action. Perhaps by adding a toString implementation to the function ? Allowing to just do when(store.data.loaded, (...) => ...) ?

@EmrysMyrddin
Copy link
Collaborator Author

EmrysMyrddin commented Feb 6, 2018

An other idea for the generation of action creators is to give a list of action name and generate an action type based on the path in a mobx-state-tree fashion

const store = createStore({
  data: {
    nested: simpleObject({
      actions: [ 'load', 'loaded' ]
    }),
  },
}

store.data.nested.loaded({ hello: 'world' })
// Dispatch {type: 'data/nested/loaded', payload: { hello: 'world' } }

But I'm not entirely convince... It's difficult then to extend the list of actions to add a custom action creator function.

A solution can be to have both API. If actions is an object, we use the previously defined API, if it's an array, we use this convention.

@fabienjuif
Copy link
Member

This is something to open to k-redux-factory first since simpleObject comes from k-redux-factory I think.

k-redux-factory offers middlewares to add some sort of custom actions/mappers/reducers.
I know this not the same thing as what you suggest, but It could interest you.

https://github.com/alakarteio/k-redux-factory#helpers

@fabienjuif
Copy link
Member

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

No branches or pull requests

2 participants