dyomedea/src/components/gpx-import/GpxImport.tsx

85 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-11-26 21:04:08 +00:00
import { Component } from 'solid-js';
import CloudUploadIcon from '@suid/icons-material/CloudUpload';
2022-11-27 11:29:30 +00:00
// @ts-ignore
2022-11-26 21:04:08 +00:00
import GPX from '../../lib/gpx-parser-builder/src/gpx';
import css from './GpxImport.module.css';
import { findStartTime } from '../../lib/gpx';
import dispatch from '../../workers/dispatcher-main';
import { intToGpxId } from '../../lib/ids';
const GpxImport: Component = () => {
const onChangeHandler = (event: any) => {
2022-12-10 20:25:15 +00:00
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);
2022-11-26 21:04:08 +00:00
2022-12-10 20:25:15 +00:00
fileReader.addEventListener(
'load',
async () => {
// this will then display a text file
console.log({
caller: 'GpxImport / XML',
file,
result: fileReader.result,
2022-11-27 21:48:35 +00:00
});
2022-12-10 20:25:15 +00:00
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,
},
},
});
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
);
}
2022-11-26 21:04:08 +00:00
};
return (
<>
<div class={css.container}>
<label for='gpx-import' class={css.label}>
<CloudUploadIcon />
</label>
<input
type='file'
id='gpx-import'
class={css.inputFile}
accept='.gpx'
2022-12-10 20:25:15 +00:00
multiple={true}
2022-11-26 21:04:08 +00:00
onChange={onChangeHandler}
/>
</div>
</>
);
};
export default GpxImport;