2016-06-29 15:47:58 +02:00
|
|
|
import { sortByAlpha } from './../lib/utils.js';
|
|
|
|
|
2016-06-27 15:46:12 +02:00
|
|
|
const choices = (state = [], action) => {
|
2016-04-12 15:10:07 +02:00
|
|
|
switch (action.type) {
|
2016-06-27 15:46:12 +02:00
|
|
|
case 'ADD_CHOICE':
|
2016-04-14 15:43:36 +02:00
|
|
|
return [...state, {
|
2016-04-17 13:02:28 +02:00
|
|
|
id: action.id,
|
2016-04-16 18:06:27 +02:00
|
|
|
groupId: action.groupId,
|
2016-04-12 15:31:07 +02:00
|
|
|
value: action.value,
|
2016-04-16 18:06:27 +02:00
|
|
|
label: action.label,
|
2016-06-30 14:57:56 +02:00
|
|
|
disabled: action.disabled, // A disabled choice appears in the choice dropdown but cannot be selected
|
|
|
|
selected: false, // A selected choice has been added to the passed input's value (added as an item)
|
|
|
|
active: true, // An active choice appears within the choice dropdown
|
2016-05-03 15:55:38 +02:00
|
|
|
score: 9999,
|
2016-06-29 15:47:58 +02:00
|
|
|
}].sort(sortByAlpha);
|
2016-04-12 21:16:36 +02:00
|
|
|
|
2016-04-28 16:30:43 +02:00
|
|
|
case 'ADD_ITEM':
|
2016-06-27 15:57:33 +02:00
|
|
|
// When an item is added and it has an associated choice,
|
2016-04-28 16:30:43 +02:00
|
|
|
// we want to disable it so it can't be chosen again
|
2016-06-27 15:46:12 +02:00
|
|
|
if(action.choiceId > -1) {
|
2016-06-27 15:57:33 +02:00
|
|
|
return state.map((choice) => {
|
|
|
|
if(choice.id === parseInt(action.choiceId)) {
|
|
|
|
choice.selected = true;
|
2016-04-28 16:30:43 +02:00
|
|
|
}
|
2016-06-27 15:57:33 +02:00
|
|
|
return choice;
|
2016-04-28 16:30:43 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
2016-04-12 21:16:36 +02:00
|
|
|
|
2016-04-25 15:59:58 +02:00
|
|
|
case 'REMOVE_ITEM':
|
2016-06-27 15:57:33 +02:00
|
|
|
// When an item is removed and it has an associated choice,
|
2016-04-25 15:59:58 +02:00
|
|
|
// we want to re-enable it so it can be chosen again
|
2016-06-27 15:46:12 +02:00
|
|
|
if(action.choiceId > -1) {
|
2016-06-27 15:57:33 +02:00
|
|
|
return state.map((choice) => {
|
|
|
|
if(choice.id === parseInt(action.choiceId)) {
|
|
|
|
choice.selected = false;
|
2016-04-25 15:59:58 +02:00
|
|
|
}
|
2016-06-27 15:57:33 +02:00
|
|
|
return choice;
|
2016-04-25 15:59:58 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2016-06-27 15:46:12 +02:00
|
|
|
case 'FILTER_CHOICES':
|
2016-05-03 15:55:38 +02:00
|
|
|
const filteredResults = action.results;
|
2016-06-27 15:57:33 +02:00
|
|
|
const filteredState = state.map((choice, index) => {
|
|
|
|
// Set active state based on whether choice is
|
2016-04-22 20:45:50 +02:00
|
|
|
// within filtered results
|
2016-05-02 23:21:34 +02:00
|
|
|
|
2016-06-27 15:57:33 +02:00
|
|
|
choice.active = filteredResults.some((result) => {
|
|
|
|
if(result.item.id === choice.id) {
|
|
|
|
choice.score = result.score;
|
2016-05-03 15:55:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-14 15:43:36 +02:00
|
|
|
});
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-06-27 15:57:33 +02:00
|
|
|
return choice;
|
2016-05-03 15:55:38 +02:00
|
|
|
}).sort((prev, next) => {
|
|
|
|
return prev.score - next.score;
|
2016-04-22 20:45:50 +02:00
|
|
|
});
|
|
|
|
|
2016-05-03 15:55:38 +02:00
|
|
|
return filteredState;
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-06-27 15:46:12 +02:00
|
|
|
case 'ACTIVATE_CHOICES':
|
2016-06-27 15:57:33 +02:00
|
|
|
return state.map((choice) => {
|
|
|
|
choice.active = action.active;
|
|
|
|
return choice;
|
2016-06-29 15:47:58 +02:00
|
|
|
}).sort(sortByAlpha);
|
2016-04-14 15:43:36 +02:00
|
|
|
|
|
|
|
|
2016-04-12 15:10:07 +02:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 15:46:12 +02:00
|
|
|
export default choices;
|