simplified markers reducer

main
Inga 🏳‍🌈 1 year ago
parent 7b95b70a10
commit 00c2abdcf2
  1. 88
      src/routePlanner/useMarkers.ts

@ -19,10 +19,7 @@ export type ReducerAction =
'add', 'add',
{ {
coordinates: Coordinates; coordinates: Coordinates;
remove: (key: string) => void; dispatcher: Dispatch<ReducerAction>;
moveUp: (key: string) => void;
moveDown: (key: string) => void;
changeLongLabel: (key: string, newLongLabel: string) => void;
} }
> >
| GenericMarkerChangeReducerAction<'remove'> | GenericMarkerChangeReducerAction<'remove'>
@ -65,11 +62,26 @@ export const markersReducer = (
key, key,
shortLabel: 'placeholder', shortLabel: 'placeholder',
longLabel: key.substring(0, 4), longLabel: key.substring(0, 4),
remove: () => action.data.remove(key), remove: () =>
moveUp: () => action.data.moveUp(key), action.data.dispatcher({
moveDown: () => action.data.moveDown(key), type: 'remove',
data: { key },
}),
moveUp: () =>
action.data.dispatcher({
type: 'moveUp',
data: { key },
}),
moveDown: () =>
action.data.dispatcher({
type: 'moveDown',
data: { key },
}),
changeLongLabel: (newLongLabel: string) => changeLongLabel: (newLongLabel: string) =>
action.data.changeLongLabel(key, newLongLabel), action.data.dispatcher({
type: 'changeLongLabel',
data: { key, newLongLabel },
}),
coordinates: action.data.coordinates, coordinates: action.data.coordinates,
}, },
]); ]);
@ -131,61 +143,21 @@ export const markersReducer = (
export const createChangeStateMethods = ( export const createChangeStateMethods = (
dispatchMarkers: Dispatch<ReducerAction>, dispatchMarkers: Dispatch<ReducerAction>,
) => { ) => ({
const reorderMarkers = ({ addMarker: (coordinates: Coordinates) =>
oldIndex,
newIndex,
}: ReorderMarkersParams): void =>
dispatchMarkers({
type: 'reorder',
data: { oldIndex, newIndex },
});
const moveMarkerUp = (key: string) =>
dispatchMarkers({
type: 'moveUp',
data: { key },
});
const moveMarkerDown = (key: string) =>
dispatchMarkers({
type: 'moveDown',
data: { key },
});
const removeMarker = (key: string) =>
dispatchMarkers({
type: 'remove',
data: { key },
});
const changeMarkerLongLabel = (key: string, newLongLabel: string) =>
dispatchMarkers({
type: 'changeLongLabel',
data: { key, newLongLabel },
});
const addMarker = (coordinates: Coordinates) =>
dispatchMarkers({ dispatchMarkers({
type: 'add', type: 'add',
data: { data: {
coordinates, coordinates,
moveUp: moveMarkerUp, dispatcher: dispatchMarkers,
moveDown: moveMarkerDown,
remove: removeMarker,
changeLongLabel: changeMarkerLongLabel,
}, },
}); }),
reorderMarkers: ({ oldIndex, newIndex }: ReorderMarkersParams): void =>
return { dispatchMarkers({
reorderMarkers, type: 'reorder',
moveMarkerUp, data: { oldIndex, newIndex },
moveMarkerDown, }),
removeMarker, });
addMarker,
changeMarkerLongLabel,
};
};
export const useMarkers = () => { export const useMarkers = () => {
const [markers, dispatchMarkers] = useReducer(markersReducer, []); const [markers, dispatchMarkers] = useReducer(markersReducer, []);

Loading…
Cancel
Save