implemented useSortable

main
Inga 🏳‍🌈 1 year ago
parent 04e340ad64
commit 562895c073
  1. 6
      src/routePlanner/hooks/useMarkers/reducer.ts
  2. 33
      src/routePlanner/hooks/useSortable/index.ts
  3. 29
      src/routePlanner/markers.tsx
  4. 4
      src/routePlanner/types.ts

@ -2,7 +2,7 @@ import { nanoid } from 'nanoid';
import type { Dispatch } from 'preact/hooks'; import type { Dispatch } from 'preact/hooks';
import { reorderElements } from '../../../shared/collections.js'; import { reorderElements } from '../../../shared/collections.js';
import { Coordinates } from '../../../shared/types'; import { Coordinates } from '../../../shared/types';
import { Marker, ReorderMarkersParams } from '../../types'; import { Marker, ReorderListParams } from '../../types';
type GenericReducerAction<TType extends string, TData> = { type GenericReducerAction<TType extends string, TData> = {
type: TType; type: TType;
@ -29,7 +29,7 @@ export type ReducerAction =
'changeLongLabel', 'changeLongLabel',
{ newLongLabel: string } { newLongLabel: string }
> >
| GenericReducerAction<'reorder', { oldIndex: number; newIndex: number }>; | GenericReducerAction<'reorder', ReorderListParams>;
const reindexMarkers = (markers: Marker[]) => const reindexMarkers = (markers: Marker[]) =>
markers.map((marker, i) => { markers.map((marker, i) => {
@ -152,7 +152,7 @@ export const createChangeStateMethods = (
dispatcher: dispatchMarkers, dispatcher: dispatchMarkers,
}, },
}), }),
reorderMarkers: ({ oldIndex, newIndex }: ReorderMarkersParams): void => reorderMarkers: ({ oldIndex, newIndex }: ReorderListParams): void =>
dispatchMarkers({ dispatchMarkers({
type: 'reorder', type: 'reorder',
data: { oldIndex, newIndex }, data: { oldIndex, newIndex },

@ -0,0 +1,33 @@
import { useEffect, Ref } from 'preact/hooks';
import Sortable from 'sortablejs';
import { ReorderListParams } from '../../types';
export const useSortable = (
listContainerRef: Readonly<Ref<HTMLElement>>,
onListReorder: (params: ReorderListParams) => void,
) => {
useEffect(() => {
if (!listContainerRef.current) {
return;
}
const sortable = Sortable.create(listContainerRef.current, {
onEnd: (event) => {
if (
event.oldIndex === undefined ||
event.newIndex === undefined
) {
return;
}
onListReorder({
oldIndex: event.oldIndex,
newIndex: event.newIndex,
});
},
});
return () => {
sortable.destroy();
};
}, [listContainerRef, onListReorder]);
};

@ -1,37 +1,14 @@
import { useEffect, useRef } from 'preact/hooks'; import { useRef } from 'preact/hooks';
import Sortable from 'sortablejs';
import { MarkerComponent } from './marker'; import { MarkerComponent } from './marker';
import { MarkersProps } from './types'; import { MarkersProps } from './types';
import { useSortable } from './hooks/useSortable';
export const MarkersComponent = ({ export const MarkersComponent = ({
markers, markers,
onMarkersReorder, onMarkersReorder,
}: MarkersProps) => { }: MarkersProps) => {
const listContainerRef = useRef<HTMLOListElement>(null); const listContainerRef = useRef<HTMLOListElement>(null);
useEffect(() => { useSortable(listContainerRef, onMarkersReorder);
if (!listContainerRef.current) {
return;
}
const sortable = Sortable.create(listContainerRef.current, {
onEnd: (event) => {
if (
event.oldIndex === undefined ||
event.newIndex === undefined
) {
return;
}
onMarkersReorder({
oldIndex: event.oldIndex,
newIndex: event.newIndex,
});
},
});
return () => {
sortable.destroy();
};
}, [listContainerRef, markers, onMarkersReorder]);
return ( return (
<ol ref={listContainerRef}> <ol ref={listContainerRef}>

@ -16,14 +16,14 @@ export type Marker = Waypoint & {
longLabel: string; longLabel: string;
}; };
export type ReorderMarkersParams = { export type ReorderListParams = {
oldIndex: number; oldIndex: number;
newIndex: number; newIndex: number;
}; };
export type MarkersProps = { export type MarkersProps = {
markers: Marker[]; markers: Marker[];
onMarkersReorder: (params: ReorderMarkersParams) => void; onMarkersReorder: (params: ReorderListParams) => void;
}; };
export type MapProps = { export type MapProps = {

Loading…
Cancel
Save