2022-12-12 10:41:09 +00:00
|
|
|
import { useI18n } from '@solid-primitives/i18n';
|
2022-12-15 17:46:58 +00:00
|
|
|
import { Box, Button, IconButton, SvgIcon, TextField } from '@suid/material';
|
2023-02-12 17:14:14 +00:00
|
|
|
import { cloneDeep, isPlainObject } from 'lodash';
|
2022-12-13 07:24:50 +00:00
|
|
|
import { Component, createSignal, Show } from 'solid-js';
|
2023-01-07 14:14:44 +00:00
|
|
|
import { newEmptyGpx } from '../../db/gpx';
|
|
|
|
import { peekCachedSignal } from '../../workers/cached-signals';
|
2022-12-12 10:41:09 +00:00
|
|
|
import dispatch from '../../workers/dispatcher-main';
|
|
|
|
import Dialog from '../dialog';
|
2022-12-13 06:59:28 +00:00
|
|
|
import GpxChooser from '../gpx-chooser';
|
2023-01-03 11:07:23 +00:00
|
|
|
import GpxIcon from '../gpx/GpxIcon';
|
2022-12-12 10:41:09 +00:00
|
|
|
//import GpxesIcon from '../../icons/adventure-journey-location-svgrepo-com.svg';
|
|
|
|
import style from './GpxDialog.module.css';
|
|
|
|
|
2022-12-12 19:57:25 +00:00
|
|
|
export const [currentGpxId, setCurrentGpxId] = createSignal<string>('new');
|
2022-12-12 14:37:12 +00:00
|
|
|
|
|
|
|
const GpxDialog: Component<{}> = (props) => {
|
2022-12-12 10:41:09 +00:00
|
|
|
const [t] = useI18n();
|
|
|
|
const [open, setOpen] = createSignal(false);
|
2022-12-13 06:59:28 +00:00
|
|
|
|
2022-12-12 14:37:12 +00:00
|
|
|
const [selectedGpxId, setSelectedGpxId] = createSignal<string>('');
|
|
|
|
const [gpx, setGpx] = createSignal<Gpx>();
|
2023-02-13 17:30:01 +00:00
|
|
|
const [currentAccount, setCurrentAccount] = createSignal<any>();
|
2022-12-12 14:37:12 +00:00
|
|
|
|
2023-01-07 14:14:44 +00:00
|
|
|
console.log({ caller: 'GpxDialog', currentGpxId: currentGpxId() });
|
|
|
|
|
|
|
|
const fetchGpx = (gpxId: string) => {
|
2023-02-13 19:57:30 +00:00
|
|
|
if (gpxId === 'new' || !gpxId) {
|
2023-01-07 14:14:44 +00:00
|
|
|
return newEmptyGpx();
|
|
|
|
}
|
|
|
|
const newGpx = peekCachedSignal({
|
|
|
|
id: gpxId,
|
|
|
|
action: 'getGpx',
|
|
|
|
});
|
|
|
|
return newGpx();
|
|
|
|
};
|
2022-12-12 10:41:09 +00:00
|
|
|
|
2023-02-13 17:30:01 +00:00
|
|
|
dispatch({ action: 'getCurrentAccount' }).then((currentAccount: any) => {
|
|
|
|
console.log({ caller: 'GpxDialog / currentAccount', currentAccount });
|
|
|
|
setCurrentAccount(currentAccount);
|
2023-04-15 16:25:27 +00:00
|
|
|
setCurrentGpxId(currentAccount.currentGpxId || 'new');
|
|
|
|
setSelectedGpxId(currentAccount.currentGpxId || 'new');
|
2023-02-13 17:30:01 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 10:41:09 +00:00
|
|
|
const handleClickOpen = async () => {
|
|
|
|
setOpen(true);
|
2023-02-13 19:57:30 +00:00
|
|
|
setGpx(fetchGpx(currentGpxId()) as Gpx);
|
2022-12-12 14:37:12 +00:00
|
|
|
console.log({
|
|
|
|
caller: 'GpxDialog / handleClickOpen',
|
|
|
|
currentGpxId: currentGpxId(),
|
|
|
|
gpx: gpx(),
|
|
|
|
});
|
2022-12-12 10:41:09 +00:00
|
|
|
};
|
2023-01-07 14:14:44 +00:00
|
|
|
|
2022-12-12 20:35:00 +00:00
|
|
|
const handleClose = (event: any, reason?: string) => {
|
|
|
|
console.log({
|
|
|
|
caller: 'GpxDialog / handleClose',
|
|
|
|
event,
|
|
|
|
reason,
|
|
|
|
});
|
|
|
|
if (reason === undefined) {
|
|
|
|
setOpen(false);
|
|
|
|
}
|
2022-12-12 10:41:09 +00:00
|
|
|
};
|
2022-12-13 06:59:28 +00:00
|
|
|
const gpxChangeHandler = async (gpxId: string) => {
|
|
|
|
setSelectedGpxId(gpxId);
|
2023-01-07 14:14:44 +00:00
|
|
|
setGpx(fetchGpx(gpxId) as Gpx);
|
2022-12-12 17:55:14 +00:00
|
|
|
console.log({
|
|
|
|
caller: 'GpxDialog / gpxChangeHandler',
|
2023-01-07 14:14:44 +00:00
|
|
|
gpxId,
|
2022-12-12 17:55:14 +00:00
|
|
|
gpx: gpx(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const gpxNameChangeHandler = (event: any) => {
|
|
|
|
const newGpx: Gpx = cloneDeep(gpx()) as Gpx;
|
|
|
|
if (newGpx.metadata === undefined) {
|
|
|
|
newGpx.metadata = {};
|
|
|
|
}
|
|
|
|
newGpx.metadata.name = event.target.value;
|
|
|
|
setGpx(newGpx);
|
|
|
|
console.log({
|
|
|
|
caller: 'GpxDialog / gpxNameChangeHandler',
|
|
|
|
value: event.target.value,
|
|
|
|
gpx: gpx(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const gpxDescChangeHandler = (event: any) => {
|
|
|
|
const newGpx: Gpx = cloneDeep(gpx()) as Gpx;
|
|
|
|
if (newGpx.metadata === undefined) {
|
|
|
|
newGpx.metadata = {};
|
|
|
|
}
|
|
|
|
newGpx.metadata.desc = event.target.value;
|
|
|
|
setGpx(newGpx);
|
|
|
|
console.log({
|
|
|
|
caller: 'GpxDialog / gpxDescChangeHandler',
|
|
|
|
value: event.target.value,
|
|
|
|
gpx: gpx(),
|
|
|
|
});
|
|
|
|
};
|
2023-03-04 12:09:11 +00:00
|
|
|
const gpxToChangeHandler = (event: any) => {
|
2023-02-12 17:14:14 +00:00
|
|
|
const newGpx: Gpx = cloneDeep(gpx()) as Gpx;
|
|
|
|
if (!isPlainObject(newGpx.extensions)) {
|
|
|
|
newGpx.extensions = {};
|
|
|
|
}
|
2023-03-04 12:09:11 +00:00
|
|
|
newGpx.extensions.to = event.target.value;
|
2023-02-12 17:14:14 +00:00
|
|
|
setGpx(newGpx);
|
|
|
|
console.log({
|
2023-03-04 12:09:11 +00:00
|
|
|
caller: 'GpxDialog / gpxToChangeHandler',
|
2023-02-12 17:14:14 +00:00
|
|
|
value: event.target.value,
|
|
|
|
gpx: gpx(),
|
|
|
|
newGpx,
|
|
|
|
});
|
|
|
|
};
|
2022-12-12 17:55:14 +00:00
|
|
|
const gpxTimeChangeHandler = (event: any) => {
|
|
|
|
const newGpx: Gpx = cloneDeep(gpx()) as Gpx;
|
|
|
|
if (newGpx.metadata === undefined) {
|
|
|
|
newGpx.metadata = {};
|
|
|
|
}
|
|
|
|
newGpx.metadata.time = new Date(event.target.value).toISOString();
|
|
|
|
setGpx(newGpx);
|
|
|
|
console.log({
|
|
|
|
caller: 'GpxDialog / gpxTimeChangeHandler',
|
|
|
|
value: event.target.value,
|
|
|
|
gpx: gpx(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const saveHandler = async (event: any) => {
|
2022-12-12 19:57:25 +00:00
|
|
|
const id = (await dispatch({
|
2022-12-12 17:55:14 +00:00
|
|
|
action: 'putGpx',
|
|
|
|
params: {
|
|
|
|
id: selectedGpxId(),
|
|
|
|
gpx: gpx(),
|
|
|
|
},
|
2022-12-12 19:57:25 +00:00
|
|
|
})) as string;
|
|
|
|
setCurrentGpxId(id);
|
2022-12-12 17:55:14 +00:00
|
|
|
setOpen(false);
|
2023-02-13 17:30:01 +00:00
|
|
|
await dispatch({
|
|
|
|
action: 'putCurrentAccount',
|
|
|
|
params: {
|
|
|
|
account: { ...currentAccount(), currentGpxId: currentGpxId() },
|
|
|
|
},
|
|
|
|
});
|
2022-12-12 14:37:12 +00:00
|
|
|
};
|
2022-12-12 10:41:09 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div class={style.control}>
|
|
|
|
<IconButton onClick={handleClickOpen}>
|
2023-01-03 11:07:23 +00:00
|
|
|
<GpxIcon />
|
2022-12-12 10:41:09 +00:00
|
|
|
</IconButton>
|
|
|
|
</div>
|
2022-12-15 17:46:58 +00:00
|
|
|
<Dialog
|
|
|
|
open={open()}
|
|
|
|
title={t('gpxDialog')}
|
|
|
|
closeHandler={handleClose}
|
|
|
|
fullScreen={true}
|
|
|
|
>
|
2022-12-12 10:41:09 +00:00
|
|
|
<Box
|
|
|
|
component='form'
|
|
|
|
sx={{
|
|
|
|
width: '100%',
|
2022-12-12 17:55:14 +00:00
|
|
|
'& .MuiTextField-root': { m: 1, width: '100%' },
|
2022-12-12 10:41:09 +00:00
|
|
|
paddingTop: '5px',
|
|
|
|
}}
|
2022-12-12 17:55:14 +00:00
|
|
|
noValidate
|
|
|
|
autoComplete='off'
|
2022-12-12 10:41:09 +00:00
|
|
|
>
|
2022-12-13 06:59:28 +00:00
|
|
|
<GpxChooser gpxId={selectedGpxId} setGpxId={gpxChangeHandler} />
|
|
|
|
|
2022-12-12 17:55:14 +00:00
|
|
|
<Show when={gpx() != undefined}>
|
|
|
|
<TextField
|
|
|
|
label={t('gpxName')}
|
|
|
|
value={gpx()?.metadata?.name || ''}
|
|
|
|
onChange={gpxNameChangeHandler}
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
label={t('gpxTime')}
|
|
|
|
type='datetime-local'
|
|
|
|
value={(gpx()?.metadata?.time || '').replace('Z', '')}
|
|
|
|
onChange={gpxTimeChangeHandler}
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
label={t('gpxDesc')}
|
|
|
|
multiline={true}
|
|
|
|
rows={5}
|
|
|
|
value={gpx()?.metadata?.desc || ''}
|
|
|
|
onChange={gpxDescChangeHandler}
|
|
|
|
InputProps={{ inputComponent: 'textarea' }}
|
|
|
|
/>
|
2023-02-12 17:14:14 +00:00
|
|
|
<TextField
|
2023-03-04 12:09:11 +00:00
|
|
|
label={t('gpxTo')}
|
2023-02-12 17:14:14 +00:00
|
|
|
multiline={true}
|
|
|
|
rows={5}
|
2023-03-04 12:09:11 +00:00
|
|
|
value={gpx()?.extensions?.to || ''}
|
|
|
|
onChange={gpxToChangeHandler}
|
2023-02-12 17:14:14 +00:00
|
|
|
InputProps={{ inputComponent: 'textarea' }}
|
|
|
|
/>
|
2022-12-12 17:55:14 +00:00
|
|
|
<Button variant='contained' onClick={saveHandler}>
|
|
|
|
{t('gpxSave')}
|
|
|
|
</Button>
|
|
|
|
</Show>
|
2022-12-12 10:41:09 +00:00
|
|
|
</Box>
|
|
|
|
</Dialog>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-12 14:37:12 +00:00
|
|
|
export default GpxDialog;
|