2016-06-27 15:46:12 +02:00
|
|
|
const choices = (state = [], action) => {
|
2016-09-05 23:04:15 +02:00
|
|
|
switch (action.type) {
|
|
|
|
case 'ADD_CHOICE': {
|
|
|
|
/*
|
|
|
|
A disabled choice appears in the choice dropdown but cannot be selected
|
|
|
|
A selected choice has been added to the passed input's value (added as an item)
|
|
|
|
An active choice appears within the choice dropdown
|
|
|
|
*/
|
|
|
|
return [...state, {
|
|
|
|
id: action.id,
|
|
|
|
groupId: action.groupId,
|
|
|
|
value: action.value,
|
|
|
|
label: action.label,
|
|
|
|
disabled: action.disabled,
|
|
|
|
selected: false,
|
|
|
|
active: true,
|
|
|
|
score: 9999,
|
|
|
|
}];
|
|
|
|
}
|
2016-04-12 21:16:36 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
case 'ADD_ITEM': {
|
|
|
|
let newState = state;
|
2016-07-13 22:40:59 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// If all choices need to be activated
|
|
|
|
if (action.activateOptions) {
|
|
|
|
newState = state.map((choice) => {
|
|
|
|
choice.active = action.active;
|
|
|
|
return choice;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// When an item is added and it has an associated choice,
|
|
|
|
// we want to disable it so it can't be chosen again
|
|
|
|
if (action.choiceId > -1) {
|
|
|
|
newState = state.map((choice) => {
|
|
|
|
if (choice.id === parseInt(action.choiceId, 10)) {
|
|
|
|
choice.selected = true;
|
|
|
|
}
|
|
|
|
return choice;
|
|
|
|
});
|
|
|
|
}
|
2016-04-12 21:16:36 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return newState;
|
|
|
|
}
|
2016-07-13 22:40:59 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
case 'REMOVE_ITEM': {
|
|
|
|
// When an item is removed and it has an associated choice,
|
|
|
|
// we want to re-enable it so it can be chosen again
|
|
|
|
if (action.choiceId > -1) {
|
|
|
|
return state.map((choice) => {
|
|
|
|
if (choice.id === parseInt(action.choiceId, 10)) {
|
|
|
|
choice.selected = false;
|
|
|
|
}
|
|
|
|
return choice;
|
|
|
|
});
|
|
|
|
}
|
2016-04-25 15:59:58 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return state;
|
|
|
|
}
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
case 'FILTER_CHOICES': {
|
|
|
|
const filteredResults = action.results;
|
|
|
|
const filteredState = state.map((choice) => {
|
|
|
|
// Set active state based on whether choice is
|
|
|
|
// within filtered results
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
choice.active = filteredResults.some((result) => {
|
|
|
|
if (result.item.id === choice.id) {
|
|
|
|
choice.score = result.score;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return choice;
|
|
|
|
});
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return filteredState;
|
|
|
|
}
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
case 'ACTIVATE_CHOICES': {
|
|
|
|
return state.map((choice) => {
|
|
|
|
choice.active = action.active;
|
|
|
|
return choice;
|
|
|
|
});
|
|
|
|
}
|
2016-04-14 15:43:36 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
default: {
|
|
|
|
return state;
|
2016-04-12 15:10:07 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-08-14 23:14:37 +02:00
|
|
|
};
|
2016-04-12 15:10:07 +02:00
|
|
|
|
2016-06-27 15:46:12 +02:00
|
|
|
export default choices;
|