parent
6dab7c18cc
commit
748334108c
@ -0,0 +1,79 @@ |
||||
import { nanoid } from 'nanoid'; |
||||
import { Marker, ReducerAction } from './types'; |
||||
import { reorderElements } from '../shared/collections'; |
||||
|
||||
const reindexMarkers = (markers: Marker[]) => |
||||
markers.map((marker, i) => { |
||||
const newLabel = `${i + 1}`; |
||||
if (marker.label === newLabel) { |
||||
return marker; |
||||
} |
||||
|
||||
return { |
||||
...marker, |
||||
label: newLabel, |
||||
}; |
||||
}); |
||||
|
||||
export const markersReducer = ( |
||||
markers: Marker[], |
||||
action: ReducerAction, |
||||
): Marker[] => { |
||||
const type = action.type; |
||||
switch (action.type) { |
||||
case 'add': { |
||||
const key = nanoid(10); |
||||
return reindexMarkers([ |
||||
...markers, |
||||
{ |
||||
key, |
||||
label: 'placeholder', |
||||
remove: () => action.data.remove(key), |
||||
moveUp: () => action.data.moveUp(key), |
||||
moveDown: () => action.data.moveDown(key), |
||||
coordinates: action.data.coordinates, |
||||
}, |
||||
]); |
||||
} |
||||
case 'remove': { |
||||
return reindexMarkers([ |
||||
...markers.filter(({ key }) => key !== action.data.key), |
||||
]); |
||||
} |
||||
case 'moveUp': { |
||||
const oldIndex = markers.findIndex( |
||||
({ key }) => key === action.data.key, |
||||
); |
||||
if (oldIndex < 1) { |
||||
return markers; |
||||
} |
||||
|
||||
return reindexMarkers( |
||||
reorderElements(markers, oldIndex, oldIndex - 1), |
||||
); |
||||
} |
||||
case 'moveDown': { |
||||
const oldIndex = markers.findIndex( |
||||
({ key }) => key === action.data.key, |
||||
); |
||||
if (oldIndex < 0 || oldIndex + 1 >= markers.length) { |
||||
return markers; |
||||
} |
||||
|
||||
return reindexMarkers( |
||||
reorderElements(markers, oldIndex, oldIndex + 1), |
||||
); |
||||
} |
||||
case 'reorder': { |
||||
return reindexMarkers( |
||||
reorderElements( |
||||
markers, |
||||
action.data.oldIndex, |
||||
action.data.newIndex, |
||||
), |
||||
); |
||||
} |
||||
default: |
||||
throw new Error(`Unsupported type ${type}`); |
||||
} |
||||
}; |
Loading…
Reference in new issue