Skip to content

Instantly share code, notes, and snippets.

@jacobp100
Created July 16, 2016 20:36
Show Gist options
  • Save jacobp100/8e5b8d3b66f1147146d49966c7e1e5de to your computer and use it in GitHub Desktop.
Save jacobp100/8e5b8d3b66f1147146d49966c7e1e5de to your computer and use it in GitHub Desktop.
import serialize from 'form-serialize';
import actionCreators from './actionCreators';
export const formDispatcher = dispatch => event => {
event.preventDefault();
const { currentTarget } = event;
const isChild = 'form' in currentTarget;
const form = isChild ? currentTarget.form : currentTarget;
const actionParams = serialize(form, { hash: true, empty: true });
if (isChild && currentTarget.tagName === 'BUTTON' && currentTarget.name) {
actionParams[currentTarget.name] = currentTarget.value;
}
const actionCreator = actionCreators[actionParams['action-creator']];
const action = actionCreator(actionParams);
dispatch(action);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment