Choices/assets/scripts/src/reducers/options.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

const options = (state = [], action) => {
switch (action.type) {
2016-04-12 15:31:07 +02:00
case 'ADD_OPTION':
2016-04-14 15:43:36 +02:00
return [...state, {
id: action.id,
groupId: action.groupId,
2016-04-12 15:31:07 +02:00
value: action.value,
label: action.label,
2016-04-12 15:31:07 +02:00
disabled: false,
selected: false,
2016-04-14 15:43:36 +02:00
}];;
case 'SELECT_OPTION':
return state.map((option) => {
if(option.id === parseInt(action.id)) {
option.selected = action.selected;
}
return option;
});
2016-04-14 15:43:36 +02:00
case 'REMOVE_ITEM':
// When an item is removed and it has an associated option,
// we want to re-enable it so it can be chosen again
if(action.optionId > -1) {
return state.map((option) => {
if(option.id === parseInt(action.optionId)) {
option.selected = action.selected;
}
return option;
});
} else {
return state;
}
default:
return state;
}
}
export default options;