Adapting the address fetcher for rtes.

This commit is contained in:
Eric van der Vlist 2023-02-10 06:35:10 +01:00
parent 796147e4a9
commit 31669d8aac
8 changed files with 67 additions and 44 deletions

View File

@ -8,43 +8,34 @@ import { useI18n } from '@solid-primitives/i18n';
import dispatch from '../../workers/dispatcher-main'; import dispatch from '../../workers/dispatcher-main';
interface Props { interface Props {
target: () => any; wpt: () => Wpt;
putAction: string; dispatchParams: (wpt: Wpt) => { action: string; params: any };
putParamName: string;
} }
const DisplayOrGetAddress: Component<Props> = ({ const DisplayOrGetAddress: Component<Props> = ({ wpt, dispatchParams }) => {
target,
putAction,
putParamName,
}) => {
const [t, { add, locale, dict }] = useI18n(); const [t, { add, locale, dict }] = useI18n();
const findClickHandler = async () => { const findClickHandler = async () => {
const address = await findAddress(target().$.lon, target().$.lat, locale); console.log({ caller: 'findClickHandler', wpt: wpt() });
const updatedTarget = cloneDeep(target()); const address = await findAddress(wpt().$.lon, wpt().$.lat, locale);
const updatedTarget = cloneDeep(wpt());
if (updatedTarget.extensions === undefined) { if (updatedTarget.extensions === undefined) {
updatedTarget.extensions = {}; updatedTarget.extensions = {};
} }
updatedTarget.extensions.address = address; updatedTarget.extensions.address = address;
const params: any = { id: target().id }; dispatch(dispatchParams(updatedTarget));
params[putParamName] = updatedTarget;
dispatch({
action: putAction,
params,
});
}; };
return ( return (
<Show <Show
when={target().extensions?.address} when={wpt()?.extensions?.address}
fallback={ fallback={
<IconButton color='primary' onClick={findClickHandler}> <IconButton color='primary' onClick={findClickHandler}>
<SearchIcon /> <SearchIcon />
</IconButton> </IconButton>
} }
> >
<>{getVillageOrTown(target().extensions?.address)}</> <>{getVillageOrTown(wpt()?.extensions?.address)}</>
</Show> </Show>
); );
}; };

View File

@ -0,0 +1,36 @@
import { Component } from 'solid-js';
import { useI18n } from '@solid-primitives/i18n';
import DisplayOrGetAddress from '.';
interface Props {
id: string;
wpts: () => Wpt[];
putAction: string;
}
const DisplayOrGetStartEndAddresses: Component<Props> = (props) => {
const { id, wpts, putAction } = props;
const [t, { add, locale, dict }] = useI18n();
const dispatchParamsFactory = (index: number) => (wpt: Wpt) => ({
action: putAction,
params: { id, index, wpt },
});
return (
<>
{t('from')}{' '}
<DisplayOrGetAddress
wpt={() => wpts()?.at(0)}
dispatchParams={dispatchParamsFactory(0)}
/>{' '}
{t('to')}{' '}
<DisplayOrGetAddress
wpt={() => wpts()?.at(-1)}
dispatchParams={dispatchParamsFactory(-1)}
/>
</>
);
};
export default DisplayOrGetStartEndAddresses;

View File

@ -1 +1,2 @@
export { default } from './DisplayOrGetAddress'; export { default } from './DisplayOrGetAddress';
export { default as DisplayOrGetStartEndAddresses } from './DisplayOrGetStartEndAddresses';

View File

