2017-10-08 16:52:54 +02:00
|
|
|
export const defaultState = [];
|
|
|
|
|
|
|
|
export default function choices(state = defaultState, 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
|
|
|
|
*/
|
2018-05-28 14:55:44 +02:00
|
|
|
return [
|
|
|
|
...state,
|
|
|
|
{
|
|
|
|
id: action.id,
|
|
|
|
elementId: action.elementId,
|
|
|
|
groupId: action.groupId,
|
|
|
|
value: action.value,
|
|
|
|
label: action.label || action.value,
|
|
|
|
disabled: action.disabled || false,
|
|
|
|
selected: false,
|
|
|
|
active: true,
|
|
|
|
score: 9999,
|
|
|
|
customProperties: action.customProperties,
|
|
|
|
placeholder: action.placeholder || false,
|
|
|
|
keyCode: null,
|
|
|
|
},
|
|
|
|
];
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-04-12 21:16:36 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
case 'ADD_ITEM': {
|
|
|
|
// If all choices need to be activated
|
|
|
|
if (action.activateOptions) {
|
2018-05-28 14:55:44 +02:00
|
|
|
return state.map(obj => {
|
2017-08-15 10:29:42 +02:00
|
|
|
const choice = obj;
|
2016-09-05 23:04:15 +02:00
|
|
|
choice.active = action.active;
|
|
|
|
return choice;
|
|
|
|
});
|
|
|
|
}
|
2017-10-09 18:46:47 +02:00
|
|
|
|
2016-09-05 23:04:15 +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
|
|
|
|
if (action.choiceId > -1) {
|
2018-05-28 14:55:44 +02:00
|
|
|
return state.map(obj => {
|
2017-08-15 10:29:42 +02:00
|
|
|
const choice = obj;
|
2016-09-05 23:04:15 +02:00
|
|
|
if (choice.id === parseInt(action.choiceId, 10)) {
|
|
|
|
choice.selected = true;
|
|
|
|
}
|
|
|
|
return choice;
|
|
|
|
});
|
|
|
|
}
|
2016-04-12 21:16:36 +02:00
|
|
|
|
2017-10-09 18:46:47 +02:00
|
|
|
return state;
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
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) {
|
2018-05-28 14:55:44 +02:00
|
|
|
return state.map(obj => {
|
2017-08-15 10:29:42 +02:00
|
|
|
const choice = obj;
|
2016-09-05 23:04:15 +02:00
|
|
|
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': {
|
2018-05-28 14:55:44 +02:00
|
|
|
return state.map(obj => {
|
2017-08-15 10:29:42 +02:00
|
|
|
const choice = obj;
|
2016-09-05 23:04:15 +02:00
|
|
|
// Set active state based on whether choice is
|
|
|
|
// within filtered results
|
2017-10-09 18:46:47 +02:00
|
|
|
choice.active = action.results.some(({ item, score }) => {
|
|
|
|
if (item.id === choice.id) {
|
|
|
|
choice.score = score;
|
2016-09-05 23:04:15 +02:00
|
|
|
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
|
|
|
case 'ACTIVATE_CHOICES': {
|
2018-05-28 14:55:44 +02:00
|
|
|
return state.map(obj => {
|
2017-08-15 10:29:42 +02:00
|
|
|
const choice = obj;
|
2016-09-05 23:04:15 +02:00
|
|
|
choice.active = action.active;
|
|
|
|
return choice;
|
|
|
|
});
|
|
|
|
}
|
2016-04-14 15:43:36 +02:00
|
|
|
|
2016-09-26 18:11:32 +02:00
|
|
|
case 'CLEAR_CHOICES': {
|
2017-10-09 18:46:47 +02:00
|
|
|
return defaultState;
|
2016-09-26 18:11:32 +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
|
|
|
}
|
2017-10-08 16:52:54 +02:00
|
|
|
}
|