melting-pot icon indicating copy to clipboard operation
melting-pot copied to clipboard

[Feature] Proposal Redux DUCKS action creaters & action dispatchers helpers

Open sakhisheikh opened this issue 6 years ago • 3 comments

This could help in reducing boilerplate using DUCKS while writing actions and reducers. Use would need to include respective creators utils for constants, actions and reducers. Hence it will help in writing much cleaner code and fast development.

sakhisheikh avatar Feb 04 '19 14:02 sakhisheikh

I really like the idea about having action creators and other redux utils in Melting-Pot, what API do you propose we should have for this?

adeelibr avatar Feb 04 '19 17:02 adeelibr

I propose the following

actionCreate = (prefix = '', action) => {
   // e.g, prefix = 'user', action = 'profile'
   const action = `${prefix}/action`; 
   const types = {
       INIT: 'init/',
       LOADING: 'loading/',
       SUCCESS: 'success/',
       ERROR: 'error/',  
   };
   return Object.values(types).map(type => `${type}/action`.toUpperCase());
}
actionDispatcher = fetch => async dispatch {
    const [init, loading, success, error] = actionCreate('user', 'get_profile');
    try {
       dispatch({ type: init }) // init is => INIT/USER/GET_PROFILE
       dispatch({ type: loading }); // loading is=> LOADING/USER/GET_PROFILE
       const payload = await fetch();
       dispatch({ type: success, payload })  // success=> SUCCESS/USER/GET_PROFILE
       return payload;
    } catch (error) {
       dispatch({ type: error}) // error is => ERROR/USER/GET_PROFILE
       throw error;
    }
}

Then use it something like this;

const myFooApiCall = async () => {
     try {
         const result = await fetch('http://some-api-call', {
              method: '',
              headers: {},
          });
          return result;
       } catch (error) {
          throw error;
       }
}

// use it like this
dispatch(actionDispatcher(myFooApiCall))

What do you think? @sakhisheikh

So we end up with 2 utils,

  • actionCreate
  • actionDispatcher

adeelibr avatar Feb 04 '19 18:02 adeelibr

@sakhisheikh are you going to be working on this issue?

adeelibr avatar Feb 07 '19 16:02 adeelibr