@ -7,7 +7,9 @@ import DeleteIcon from '@suid/icons-material/Delete';
import QuestionMarkIcon from '@suid/icons-material/QuestionMark'; import QuestionMarkIcon from '@suid/icons-material/QuestionMark';
import Tree from '../tree'; import Tree from '../tree';
import { LineString } from 'ol/geom'; import { LineString } from 'ol/geom';
import DisplayOrGetAddress from '../display-or-get-address'; import DisplayOrGetAddress, {
DisplayOrGetStartEndAddresses,
} from '../display-or-get-address';
import { getFormatedLength } from '../../lib/ol'; import { getFormatedLength } from '../../lib/ol';
import Alert from '../alert'; import Alert from '../alert';
import dispatch from '../../workers/dispatcher-main'; import dispatch from '../../workers/dispatcher-main';
@ -73,19 +75,11 @@ const RteViewer: Component<Props> = ({ rteId }) => {
content={ content={
<> <>
<div> <div>
{getFormatedLength(lineString)} {t('from')}{' '} {getFormatedLength(lineString)}
<Show when={false}> <DisplayOrGetStartEndAddresses
<DisplayOrGetAddress id={rteId}
target={() => rte().rtept.at(0)} wpts={() => rte().rtept}
putAction='putRtept'
putParamName='rtept'
/>{' '}
</Show>
{t('to')}{' '}
<DisplayOrGetAddress
target={() => rte().rtept.at(-1)}
putAction='putRtept' putAction='putRtept'
putParamName='rtept'
/> />
</div> </div>
</> </>

View File

@ -74,7 +74,6 @@ export const getRteDocs: ({
}) => Promise<{ docs: any; rte: Rte }> = async (params) => { }) => Promise<{ docs: any; rte: Rte }> = async (params) => {
const { id } = params; const { id } = params;
const docs = await getFamily(id, { include_docs: true }); const docs = await getFamily(id, { include_docs: true });
console.log({ caller: 'getRteDocs', id, docs });
let rte: Rte; let rte: Rte;
if (docs.rows.length === 1) { if (docs.rows.length === 1) {
rte = docs.rows[0].doc.doc; rte = docs.rows[0].doc.doc;
@ -91,7 +90,7 @@ export const getRteDocs: ({
caller: 'getRteDocs', caller: 'getRteDocs',
id, id,
docs, docs,
nbRteptIn: docs.rows[0].doc.rtept?.length, nbRteptIn: docs.rows[0].doc.doc.rtept?.length,
nbRteptTotal: rte?.rtept?.length, nbRteptTotal: rte?.rtept?.length,
}); });
return { docs, rte }; return { docs, rte };

View File

@ -69,7 +69,7 @@ describe('The rtept module with a real db', () => {
const rtePut = await putRte({ id: idRte, rte: rte0 }); const rtePut = await putRte({ id: idRte, rte: rte0 });
expect(rtePut).toEqual(rte0); expect(rtePut).toEqual(rte0);
const rtePutRtept = await putRtept({ idRte, index: 1, rtept }); const rtePutRtept = await putRtept({ id: idRte, index: 1, wpt: rtept });
const rteGetRtept = await getRte({ id: idRte }); const rteGetRtept = await getRte({ id: idRte });
expect(rteGetRtept).toEqual(rte1); expect(rteGetRtept).toEqual(rte1);
}); });
@ -123,9 +123,9 @@ describe('The rtept module with a real db', () => {
await put(idRtept, 'rtept', () => rtept3, emptyWpt); await put(idRtept, 'rtept', () => rtept3, emptyWpt);
expect(await getRte({ id: idRte })).toEqual(rte1); expect(await getRte({ id: idRte })).toEqual(rte1);
await putRtept({ await putRtept({
idRte, id: idRte,
index: 2, index: 2,
rtept: rtept4, wpt: rtept4,
}); });
expect((await getFamily(idRte)).rows.length).toEqual(2); expect((await getFamily(idRte)).rows.length).toEqual(2);
expect(await getRte({ id: idRte })).toEqual(rte2); expect(await getRte({ id: idRte })).toEqual(rte2);
@ -179,9 +179,9 @@ describe('The rtept module with a real db', () => {
const rtePut = await putRte({ id: idRte, rte: rte0 }); const rtePut = await putRte({ id: idRte, rte: rte0 });
await put(idRtept, 'rtept', () => rtept3, emptyWpt); await put(idRtept, 'rtept', () => rtept3, emptyWpt);
await putRtept({ await putRtept({
idRte, id: idRte,
index: -1, index: -1,
rtept: rtept4, wpt: rtept4,
}); });
expect(await getRte({ id: idRte })).toEqual(rte2); expect(await getRte({ id: idRte })).toEqual(rte2);
expect((await get(idRtept)).doc).toEqual(rtept4); expect((await get(idRtept)).doc).toEqual(rtept4);

View File

@ -26,8 +26,8 @@ export const emptyRtept: Wpt = {
}; };
export const putRtept = async (params: any) => { export const putRtept = async (params: any) => {
const { idRte, index, rtept } = params; const { id, index, wpt } = params;
const { docs } = await getRteDocs({ id: idRte }); const { docs } = await getRteDocs({ id: id });
const rte = docs.rows[0].doc.doc; const rte = docs.rows[0].doc.doc;
const nbRteptInRte = rte && rte.rtept ? rte.rtept.length : 0; const nbRteptInRte = rte && rte.rtept ? rte.rtept.length : 0;
const nbRteptOutRte = docs.rows.length - 1; const nbRteptOutRte = docs.rows.length - 1;
@ -41,11 +41,11 @@ export const putRtept = async (params: any) => {
// positiveIndex, // positiveIndex,
// }); // });
if (positiveIndex < nbRteptInRte) { if (positiveIndex < nbRteptInRte) {
rte.rtept[positiveIndex] = rtept; rte.rtept[positiveIndex] = wpt;
await putRte({ id: idRte, rte }); await putRte({ id, rte });
} else { } else {
const reptId = docs.rows[1 + positiveIndex - nbRteptInRte].doc._id; const reptId = docs.rows[1 + positiveIndex - nbRteptInRte].doc._id;
await put(reptId, 'rtept', () => rtept, emptyRtept); await put(reptId, 'rtept', () => wpt, emptyRtept);
} }
return rtept; return wpt;
}; };

View File

@ -14,6 +14,7 @@ import {
} from '../db/gpx'; } from '../db/gpx';
import { putOverlays } from '../db/overlays'; import { putOverlays } from '../db/overlays';
import { deleteRte, putRte } from '../db/rte'; import { deleteRte, putRte } from '../db/rte';
import { putRtept } from '../db/rtept';
import { getSettings, putSettings } from '../db/settings'; import { getSettings, putSettings } from '../db/settings';
import { getState, setState } from '../db/state'; import { getState, setState } from '../db/state';
import { deleteTrk, getTrk, putNewTrk } from '../db/trk'; import { deleteTrk, getTrk, putNewTrk } from '../db/trk';
@ -52,6 +53,7 @@ onmessage = async function (e) {
putWpt, putWpt,
putRte, putRte,
putTrkpt, putTrkpt,
putRtept,
deleteTrk, deleteTrk,
deleteRte, deleteRte,