1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/frontend/src/component/input-helpers.js
2016-12-13 19:56:52 +01:00

69 lines
1.8 KiB
JavaScript

import {
createInc,
createClear,
createSet,
createPop,
createPush,
createUp,
createInit,
} from '../store/input-actions';
function getId (id, ownProps) {
if (typeof id === 'function') {
return id(ownProps); // should return array...
}
return [id];
}
export function createMapper ({ id, getDefault, prepare = (v) => v }) {
return (state, ownProps) => {
let input;
let initCallRequired = false;
const scope = getId(id, ownProps);
if (state.input.hasIn(scope)) {
input = state.input.getIn(scope).toJS();
} else {
initCallRequired = true;
input = getDefault ? getDefault(state, ownProps) : {};
}
return prepare({
initCallRequired,
input,
}, state, ownProps);
};
}
export function createActions ({ id, prepare = (v) => v }) {
return (dispatch, ownProps) => (prepare({
clear () {
dispatch(createClear({ id: getId(id, ownProps) }));
},
init (value) {
dispatch(createInit({ id: getId(id, ownProps), value }));
},
setValue (key, value) {
dispatch(createSet({ id: getId(id, ownProps), key, value }));
},
pushToList (key, value) {
dispatch(createPush({ id: getId(id, ownProps), key, value }));
},
removeFromList (key, index) {
dispatch(createPop({ id: getId(id, ownProps), key, index }));
},
updateInList (key, index, newValue, merge = false) {
dispatch(createUp({ id: getId(id, ownProps), key, index, newValue, merge }));
},
incValue (key) {
dispatch(createInc({ id: getId(id, ownProps), key }));
},
}, dispatch, ownProps));
}