From e7b27cda4e8a20089a8b28fc2ff25cf8af68184f Mon Sep 17 00:00:00 2001 From: evlist Date: Sat, 7 Jan 2023 15:41:16 +0100 Subject: [PATCH] Adding adresses to rtept rather than rte. --- src/components/rte/RteViewer.tsx | 40 +++++++++++++++++--------------- src/db/rte.ts | 1 + src/db/rtept.ts | 7 ++++++ src/db/types.d.ts | 1 + src/workers/dispatcher-worker.ts | 4 +++- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/components/rte/RteViewer.tsx b/src/components/rte/RteViewer.tsx index d1763f4..10b31bd 100644 --- a/src/components/rte/RteViewer.tsx +++ b/src/components/rte/RteViewer.tsx @@ -50,23 +50,21 @@ const RteViewer: Component = ({ rteId }) => { 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 = (target: string) => async () => { - const index = target === 'from' ? 0 : -1; - const address = await findAddress( - rte().rtept.at(index).$.lon, - rte().rtept.at(index).$.lat, - locale - ); - const updatedRte = cloneDeep(rte()); - if (updatedRte.extensions === undefined) { - updatedRte.extensions = {}; + const findClickHandlerFactory = (rtept: any) => async () => { + const address = await findAddress(rtept.$.lon, rtept.$.lat, locale); + const updatedRtept = cloneDeep(rtept); + if (updatedRtept.extensions === undefined) { + updatedRtept.extensions = {}; } - updatedRte.extensions[target] = address; + updatedRtept.extensions.address = address; dispatch({ - action: 'putRte', - params: { id: rteId, rte: updatedRte }, + action: 'putRtept', + params: { id: rtept.id, rtept: updatedRtept }, }); }; @@ -82,24 +80,28 @@ const RteViewer: Component = ({ rteId }) => {
{formatLength(length)} {t('from')}{' '} {getVillageOrTown(rte()?.extensions?.from)}} + when={!rte().rtept.at(0).extensions?.address} + fallback={ + <>{getVillageOrTown(rte().rtept.at(0).extensions?.address)} + } > {' '} {t('to')}{' '} {getVillageOrTown(rte()?.extensions?.to)}} + when={!rte().rtept.at(-1).extensions?.address} + fallback={ + <>{getVillageOrTown(rte().rtept.at(-1).extensions?.address)} + } > diff --git a/src/db/rte.ts b/src/db/rte.ts index eb6d342..7493db3 100644 --- a/src/db/rte.ts +++ b/src/db/rte.ts @@ -46,6 +46,7 @@ export const getRte = async (params: any) => { //level 1 if (row.doc.type === 'rtept') { target.splice(1); + row.doc.doc.id = row.doc._id; appendToArray(target.at(-1), row.doc.type, row.doc.doc); target.push(row.doc.doc); } diff --git a/src/db/rtept.ts b/src/db/rtept.ts index cb4a8c1..7868773 100644 --- a/src/db/rtept.ts +++ b/src/db/rtept.ts @@ -42,3 +42,10 @@ export const putNewRtept = async (id?: IdGpx | IdRte | IdRtept) => { ); return finalId as IdRtept; }; + +export const putRtept = async (params: any) => { + const { id, rtept } = params; + rtept.id = undefined; + await put(id, 'rtept', (doc) => rtept, rtept); + return rtept; +}; diff --git a/src/db/types.d.ts b/src/db/types.d.ts index e718070..0c5e565 100644 --- a/src/db/types.d.ts +++ b/src/db/types.d.ts @@ -49,6 +49,7 @@ interface Extensions { 'dyo:batterylevel'?: number; 'dyo:useragent'?: string; 'dyo:minZoom'?: number; + address: any; } interface Trk { diff --git a/src/workers/dispatcher-worker.ts b/src/workers/dispatcher-worker.ts index da86f71..2175d8c 100644 --- a/src/workers/dispatcher-worker.ts +++ b/src/workers/dispatcher-worker.ts @@ -13,6 +13,7 @@ import { getAllGpxesWithSummary, } from '../db/gpx'; import { putRte } from '../db/rte'; +import { putRtept } from '../db/rtept'; import { getSettings, putSettings } from '../db/settings'; import { getState, setState } from '../db/state'; import { getTrk, putNewTrk } from '../db/trk'; @@ -49,7 +50,8 @@ onmessage = async function (e) { putWpt, putRte, - + putRtept, + getState, setState,