More robust id handling in imports and rte delete.

This commit is contained in:
Eric van der Vlist 2023-02-03 14:57:40 +01:00
parent 5658cc0556
commit db337892c4
4 changed files with 150 additions and 43 deletions

View File

@ -1,12 +1,16 @@
import { Component } from 'solid-js'; import { Component, createSignal } from 'solid-js';
import { peekCachedSignal } from '../../workers/cached-signals'; import { peekCachedSignal } from '../../workers/cached-signals';
import RteIcon from '../../icons/directions-svgrepo-com.svg?component-solid'; import RteIcon from '../../icons/directions-svgrepo-com.svg?component-solid';
import { useI18n } from '@solid-primitives/i18n'; import { useI18n } from '@solid-primitives/i18n';
import { Button, IconButton } from '@suid/material';
import DeleteIcon from '@suid/icons-material/Delete';
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 from '../display-or-get-address';
import { getFormatedLength } from '../../lib/ol'; import { getFormatedLength } from '../../lib/ol';
import Alert from '../alert';
import dispatch from '../../workers/dispatcher-main';
interface Props { interface Props {
rteId: string; rteId: string;
@ -21,39 +25,100 @@ const RteViewer: Component<Props> = ({ rteId }) => {
return rte().name; return rte().name;
}; };
const [confirmDelete, setConfirmDelete] = createSignal(false);
const lineString = new LineString( const lineString = new LineString(
rte().rtept.map((rtept: any) => [rtept.$.lon, rtept.$.lat]) rte().rtept.map((rtept: any) => [rtept.$.lon, rtept.$.lat])
); );
const confirmDeleteHandler = () => {
console.log({
caller: 'RteViewer / confirmDeleteHandler',
rteId,
});
setConfirmDelete(true);
};
const deleteHandler = () => {
console.log({
caller: 'RteViewer / deleteHandler',
rteId,
});
dispatch({ action: 'deleteRte', params: { id: rteId } });
setConfirmDelete(false);
};
const cancelDeleteHandler = () => {
console.log({
caller: 'RteViewer / cancelDeleteHandler',
rteId,
});
setConfirmDelete(false);
};
return ( return (
<Tree <>
title={ <Tree
<> title={
<span> <>
<RteIcon fill='black' width='24' height='24' /> {title()} <span>
</span> <RteIcon fill='black' width='24' height='24' /> {title()}
</> </span>
} <div>
content={ <IconButton onClick={confirmDeleteHandler}>
<> <DeleteIcon fill='white' />
<div> </IconButton>
{getFormatedLength(lineString)} {t('from')}{' '} </div>
<DisplayOrGetAddress </>
target={() => rte().rtept.at(0)} }
putAction='putRtept' content={
putParamName='rtept' <>
/>{' '} <div>
{t('to')}{' '} {getFormatedLength(lineString)} {t('from')}{' '}
<DisplayOrGetAddress <DisplayOrGetAddress
target={() => rte().rtept.at(-1)} target={() => rte().rtept.at(0)}
putAction='putRtept' putAction='putRtept'
putParamName='rtept' putParamName='rtept'
/> />{' '}
</div> {t('to')}{' '}
</> <DisplayOrGetAddress
} target={() => rte().rtept.at(-1)}
subTree={undefined} putAction='putRtept'
/> putParamName='rtept'
/>
</div>
</>
}
subTree={undefined}
/>
<Alert
open={confirmDelete}
closeHandler={cancelDeleteHandler}
severity='warning'
icon={<QuestionMarkIcon />}
action={
<>
<Button
color='error'
variant='contained'
size='small'
onClick={deleteHandler}
>
{t('yes')}
</Button>
<Button
color='primary'
variant='outlined'
size='small'
onclick={cancelDeleteHandler}
>
{t('no')}
</Button>
</>
}
>
{t('deleteTrack')}
</Alert>
</>
); );
}; };

View File

