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
2017-08-28 21:40:44 +02:00

82 lines
2.2 KiB
JavaScript

import {
createInc,
createClear,
createSet,
createPop,
createPush,
createUp,
createInit,
createMove,
} 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 }));
},
moveItem(key, index, toIndex) {
dispatch(createMove({ id: getId(id, ownProps), key, index, toIndex }));
},
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
);
}