I am trying to find out how can I update the state in redux so that I can move the elements up an d down on click.
state is an array of objects, currently 3 elements with id, text, and isCrossed: false. On click I am passing the id so action.id will be the id of the element I want to move.
My current code below but it does not work. It seem to only move once then the code breaks. Ive added if statement to avoid moving the first element up
case 'UP_TODO': {
return state.map((todo, idx, arr) => {
if (action !== arr[0]) {
return (todo.id === action.id ? arr[idx - 1] : todo.id === action.id - 1 ? arr[idx + 1] : todo)
} else {
return todo
}
});
}