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

93 lines
2.9 KiB
JavaScript
Raw Normal View History

const choices = (state = [], action) => {
switch (action.type) {
2016-08-14 23:14:37 +02:00
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
*/
2016-04-14 15:43:36 +02:00
return [...state, {
id: action.id,
groupId: action.groupId,
2016-04-12 15:31:07 +02:00
value: action.value,
label: action.label,
2016-08-14 23:14:37 +02:00
disabled: action.disabled,
selected: false,
active: true,
2016-05-03 15:55:38 +02:00
score: 9999,
}];
2016-08-14 23:14:37 +02:00
}
2016-08-14 23:14:37 +02:00
case 'ADD_ITEM': {
2016-07-13 22:40:59 +02:00
let newState = state;
// If all choices need to be activated
2016-08-14 23:14:37 +02:00
if (action.activateOptions) {
2016-07-13 22:40:59 +02:00
newState = state.map((choice) => {
choice.active = action.active;
return choice;
});
2016-07-13 22:40:59 +02:00
}
2016-06-27 15:57:33 +02:00
// When an item is added and it has an associated choice,
// we want to disable it so it can't be chosen again
2016-08-14 23:14:37 +02:00
if (action.choiceId > -1) {
2016-07-13 22:40:59 +02:00
newState = state.map((choice) => {
2016-08-14 23:14:37 +02:00
if (choice.id === parseInt(action.choiceId, 10)) {
2016-06-27 15:57:33 +02:00
choice.selected = true;
}
2016-06-27 15:57:33 +02:00
return choice;
});
}
2016-07-13 22:40:59 +02:00
return newState;
2016-08-14 23:14:37 +02:00
}
2016-07-13 22:40:59 +02:00
2016-08-14 23:14:37 +02:00
case 'REMOVE_ITEM': {
2016-06-27 15:57:33 +02:00
// When an item is removed and it has an associated choice,
// we want to re-enable it so it can be chosen again
2016-08-14 23:14:37 +02:00
if (action.choiceId > -1) {
2016-06-27 15:57:33 +02:00
return state.map((choice) => {
2016-08-14 23:14:37 +02:00
if (choice.id === parseInt(action.choiceId, 10)) {
2016-06-27 15:57:33 +02:00
choice.selected = false;
}
2016-06-27 15:57:33 +02:00
return choice;
});
}
2016-08-14 23:14:37 +02:00
return state;
}
case 'FILTER_CHOICES': {
2016-05-03 15:55:38 +02:00
const filteredResults = action.results;
2016-08-14 23:14:37 +02:00
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-06-27 15:57:33 +02:00
choice.active = filteredResults.some((result) => {
2016-08-14 23:14:37 +02:00
if (result.item.id === choice.id) {
2016-06-27 15:57:33 +02:00
choice.score = result.score;
2016-05-03 15:55:38 +02:00
return true;
}
2016-08-14 23:14:37 +02:00
return false;
2016-04-14 15:43:36 +02:00
});
2016-06-27 15:57:33 +02:00
return choice;
});
2016-05-03 15:55:38 +02:00
return filteredState;
2016-08-14 23:14:37 +02:00
}
2016-08-14 23:14:37 +02:00
case 'ACTIVATE_CHOICES': {
2016-06-27 15:57:33 +02:00
return state.map((choice) => {
choice.active = action.active;
return choice;
2016-08-14 23:14:37 +02:00
});
}
2016-04-14 15:43:36 +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 choices;