diff --git a/src/components/gpx-import/GpxImport.tsx b/src/components/gpx-import/GpxImport.tsx index c95df42..ecd4e14 100644 --- a/src/components/gpx-import/GpxImport.tsx +++ b/src/components/gpx-import/GpxImport.tsx @@ -12,44 +12,54 @@ import { intToGpxId } from '../../lib/ids'; const GpxImport: Component = () => { const onChangeHandler = (event: any) => { - console.log('On change handler'); - const file: File = event.target.files[0]; - const fileReader = new FileReader(); - fileReader.readAsText(file); + console.log({ + caller: 'GpxImport / On change handler', + files: event.target.files, + }); + for (const file of event.target.files) { + const fileReader = new FileReader(); + fileReader.readAsText(file); - fileReader.addEventListener( - 'load', - () => { - // this will then display a text file - console.log({ caller: 'GpxImport / XML', result: fileReader.result }); - const gpx = GPX.parse(fileReader.result); - console.log({ caller: 'GpxImport / JSON', gpx }); - if (gpx) { - const startTime = new Date(findStartTime(gpx)!); - dispatch({ - action: 'pruneAndSaveImportedGpx', - params: { - id: { gpx: intToGpxId(startTime.valueOf()) }, - gpx: gpx, - tech: { - lastModified: new Date(file.lastModified).toISOString(), - importDate: new Date().toISOString(), - name: file.name, - size: file.size, - type: file.type, + fileReader.addEventListener( + 'load', + async () => { + // this will then display a text file + console.log({ + caller: 'GpxImport / XML', + file, + result: fileReader.result, + }); + const gpx = GPX.parse(fileReader.result); + console.log({ caller: 'GpxImport / JSON', file, gpx }); + if (gpx) { + const startTime = new Date(findStartTime(gpx)!); + await dispatch({ + action: 'pruneAndSaveImportedGpx', + params: { + id: { gpx: intToGpxId(startTime.valueOf()) }, + gpx: gpx, + tech: { + lastModified: new Date(file.lastModified).toISOString(), + importDate: new Date().toISOString(), + name: file.name, + size: file.size, + type: file.type, + }, }, - }, - }); - } else { - console.error({ - message: "can't parse GPX file", - xml: fileReader.result, - }); - } - // TODO: error handling - }, - false - ); + }); + console.log({ caller: 'GpxImport / JSON / done', file, gpx }); + } else { + console.error({ + message: "can't parse GPX file", + file, + xml: fileReader.result, + }); + } + // TODO: error handling + }, + false + ); + } }; return ( @@ -63,6 +73,7 @@ const GpxImport: Component = () => { id='gpx-import' class={css.inputFile} accept='.gpx' + multiple={true} onChange={onChangeHandler} /> diff --git a/src/db/gpx.ts b/src/db/gpx.ts index 8bcbab3..5320489 100644 --- a/src/db/gpx.ts +++ b/src/db/gpx.ts @@ -72,12 +72,20 @@ const prune = (id: any, object: any, docs: any[]) => { key === 'trkpt' ) { const subObjects = object[key]; + let previousId = 0; for (const index in subObjects) { const subId = { ...id }; - subId[key] = - key === 'trkpt' - ? intToTrkptId(new Date(object[key][index].time).valueOf()) - : index; + if (key === 'trkpt') { + // fix buggy times in GPX tracks + const normalId = intToTrkptId( + new Date(object[key][index].time).valueOf() + ); + const id = normalId > previousId ? normalId : previousId + 1; + subId[key] = id; + previousId = id; + } else { + subId[key] = index; + } // console.log({ // caller: 'prune', // id, diff --git a/src/db/index.ts b/src/db/index.ts index 40d3321..84b628d 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -25,7 +25,7 @@ export const initDb = async (params: any) => { if (globalThis.db === undefined) { globalThis.db = new PouchDB('dyomedea', { auto_compaction: false, - revs_limit: 10, + // revs_limit: 10, }); } const db = globalThis.db;