@ -68,7 +68,14 @@ export const existsGpx = async (id: IdGpx) => {
const prune = ( const prune = (
id: any, id: any,
object: any, object: any,
previousGpx: Gpx | null, previousIds: {
rte: number;
wpt: number;
trk: number;
rtept: number;
trkseg: number;
trkpt: number;
},
extensions: any, extensions: any,
docs: any[] docs: any[]
) => { ) => {
@ -83,11 +90,6 @@ const prune = (
key === 'trkpt' key === 'trkpt'
) { ) {
const subObjects = object[key]; const subObjects = object[key];
let previousId = 0;
let nbPreviousObjects = 0;
if (previousGpx !== null && key in previousGpx) {
nbPreviousObjects = previousGpx[key].length;
}
for (const index in subObjects) { for (const index in subObjects) {
const subId = { ...id }; const subId = { ...id };
if (key === 'trkpt') { if (key === 'trkpt') {
@ -95,11 +97,13 @@ const prune = (
const normalId = intToTrkptId( const normalId = intToTrkptId(
new Date(object[key][index].time).valueOf() new Date(object[key][index].time).valueOf()
); );
const id = normalId > previousId ? normalId : previousId + 1; const id =
normalId > previousIds.trk ? normalId : previousIds.trk + 1;
subId[key] = id; subId[key] = id;
previousId = id; previousIds.trk = id;
} else { } else {
subId[key] = index + nbPreviousObjects; previousIds[key] = previousIds[key] + 1;
subId[key] = previousIds[key];
} }
// console.log({ // console.log({
// caller: 'prune', // caller: 'prune',
@ -121,10 +125,10 @@ const prune = (
type: key, type: key,
doc: subObjects[index], doc: subObjects[index],
}); });
prune(subId, subObjects[index], previousGpx, {}, docs); prune(subId, subObjects[index], previousIds, {}, docs);
} }
object[key] = undefined; object[key] = undefined;
} else prune(id, object[key], previousGpx, extensions, docs); } else prune(id, object[key], previousIds, extensions, docs);
} }
} }
}; };
@ -284,7 +288,30 @@ export const pruneAndSaveImportedGpx = async (params: any) => {
gpxId, gpxId,
}); });
} }
prune(gpxId, gpx, previousGpx, extensions, docs); let previousIds = {
rte: -1,
wpt: -1,
trk: -1,
rtept: -1,
trkseg: -1,
trkpt: -1,
};
if (!!previousGpx?.wpt) {
const wptUri = previousGpx.wpt.at(-1);
const wptId = getUri('wpt', wptUri);
previousIds.wpt = wptId.wpt;
}
if (!!previousGpx?.rte) {
const rteUri = previousGpx.rte.at(-1);
const rteId = getUri('rte', rteUri);
previousIds.rte = rteId.rte;
}
if (!!previousGpx?.trk) {
const trkUri = previousGpx.trk.at(-1);
const trkId = getUri('trk', trkUri);
previousIds.trk = trkId.trk;
}
prune(gpxId, gpx, previousIds, extensions, docs);
console.log({ caller: 'pruneAndSaveImportedGpx / pruned', docs }); console.log({ caller: 'pruneAndSaveImportedGpx / pruned', docs });
try { try {
const result = await putAll(docs); const result = await putAll(docs);

View File

@ -71,3 +71,17 @@ export const putRte = async (params: any) => {
await put(id, 'rte', (doc) => rte, rte); await put(id, 'rte', (doc) => rte, rte);
return rte; return rte;
}; };
export const deleteRte = async (params: any) => {
const { id } = params;
const docs = await getFamily(id, { include_docs: false });
console.log({ caller: 'deleteRte' }, id, docs);
const deletedDocs = docs.rows.reverse().map((doc: any) => ({
_deleted: true,
_id: doc.id,
_rev: doc.value.rev,
}));
console.log({ caller: 'deleteRte' }, id, docs, deletedDocs);
await db.bulkDocs(deletedDocs);
return id;
};

View File

@ -13,7 +13,7 @@ import {
getAllGpxesWithSummary, getAllGpxesWithSummary,
} from '../db/gpx'; } from '../db/gpx';
import { putOverlays } from '../db/overlays'; import { putOverlays } from '../db/overlays';
import { putRte } from '../db/rte'; import { deleteRte, putRte } from '../db/rte';
import { putRtept } from '../db/rtept'; 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';
@ -56,6 +56,7 @@ onmessage = async function (e) {
putTrkpt, putTrkpt,
deleteTrk, deleteTrk,
deleteRte,
getState, getState,
setState, setState,