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

47 lines
1.2 KiB
JavaScript
Raw Normal View History

const items = (state = [], action) => {
switch (action.type) {
2016-08-14 23:14:37 +02:00
case 'ADD_ITEM': {
// Add object to items array
2016-08-14 23:14:37 +02:00
const newState = [...state, {
2016-04-14 15:43:36 +02:00
id: action.id,
choiceId: action.choiceId,
value: action.value,
label: action.label,
active: true,
2016-08-14 23:14:37 +02:00
highlighted: false,
}];
return newState.map((item) => {
2016-08-14 23:14:37 +02:00
if (item.highlighted) {
item.highlighted = false;
}
return item;
});
2016-08-14 23:14:37 +02:00
}
2016-08-14 23:14:37 +02:00
case 'REMOVE_ITEM': {
// Set item to inactive
return state.map((item) => {
2016-08-14 23:14:37 +02:00
if (item.id === action.id) {
item.active = false;
}
return item;
});
2016-08-14 23:14:37 +02:00
}
2016-08-14 23:14:37 +02:00
case 'HIGHLIGHT_ITEM': {
return state.map((item) => {
2016-08-14 23:14:37 +02:00
if (item.id === action.id) {
item.highlighted = action.highlighted;
}
return item;
});
2016-08-14 23:14:37 +02:00
}
2016-08-14 23:14:37 +02:00
default: {
return state;
2016-08-14 23:14:37 +02:00
}
}
2016-08-14 23:14:37 +02:00
};
export default items;