From 7483d9f904e2bbb7bd1273cdd54e275305e6702b Mon Sep 17 00:00:00 2001 From: evlist Date: Sat, 7 Jan 2023 18:00:35 +0100 Subject: [PATCH] Refactoring --- .../DisplayOrGetAddress.tsx | 52 +++++++++++ .../display-or-get-address/index.ts | 1 + src/components/rte/RteViewer.tsx | 90 ++++--------------- src/lib/ol.ts | 17 ++++ 4 files changed, 86 insertions(+), 74 deletions(-) create mode 100644 src/components/display-or-get-address/DisplayOrGetAddress.tsx create mode 100644 src/components/display-or-get-address/index.ts create mode 100644 src/lib/ol.ts diff --git a/src/components/display-or-get-address/DisplayOrGetAddress.tsx b/src/components/display-or-get-address/DisplayOrGetAddress.tsx new file mode 100644 index 0000000..5eea73b --- /dev/null +++ b/src/components/display-or-get-address/DisplayOrGetAddress.tsx @@ -0,0 +1,52 @@ +import { IconButton } from '@suid/material'; +import SearchIcon from '@suid/icons-material/Search'; + +import { Component, Show } from 'solid-js'; +import { findAddress, getVillageOrTown } from '../../lib/osm'; +import { cloneDeep } from 'lodash'; +import { useI18n } from '@solid-primitives/i18n'; +import dispatch from '../../workers/dispatcher-main'; + +interface Props { + target: any; + putAction: string; + putParamName: string; +} + +const DisplayOrGetAddress: Component = ({ + target, + putAction, + putParamName, +}) => { + const [t, { add, locale, dict }] = useI18n(); + + const findClickHandlerFactory = (target: any) => async () => { + const address = await findAddress(target.$.lon, target.$.lat, locale); + const updatedTarget = cloneDeep(target); + if (updatedTarget.extensions === undefined) { + updatedTarget.extensions = {}; + } + updatedTarget.extensions.address = address; + const params: any = { id: target.id }; + params[putParamName] = updatedTarget; + dispatch({ + action: putAction, + params, + }); + }; + + return ( + + + + } + > + <>{getVillageOrTown(target.extensions?.address)} + + ); +}; + +export default DisplayOrGetAddress; diff --git a/src/components/display-or-get-address/index.ts b/src/components/display-or-get-address/index.ts new file mode 100644 index 0000000..bd61935 --- /dev/null +++ b/src/components/display-or-get-address/index.ts @@ -0,0 +1 @@ +export { default } from './DisplayOrGetAddress'; diff --git a/src/components/rte/RteViewer.tsx b/src/components/rte/RteViewer.tsx index 10b31bd..e2db6fd 100644 --- a/src/components/rte/RteViewer.tsx +++ b/src/components/rte/RteViewer.tsx @@ -1,16 +1,13 @@ -import { Component, createResource, Show } from 'solid-js'; +import { Component } from 'solid-js'; import { peekCachedSignal } from '../../workers/cached-signals'; import RteIcon from '../../icons/directions-svgrepo-com.svg?component-solid'; -import SearchIcon from '@suid/icons-material/Search'; import { useI18n } from '@solid-primitives/i18n'; import Tree from '../tree'; -import { findAddress, getVillageOrTown } from '../../lib/osm'; import { getLength } from 'ol/sphere'; -import { Geometry, LineString } from 'ol/geom'; -import { IconButton } from '@suid/material'; -import { cloneDeep } from 'lodash'; -import dispatch from '../../workers/dispatcher-main'; +import { LineString } from 'ol/geom'; +import DisplayOrGetAddress from '../display-or-get-address'; +import { getFormatedLength } from '../../lib/ol'; interface Props { rteId: string; @@ -25,49 +22,10 @@ const RteViewer: Component = ({ rteId }) => { return rte().name; }; - // const [startAddress] = createResource(rte, (r: Rte) => - // findAddress(r.rtept[0].$.lon, r.rtept[0].$.lat, locale) - // ); - // const [endAddress] = createResource(rte, (r: Rte) => - // findAddress(r.rtept.at(-1).$.lon, r.rtept.at(-1).$.lat, locale) - // ); - - const formatLength = (length: number) => { - if (length > 1000) { - const l = length / 1000; - return ` ${l.toLocaleString(undefined, { - minimumFractionDigits: 2, - maximumFractionDigits: 2, - })} km`; - } - return ` ${length.toLocaleString(undefined, { - minimumFractionDigits: 0, - maximumFractionDigits: 0, - })} m`; - }; - - const geometry = new LineString( + const lineString = new LineString( rte().rtept.map((rtept: any) => [rtept.$.lon, rtept.$.lat]) ); - const firstRtept = rte().rtept.at(0); - const lastRtept = rte().rtept.at(-1); - - const length = getLength(geometry, { projection: 'EPSG:4326' }); - - const findClickHandlerFactory = (rtept: any) => async () => { - const address = await findAddress(rtept.$.lon, rtept.$.lat, locale); - const updatedRtept = cloneDeep(rtept); - if (updatedRtept.extensions === undefined) { - updatedRtept.extensions = {}; - } - updatedRtept.extensions.address = address; - dispatch({ - action: 'putRtept', - params: { id: rtept.id, rtept: updatedRtept }, - }); - }; - return ( = ({ rteId }) => { content={ <>
- {formatLength(length)} {t('from')}{' '} - {getVillageOrTown(rte().rtept.at(0).extensions?.address)} - } - > - - - - {' '} + {getFormatedLength(lineString)} {t('from')}{' '} + {' '} {t('to')}{' '} - {getVillageOrTown(rte().rtept.at(-1).extensions?.address)} - } - > - - - - +
} diff --git a/src/lib/ol.ts b/src/lib/ol.ts new file mode 100644 index 0000000..6155a06 --- /dev/null +++ b/src/lib/ol.ts @@ -0,0 +1,17 @@ +import { LineString } from 'ol/geom'; +import { getLength } from 'ol/sphere'; + +export const getFormatedLength = (lineString: LineString) => { + const length = getLength(lineString, { projection: 'EPSG:4326' }); + if (length > 1000) { + const l = length / 1000; + return ` ${l.toLocaleString(undefined, { + minimumFractionDigits: 2, + maximumFractionDigits: 2, + })} km`; + } + return ` ${length.toLocaleString(undefined, { + minimumFractionDigits: 0, + maximumFractionDigits: 0, + })} m`; +};