GpxDialog (working).

This commit is contained in:
Eric van der Vlist 2022-12-12 20:57:25 +01:00
parent 6da10713a0
commit daed8e5dc1
5 changed files with 29 additions and 11 deletions

View File

@ -17,7 +17,7 @@ import Dialog from '../dialog';
//import GpxesIcon from '../../icons/adventure-journey-location-svgrepo-com.svg'; //import GpxesIcon from '../../icons/adventure-journey-location-svgrepo-com.svg';
import style from './GpxDialog.module.css'; import style from './GpxDialog.module.css';
export const [currentGpxId, setCurrentGpxId] = createSignal<string>(''); export const [currentGpxId, setCurrentGpxId] = createSignal<string>('new');
const GpxDialog: Component<{}> = (props) => { const GpxDialog: Component<{}> = (props) => {
const [t] = useI18n(); const [t] = useI18n();
@ -30,10 +30,13 @@ const GpxDialog: Component<{}> = (props) => {
const handleClickOpen = async () => { const handleClickOpen = async () => {
setOpen(true); setOpen(true);
const newAllGpxes = await dispatch({ const newAllGpxes = (await dispatch({
action: 'getAllGpxesWithSummary', action: 'getAllGpxesWithSummary',
}); })) as any[];
setAllGpxes(newAllGpxes as any[]); setAllGpxes([
{ id: 'new', name: t('newGpx'), creator: 'dyomedea' },
...newAllGpxes,
]);
setSelectedGpxId(currentGpxId()); setSelectedGpxId(currentGpxId());
if (currentGpxId() !== '') { if (currentGpxId() !== '') {
const newGpx = await dispatch({ const newGpx = await dispatch({
@ -111,14 +114,14 @@ const GpxDialog: Component<{}> = (props) => {
}; };
const saveHandler = async (event: any) => { const saveHandler = async (event: any) => {
setCurrentGpxId(selectedGpxId()); const id = (await dispatch({
await dispatch({
action: 'putGpx', action: 'putGpx',
params: { params: {
id: selectedGpxId(), id: selectedGpxId(),
gpx: gpx(), gpx: gpx(),
}, },
}); })) as string;
setCurrentGpxId(id);
setOpen(false); setOpen(false);
}; };

View File

@ -43,7 +43,7 @@ const changeHandler = async (change: any) => {
if (parentId === 'gpx') { if (parentId === 'gpx') {
const gpxes = await getAllGpxes(); const gpxes = await getAllGpxes();
console.log({ caller: 'changeHandler / gpxes', gpxes, id }); console.log({ caller: 'changeHandler / gpxes', gpxes, id });
if (!(id in gpxes)) { if (!gpxes.includes(id)) {
await put( await put(
parentId, parentId,
`idx-${parentId}`, `idx-${parentId}`,

View File

@ -1,10 +1,11 @@
import { cloneDeep } from 'lodash';
import { PureComponent } from 'react'; import { PureComponent } from 'react';
import { Point, Rectangle } from '../components/map/types'; import { Point, Rectangle } from '../components/map/types';
import { lat2tile, lon2tile, rectanglesIntersect } from '../lib/geo'; import { lat2tile, lon2tile, rectanglesIntersect } from '../lib/geo';
import getUri, { intToGpxId, intToTrkptId } from '../lib/ids'; import getUri, { intToGpxId, intToTrkptId } from '../lib/ids';
import { get, getDocsByType, getFamily, put, putAll } from './lib'; import { get, getDocsByType, getFamily, put, putAll } from './lib';
const emptyGpx: Gpx = { export const emptyGpx: Gpx = {
$: { $: {
version: '1.1', version: '1.1',
creator: 'dyomedea version 0.000002', creator: 'dyomedea version 0.000002',
@ -276,6 +277,13 @@ export const getFullGpx = async (params: any) => {
export const getGpx = async (params: any) => { export const getGpx = async (params: any) => {
const { id } = params; const { id } = params;
if (id === 'new') {
const newGpx = cloneDeep(emptyGpx);
newGpx.metadata!.time = new Date().toISOString();
return newGpx;
}
const docs = await getFamily(id, { include_docs: true }); const docs = await getFamily(id, { include_docs: true });
let target: any[]; let target: any[];
let gpx: Gpx | undefined = undefined; let gpx: Gpx | undefined = undefined;
@ -306,7 +314,13 @@ export const getGpx = async (params: any) => {
}; };
export const putGpx = async (params: any) => { export const putGpx = async (params: any) => {
const { id, gpx } = params; let { id, gpx } = params;
if (id === 'new') {
id = getUri('gpx', {
gpx: intToGpxId(new Date(gpx.metadata.time).valueOf()),
});
}
const extensions = gpx?.extensions; const extensions = gpx?.extensions;

View File

@ -40,7 +40,7 @@ const dict = {
gpxDesc: 'Description', gpxDesc: 'Description',
gpxTime: 'Start date', gpxTime: 'Start date',
gpxSave: 'Save', gpxSave: 'Save',
newGpx: '-- new journey --',
}; };
export default dict; export default dict;

View File

@ -44,6 +44,7 @@ const dict = {
gpxDesc: 'Description', gpxDesc: 'Description',
gpxTime: 'Date de début', gpxTime: 'Date de début',
gpxSave: 'Sauvegarder', gpxSave: 'Sauvegarder',
newGpx: '-- nouveau voyage --',
}; };
export default dict; export default dict;