From e48de7075053d024f3b45f08bd812d8ff89bcf84 Mon Sep 17 00:00:00 2001 From: Inga Date: Sun, 19 Nov 2023 02:53:44 +0000 Subject: [PATCH] reorganized project layout (useMarkers) --- src/routePlanner/hooks/useMarkers/index.ts | 12 ++++++++++++ .../useMarkers/reducer.test.ts} | 4 ++-- .../useMarkers/reducer.ts} | 18 ++++-------------- src/routePlanner/index.tsx | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 src/routePlanner/hooks/useMarkers/index.ts rename src/routePlanner/{useMarkers.test.ts => hooks/useMarkers/reducer.test.ts} (99%) rename src/routePlanner/{useMarkers.ts => hooks/useMarkers/reducer.ts} (90%) diff --git a/src/routePlanner/hooks/useMarkers/index.ts b/src/routePlanner/hooks/useMarkers/index.ts new file mode 100644 index 0000000..3ce8962 --- /dev/null +++ b/src/routePlanner/hooks/useMarkers/index.ts @@ -0,0 +1,12 @@ +import { useMemo, useReducer } from 'preact/hooks'; +import { createChangeStateMethods, markersReducer } from './reducer'; + +export const useMarkers = () => { + const [markers, dispatchMarkers] = useReducer(markersReducer, []); + const changeStateMethods = useMemo( + () => createChangeStateMethods(dispatchMarkers), + [], + ); + + return [markers, changeStateMethods] as const; +}; diff --git a/src/routePlanner/useMarkers.test.ts b/src/routePlanner/hooks/useMarkers/reducer.test.ts similarity index 99% rename from src/routePlanner/useMarkers.test.ts rename to src/routePlanner/hooks/useMarkers/reducer.test.ts index 79a1ac2..0a6b978 100644 --- a/src/routePlanner/useMarkers.test.ts +++ b/src/routePlanner/hooks/useMarkers/reducer.test.ts @@ -1,10 +1,10 @@ import t from 'tap'; -import { Marker } from './types'; +import { Marker } from '../../types'; import { ReducerAction, createChangeStateMethods, markersReducer, -} from './useMarkers.js'; +} from './reducer.js'; // Testing entire `useMarkers` custom hook would require us to create test preact components here // and render them into virtual DOM and check the results, which is just too much hassle diff --git a/src/routePlanner/useMarkers.ts b/src/routePlanner/hooks/useMarkers/reducer.ts similarity index 90% rename from src/routePlanner/useMarkers.ts rename to src/routePlanner/hooks/useMarkers/reducer.ts index 6318b4c..ae87bd6 100644 --- a/src/routePlanner/useMarkers.ts +++ b/src/routePlanner/hooks/useMarkers/reducer.ts @@ -1,8 +1,8 @@ import { nanoid } from 'nanoid'; -import { Dispatch, useMemo, useReducer } from 'preact/hooks'; -import { reorderElements } from '../shared/collections.js'; -import { Coordinates } from '../shared/types'; -import { Marker, ReorderMarkersParams } from './types'; +import type { Dispatch } from 'preact/hooks'; +import { reorderElements } from '../../../shared/collections.js'; +import { Coordinates } from '../../../shared/types'; +import { Marker, ReorderMarkersParams } from '../../types'; type GenericReducerAction = { type: TType; @@ -158,13 +158,3 @@ export const createChangeStateMethods = ( data: { oldIndex, newIndex }, }), }); - -export const useMarkers = () => { - const [markers, dispatchMarkers] = useReducer(markersReducer, []); - const changeStateMethods = useMemo( - () => createChangeStateMethods(dispatchMarkers), - [], - ); - - return [markers, changeStateMethods] as const; -}; diff --git a/src/routePlanner/index.tsx b/src/routePlanner/index.tsx index f156ec2..880ef2a 100644 --- a/src/routePlanner/index.tsx +++ b/src/routePlanner/index.tsx @@ -1,7 +1,7 @@ import { ExportComponent } from './export'; import { MapComponent } from './map'; import { MarkersComponent } from './markers'; -import { useMarkers } from './useMarkers'; +import { useMarkers } from './hooks/useMarkers'; import './style.css';