Choices/assets/scripts/src/reducers/items.js
Jay Kid 0b3cf1a355 feat: implement custom properties for items and choices
+ Added unit tests for both choice and item reducers
+ Had to add some visually unpleasant undefineds, sorry :P
2017-06-28 11:11:02 +02:00

50 lines
1 KiB
JavaScript

const items = (state = [], action) => {
switch (action.type) {
case 'ADD_ITEM': {
// Add object to items array
const newState = [...state, {
id: action.id,
choiceId: action.choiceId,
groupId: action.groupId,
value: action.value,
label: action.label,
active: true,
highlighted: false,
customProperties: action.customProperties
}];
return newState.map((item) => {
if (item.highlighted) {
item.highlighted = false;
}
return item;
});
}
case 'REMOVE_ITEM': {
// Set item to inactive
return state.map((item) => {
if (item.id === action.id) {
item.active = false;
}
return item;
});
}
case 'HIGHLIGHT_ITEM': {
return state.map((item) => {
if (item.id === action.id) {
item.highlighted = action.highlighted;
}
return item;
});
}
default: {
return state;
}
}
};
export default items;