2019-12-23 19:22:54 +01:00
|
|
|
import {
|
|
|
|
AddItemAction,
|
|
|
|
RemoveItemAction,
|
|
|
|
HighlightItemAction,
|
|
|
|
} from '../actions/items';
|
2021-12-17 22:26:52 +01:00
|
|
|
import { Item } from '../interfaces/item';
|
|
|
|
import { State } from '../interfaces/state';
|
2019-12-23 19:22:54 +01:00
|
|
|
|
|
|
|
export const defaultState = [];
|
|
|
|
|
2021-12-17 22:26:52 +01:00
|
|
|
type ActionTypes =
|
|
|
|
| AddItemAction
|
|
|
|
| RemoveItemAction
|
|
|
|
| HighlightItemAction
|
|
|
|
| Record<string, never>;
|
2019-12-23 19:22:54 +01:00
|
|
|
|
|
|
|
export default function items(
|
|
|
|
state: Item[] = defaultState,
|
2021-12-17 22:26:52 +01:00
|
|
|
action: ActionTypes = {},
|
2019-12-23 19:22:54 +01:00
|
|
|
): State['items'] {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'ADD_ITEM': {
|
|
|
|
const addItemAction = action as AddItemAction;
|
|
|
|
// Add object to items array
|
|
|
|
const newState = [
|
|
|
|
...state,
|
|
|
|
{
|
|
|
|
id: addItemAction.id,
|
|
|
|
choiceId: addItemAction.choiceId,
|
|
|
|
groupId: addItemAction.groupId,
|
|
|
|
value: addItemAction.value,
|
|
|
|
label: addItemAction.label,
|
|
|
|
active: true,
|
|
|
|
highlighted: false,
|
|
|
|
customProperties: addItemAction.customProperties,
|
|
|
|
placeholder: addItemAction.placeholder || false,
|
|
|
|
keyCode: null,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return newState.map((obj: Item) => {
|
|
|
|
const item = obj;
|
|
|
|
item.highlighted = false;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'REMOVE_ITEM': {
|
|
|
|
// Set item to inactive
|
2021-12-17 22:26:52 +01:00
|
|
|
return state.map((obj) => {
|
2019-12-23 19:22:54 +01:00
|
|
|
const item = obj;
|
|
|
|
if (item.id === action.id) {
|
|
|
|
item.active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'HIGHLIGHT_ITEM': {
|
|
|
|
const highlightItemAction = action as HighlightItemAction;
|
|
|
|
|
2021-12-17 22:26:52 +01:00
|
|
|
return state.map((obj) => {
|
2019-12-23 19:22:54 +01:00
|
|
|
const item = obj;
|
|
|
|
if (item.id === highlightItemAction.id) {
|
|
|
|
item.highlighted = highlightItemAction.highlighted;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|