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

44 lines
1.1 KiB
JavaScript
Raw Normal View History

const choices = (state = [], action) => {
switch (action.type) {
case 'ADD_ITEM':
// Add object to items array
return [...state, {
id: parseInt(action.id),
2016-03-30 16:04:21 +02:00
value: action.value,
active: true,
selected: false
}];
2016-04-04 15:43:22 +02:00
case 'UNSELECT_ALL':
return state.map((item) => {
if(item.selected) {
item.selected = false;
}
return item;
});
case 'REMOVE_ITEM':
// Set item to inactive
return state.map((item) => {
if(item.id === parseInt(action.id)) {
item.active = false;
}
return item;
2016-03-30 16:04:21 +02:00
});
case 'SELECT_ITEM':
return state.map((item) => {
if(item.id === parseInt(action.id)) {
item.selected = action.value;
}
return item;
});
2016-03-30 16:04:21 +02:00
default:
return state;
}
}
export default choices