fix lost gpx on summit log update (#702)
* fix lost gpx on summit log update * fix missing trail in summit log modal
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
import { SummitLogCreateSchema } from "$lib/models/api/summit_log_schema";
|
||||
import GPX from "$lib/models/gpx/gpx";
|
||||
import { summitLog } from "$lib/stores/summit_log_store";
|
||||
import { fetchGPX } from "$lib/stores/trail_store";
|
||||
import { cloneDeep } from "$lib/util/deep_util";
|
||||
import { validator } from "@felte/validator-zod";
|
||||
import { createForm } from "felte";
|
||||
@@ -45,10 +46,6 @@
|
||||
initialValues: $summitLog,
|
||||
extend: validator({ schema: ClientSummitLogCreateSchema }),
|
||||
onSubmit: async (form) => {
|
||||
if (!form.expand?.gpx_data) {
|
||||
form.gpx = "";
|
||||
}
|
||||
|
||||
if (
|
||||
!form._photos?.length &&
|
||||
!form.photos?.length &&
|
||||
@@ -69,8 +66,6 @@
|
||||
},
|
||||
});
|
||||
|
||||
let trailData = $derived($data.expand?.gpx_data);
|
||||
|
||||
$effect(() => {
|
||||
setFields(cloneDeep($summitLog));
|
||||
});
|
||||
@@ -81,12 +76,42 @@
|
||||
}
|
||||
});
|
||||
|
||||
let gpxLoading = $state(false);
|
||||
|
||||
async function ensureGpxDataLoaded() {
|
||||
if (gpxLoading || !$data.id || !$data.gpx || $data.expand?.gpx_data) {
|
||||
return;
|
||||
}
|
||||
gpxLoading = true;
|
||||
try {
|
||||
const gpxData = await fetchGPX($data as any, fetch);
|
||||
if (!gpxData) {
|
||||
return;
|
||||
}
|
||||
if (!$data.expand) {
|
||||
$data.expand = {};
|
||||
}
|
||||
$data.expand.gpx_data = gpxData;
|
||||
} finally {
|
||||
gpxLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
void ensureGpxDataLoaded();
|
||||
});
|
||||
|
||||
async function handleTrailSelection(trailData: string | null) {
|
||||
if (!trailData) {
|
||||
$data.duration = undefined;
|
||||
$data.elevation_gain = undefined;
|
||||
$data.elevation_loss = undefined;
|
||||
$data.distance = undefined;
|
||||
$data.gpx = "";
|
||||
if ($data.expand) {
|
||||
$data.expand.gpx_data = undefined;
|
||||
}
|
||||
$data._gpx = null;
|
||||
return;
|
||||
}
|
||||
const gpxObject = GPX.parse(trailData);
|
||||
@@ -141,7 +166,8 @@
|
||||
{#if $data.expand}
|
||||
<TrailPicker
|
||||
bind:trailFile={$data._gpx}
|
||||
{trailData}
|
||||
bind:trailData={$data.expand.gpx_data}
|
||||
hasTrail={Boolean($data.gpx)}
|
||||
label={$_("trail", { values: { n: 1 } })}
|
||||
onchange={(trail) => handleTrailSelection(trail)}
|
||||
></TrailPicker>
|
||||
|
||||
Reference in New Issue
Block a user