melting-pot
melting-pot copied to clipboard
[Feature] Proposal Redux DUCKS action creaters & action dispatchers helpers
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.
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?
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
@sakhisheikh are you going to be working on this issue?