From dec79c9c2d81fda980f6016781fcb5b68ac3152a Mon Sep 17 00:00:00 2001 From: evlist Date: Thu, 2 Feb 2023 16:58:23 +0100 Subject: [PATCH] Refactoring to use the finder from en intent. --- src/components/finder/Finder.tsx | 66 +++++++++++++++++--------------- src/components/finder/index.ts | 2 +- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/components/finder/Finder.tsx b/src/components/finder/Finder.tsx index affe0d5..2ace33e 100644 --- a/src/components/finder/Finder.tsx +++ b/src/components/finder/Finder.tsx @@ -14,26 +14,56 @@ import { Overlay } from 'ol'; import { fromLonLat } from 'ol/proj'; import WptEditDialog from '../wpt/WptEditDialog'; +const [popup, setPopup] = createSignal(); +const [open, setOpen] = createSignal(false); +let popupElement: HTMLElement; + +export const findLocation = ( + navigate: (url: string) => void, + coordinate: Coordinate +) => { + setOpen(false); + if (popup() === undefined) { + setPopup( + new Overlay({ + element: popupElement, + positioning: 'center-center', + }) + ); + } + console.log({ + caller: 'Finder / submitHandler / popup', + coordinate, + popup: popup(), + popupElement, + }); + popup().setMap(getMap()); + popup().setPosition(fromLonLat(coordinate)); + getMap()?.addOverlay(popup()); + + navigate( + `/map/${getState().provider}/${coordinate[0]}/${coordinate[1]}/16/${ + getState().rotation + }` + ); +}; + interface Props {} const Finder: Component = (props) => { - const [open, setOpen] = createSignal(false); const [searchString, setSearchString] = createSignal(''); const [popupContent, setPopupContent] = createSignal(<>); - const navigate = useNavigate(); const [t, { add, locale, dict }] = useI18n(); - const [popup, setPopup] = createSignal(); const [coordinate, setCoordinate] = createSignal(); const searchStringChangeHandler = (event: any) => { setSearchString(event.target.value); }; + const navigate = useNavigate(); const popupCloseHandler = () => { popup()?.setMap(null); }; - let popupElement: HTMLElement; - const submitHandler = () => { const latLonRegExp = /(-?[0-9]+.[0-9]+)\s*,\s*(-?[0-9]+.[0-9]+)/; const latLonMatch = searchString().match(latLonRegExp); @@ -66,31 +96,7 @@ const Finder: Component = (props) => { } if (!!coordinate()) { - setOpen(false); - if (popup() === undefined) { - setPopup( - new Overlay({ - element: popupElement, - positioning: 'center-center', - }) - ); - } - console.log({ - caller: 'Finder / submitHandler / popup', - searchString: searchString(), - coordinate: coordinate(), - popup: popup(), - popupElement, - }); - popup().setMap(getMap()); - popup().setPosition(fromLonLat(coordinate())); - getMap()?.addOverlay(popup()); - - navigate( - `/map/${getState().provider}/${coordinate()[0]}/${coordinate()[1]}/16/${ - getState().rotation - }` - ); + findLocation(navigate, coordinate()); } }; diff --git a/src/components/finder/index.ts b/src/components/finder/index.ts index af60998..93a5b94 100644 --- a/src/components/finder/index.ts +++ b/src/components/finder/index.ts @@ -1 +1 @@ -export { default } from './Finder'; +export { default, findLocation } from './Finder';