Coder Social home page Coder Social logo

Comments (5)

adrienWeiss avatar adrienWeiss commented on May 13, 2024

I've been using a function that takes the name and the url to fetch (that would be the function to call for you, getAllMaterialien or getAllFormen) that returns the state tree

{
  [name]: {
    list: [],
    loadAll: effect(...) => {
      ...
      dispatch.helper[name].setList(...
  },
}

and then spreading those

const model = {
  ...materialState,
  ...formState,
},

But I agree with you, one would want to easily parse the local state, but it is also important to be able to dispatch elsewhere.
Also, with the project growing there is a risk of conflicting names.

from easy-peasy.

ctrlplusb avatar ctrlplusb commented on May 13, 2024

@RuedigerLudwig I have opened a PR which may help you with this. Utilising the meta data exposed to the effect function you could do something like:

const list = (endpoint) => ({  
  list: [],
  ids: select(state => R.pluck('id', state.list)),
  loaded: select(state => state.list === []),
  setList: (state, payload) => ({ ...state, list: payload }),
  loadAll: effect(async (dispatch, _payload, _getState, _injections, meta) => {
    const data = await endpoint()
    const setList = R.path(meta.parent.join('setList'), dispatch);
    setList(data);
  }),
})

And then use it like so:

const store = {
  helper: {
    material: {
	  other: '👋',
      ...list(getAllMaterialien)
    },
    form: {
      ...list(getAllFormen)
    },
  },
}

Let me know what you think. 😊

@adrienWeiss - would appreciate your input too.

from easy-peasy.

ctrlplusb avatar ctrlplusb commented on May 13, 2024

The only thing I dislike about this helper approach is that it can be harder to visualise your state tree, however, when the typescript work lands you should get typesafe autocomplete on your model which will help dramatically. :)

from easy-peasy.

ctrlplusb avatar ctrlplusb commented on May 13, 2024

Available in latest release.

https://github.com/ctrlplusb/easy-peasy/releases/tag/v1.7.0

🤘

from easy-peasy.

RuedigerLudwig avatar RuedigerLudwig commented on May 13, 2024

Wow, that is quick. Thank you for the quick addition. I am pretty sure that it solves my use case.

from easy-peasy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.