minor tweaks and optimizations

main
Inga 🏳‍🌈 1 year ago
parent ed1f4bc47c
commit 1022ad5e97
  1. 8
      src/routePlanner/export.tsx
  2. 6
      src/routePlanner/hooks/useMarkers/reducer.ts
  3. 7
      src/shared/collections.test.ts
  4. 7
      src/shared/collections.ts

@ -3,10 +3,8 @@ import type { ExportProps } from './types';
export const ExportComponent = ({ markers }: ExportProps) => { export const ExportComponent = ({ markers }: ExportProps) => {
return ( return (
<> <button onClick={() => exportMarkersToGpx(markers)}>
<button onClick={() => exportMarkersToGpx(markers)}> Download your route
Download your route </button>
</button>
</>
); );
}; };

@ -56,11 +56,11 @@ export const markersReducer = (
switch (type) { switch (type) {
case 'add': { case 'add': {
const key = nanoid(10); const key = nanoid(10);
return reindexMarkers([ return [
...markers, ...markers,
{ {
key, key,
shortLabel: 'placeholder', shortLabel: `${markers.length + 1}`,
longLabel: key.substring(0, 4), longLabel: key.substring(0, 4),
remove: () => remove: () =>
action.data.dispatcher({ action.data.dispatcher({
@ -84,7 +84,7 @@ export const markersReducer = (
}), }),
coordinates: action.data.coordinates, coordinates: action.data.coordinates,
}, },
]); ];
} }
case 'remove': { case 'remove': {
return reindexMarkers([ return reindexMarkers([

@ -13,13 +13,6 @@ void t.test('reorderElements', (t) => {
); );
}; };
checkReordering(0, -1, 'bcde');
checkReordering(1, -1, 'acde');
checkReordering(2, -1, 'abde');
checkReordering(3, -1, 'abce');
checkReordering(4, -1, 'abcd');
checkReordering(5, -1, 'abcde');
checkReordering(0, 0, 'abcde'); checkReordering(0, 0, 'abcde');
checkReordering(0, 1, 'bacde'); checkReordering(0, 1, 'bacde');
checkReordering(0, 2, 'bcade'); checkReordering(0, 2, 'bcade');

@ -3,13 +3,6 @@ export const reorderElements = <T>(
oldIndex: number, oldIndex: number,
newIndex: number, newIndex: number,
) => { ) => {
if (newIndex < 0) {
return [
...collection.slice(0, oldIndex),
...collection.slice(oldIndex + 1, collection.length),
];
}
if (oldIndex < newIndex) { if (oldIndex < newIndex) {
return [ return [
...collection.slice(0, oldIndex), ...collection.slice(0, oldIndex),

Loading…
Cancel
Save