unirakun / k-redux-factory

Factory of Redux reducers and their associated actions and selectors.
MIT License
18 stars 4 forks source link

API to add custom actions to state nodes #91

Open fabienjuif opened 6 years ago

fabienjuif commented 6 years ago

From @EmrysMyrddin (https://github.com/alakarteio/k-ramel/issues/60)


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, (...) => ...) ?

fabienjuif commented 6 years ago

From @EmrysMyrddin (https://github.com/alakarteio/k-ramel/issues/60)


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.

guillaumecrespel commented 6 years ago

Why define actions ? What do you think about :

const store = createStore({
  data: {
    nested: simpleObject(),
  },
}

store.data.nested.dispatch('load')
// Dispatch { type: 'data/nested/load' }

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

// OR - if payload it's not a rule on krf

store.data.nested.dispatch('loaded')({ hello: 'world' })
// Dispatch { type: 'data/nested/loaded', hello: 'world' }
fabienjuif commented 6 years ago

He want to define actions so he can uses its like constants. And use static access to them (completion, etc).

Out of topic for @guillaumecrespel, how can you make them both works:


store.data.nested.dispatch('load')
// Dispatch { type: 'data/nested/load' }

store.data.nested.dispatch('loaded')({ hello: 'world' })
// Dispatch { type: 'data/nested/loaded', payload: { hello: 'world' } }
fabienjuif commented 6 years ago

@EmrysMyrddin is it something you still need after using the library more often ?

EmrysMyrddin commented 6 years ago

Yes, I did had the time toake a PR but I have began to make some utility functions on ICES project that implements my first API proposition.

For now, it works pretty well, the only drawback is that our actions are currently defined in a separate file from my model. So yes it could be great to integrate it directly in the model declaration.

fabienjuif commented 6 years ago

Do you want to open a PR or may I think of an implementation myself ?

EmrysMyrddin commented 6 years ago

I think I can make the PR. While using it, I had some ideas to improve our first draft of the API.

Le lun. 11 juin 2018 à 08:42, Fabien JUIF notifications@github.com a écrit :

Do you want to open a PR or may I think of an implementation myself ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/alakarteio/k-redux-factory/issues/91#issuecomment-396138217, or mute the thread https://github.com/notifications/unsubscribe-auth/ADrU8mowguqjglMmyQumeYCjDhaXPQgSks5t7hFugaJpZM4SBU5w .

fabienjuif commented 6 years ago

Should we wait this issue for the 6.0.0 ?

The questions behind that are:

fabienjuif commented 6 years ago

ping @EmrysMyrddin

EmrysMyrddin commented 6 years ago

I don't currently have the time to do the PR. But I don't think i will break existing API. I will just add things.