Implementing a delete method for tracks.

This commit is contained in:
Eric van der Vlist 2023-01-08 22:05:08 +01:00
parent a2149f9a43
commit 45f7c797e7
3 changed files with 107 additions and 25 deletions

View File

@ -1,8 +1,12 @@
import { Component, For } from 'solid-js'; import { Component, createSignal, For, Show } from 'solid-js';
import { peekCachedSignal } from '../../workers/cached-signals'; import { peekCachedSignal } from '../../workers/cached-signals';
import Tree from '../tree'; import Tree from '../tree';
import TrkIcon from '../../icons/human-footprints-svgrepo-com.svg?component-solid'; import TrkIcon from '../../icons/human-footprints-svgrepo-com.svg?component-solid';
import { TrksegViewer } from '../trkseg'; import { TrksegViewer } from '../trkseg';
import { Alert, Button, IconButton } from '@suid/material';
import DeleteIcon from '@suid/icons-material/Delete';
import QuestionMarkIcon from '@suid/icons-material/QuestionMark';
import dispatch from '../../workers/dispatcher-main';
interface Props { interface Props {
trkId: string; trkId: string;
@ -12,11 +16,40 @@ interface Props {
const TrkViewer: Component<Props> = ({ trkId, restrictToHierarchy }) => { const TrkViewer: Component<Props> = ({ trkId, restrictToHierarchy }) => {
const trk = peekCachedSignal({ id: trkId, method: 'getTrk' }); const trk = peekCachedSignal({ id: trkId, method: 'getTrk' });
console.log({ caller: 'TrkViewer', trkId, restrictToHierarchy, trk: trk() }); console.log({ caller: 'TrkViewer', trkId, restrictToHierarchy, trk: trk() });
const [confirmDelete, setConfirmDelete] = createSignal(false);
const title = () => { const title = () => {
return trk().name; return trk().name;
}; };
const confirmDeleteHandler = () => {
console.log({
caller: 'TrkViewer / confirmDeleteHandler',
trkId,
});
setConfirmDelete(true);
};
const deleteHandler = () => {
console.log({
caller: 'TrkViewer / deleteHandler',
trkId,
});
dispatch({ action: 'deleteTrk', params: { id: trkId } });
setConfirmDelete(false);
};
const cancelDeleteHandler = () => {
console.log({
caller: 'TrkViewer / cancelDeleteHandler',
trkId,
});
setConfirmDelete(false);
};
return ( return (
<>
<Tree <Tree
title={ title={
<> <>
@ -25,7 +58,11 @@ const TrkViewer: Component<Props> = ({ trkId, restrictToHierarchy }) => {
<TrkIcon fill='white' width='24' height='24' /> {title()} <TrkIcon fill='white' width='24' height='24' /> {title()}
</span> </span>
</div> </div>
<div>x</div> <div>
<IconButton onClick={confirmDeleteHandler}>
<DeleteIcon />
</IconButton>
</div>
</> </>
} }
content={<div></div>} content={<div></div>}
@ -41,6 +78,35 @@ const TrkViewer: Component<Props> = ({ trkId, restrictToHierarchy }) => {
</For> </For>
} }
/> />
<Show when={confirmDelete()}>
<Alert
severity='warning'
icon={<QuestionMarkIcon />}
action={
<>
<Button
color='error'
variant='contained'
size='small'
onClick={deleteHandler}
>
YES
</Button>
<Button
color='primary'
variant='outlined'
size='small'
onclick={cancelDeleteHandler}
>
NO
</Button>
</>
}
>
Delete this track ?
</Alert>
</Show>
</>
); );
}; };

View File

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

View File

@ -16,7 +16,7 @@ import { 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';
import { getTrk, putNewTrk } from '../db/trk'; import { deleteTrk, getTrk, putNewTrk } from '../db/trk';
import { putTrkpt } from '../db/trkpt'; import { putTrkpt } from '../db/trkpt';
import { getTrkseg, appendTrkpt } from '../db/trkseg'; import { getTrkseg, appendTrkpt } from '../db/trkseg';
import { getWpt, putWpt } from '../db/wpt'; import { getWpt, putWpt } from '../db/wpt';
@ -54,6 +54,8 @@ onmessage = async function (e) {
putRtept, putRtept,
putTrkpt, putTrkpt,
deleteTrk,
getState, getState,
setState, setState,