`<Trk>` component

This commit is contained in:
Eric van der Vlist 2022-11-27 16:09:08 +01:00
parent 79fb39100f
commit 54f2ab06d9
6 changed files with 65 additions and 10 deletions

View File

@ -23,8 +23,9 @@ export const AllGpxes: Component<Props> = ({ map }) => {
});
return (
// @ts-ignore
<For each={allGpxIds()}>
{(gpxId) => {
{(gpxId: string) => {
console.log({ caller: 'AllGpxes / loop', gpxId });
return <Gpx map={map} gpxId={gpxId} />;
}}

View File

@ -1,9 +1,9 @@
import { Component, createEffect, createResource } from 'solid-js';
import { Component, createEffect, createResource, For } from 'solid-js';
import OlMap from 'ol/Map';
import dispatch from '../../workers/dispatcher-main';
//import Trk from './Trk';
import Trk from '../trk';
import getUri from '../../lib/ids';
interface Props {
@ -27,7 +27,15 @@ export const Gpx: Component<Props> = ({ map, gpxId }) => {
console.log({ caller: 'Gpx', map: map(), gpxId, gpx: gpx() });
});
return <></>;
return (
// @ts-ignore
<For each={gpx() ? gpx().trk || [] : []}>
{(trkId: string) => {
console.log({ caller: 'Gpx / loop', trkId });
return <Trk map={map} trkId={trkId} />;
}}
</For>
);
};
export default Gpx;

View File

@ -0,0 +1,33 @@
import { Component, createEffect, createResource } from 'solid-js';
import OlMap from 'ol/Map';
import dispatch from '../../workers/dispatcher-main';
import getUri from '../../lib/ids';
//import Trkseg from './Trkseg';
interface Props {
trkId: string;
map: () => OlMap | null;
}
export const Trk: Component<Props> = ({ map, trkId }) => {
const [trk] = createResource(
trkId,
async () =>
await dispatch({
action: 'getTrk',
params: {
id: trkId,
},
})
);
createEffect(() => {
console.log({ caller: 'Trk', map: map(), trkId, trk: trk() });
});
return <></>;
};
export default Trk;

View File

@ -0,0 +1 @@
export { default } from './Trk';

View File

@ -215,7 +215,7 @@ export const getGpxesForViewport = async (params: any) => {
);
};
const appendToArray = (target: any, key: string, value: any) => {
export const appendToArray = (target: any, key: string, value: any) => {
if (!(key in target)) {
target[key] = <any>[];
}

View File

@ -1,5 +1,5 @@
import getUri from '../lib/ids';
import { putNewGpx } from './gpx';
import { appendToArray, putNewGpx } from './gpx';
import { getFamily, put } from './lib';
export const emptyTrk: Trk = {
@ -35,8 +35,20 @@ export const putNewTrk = async (id?: IdTrk | IdGpx) => {
export const getTrk = async (params: any) => {
const { id } = params;
const docs = await getFamily(id, { include_docs: true });
console.log(`getTrk, uri: ${id} docs: ${JSON.stringify(docs)}`);
return docs.rows.filter(
(row: any) => row.doc.type === 'trk' || row.doc.type === 'trkseg'
);
let target: any[];
let trk: Trk | undefined = undefined;
docs.rows.forEach((row: any) => {
// level 0
if (row.doc.type === 'trk') {
target = [row.doc.doc];
trk = row.doc.doc;
}
//level 1
if (row.doc.type === 'trkseg') {
target.splice(1);
appendToArray(target.at(-1), row.doc.type, row.doc._id);
target.push(row.doc.doc);
}
});
return trk;
};