fixes trail multi save bug

This commit is contained in:
Christian Beutel
2025-03-14 17:36:11 +01:00
parent ababbabadc
commit e5a28d2c42
3 changed files with 21 additions and 13 deletions

View File

@@ -184,9 +184,11 @@ export async function trails_create(trail: Trail, photos: File[], gpx: File | Bl
trail.author = pb.authStore.model!.id
let r = await f('/api/v1/trail', {
let r = await f(`/api/v1/trail?` + new URLSearchParams({
expand: "category,waypoints,summit_logs,trail_share_via_trail",
}), {
method: 'PUT',
body: JSON.stringify({ ...trail, expand: undefined }),
body: JSON.stringify({ ...trail }),
})
if (!r.ok) {
@@ -216,7 +218,7 @@ export async function trails_create(trail: Trail, photos: File[], gpx: File | Bl
throw new APIError(r.status, response.message, response.detail)
}
return await r.json();
return model;
}

View File

@@ -28,8 +28,16 @@ export async function GET(event: RequestEvent) {
export async function PUT(event: RequestEvent) {
try {
const r = await create<Trail>(event, TrailCreateSchema, Collection.trails)
enrichRecord(r);
return json(r);
} catch (e) {
throw handleError(e)
}
}
function enrichRecord(r: Trail) {
r.date = r.date?.substring(0, 10) ?? "";
for (const log of r.expand?.summit_logs ?? []) {
log.date = log.date.substring(0, 10);
}
}

View File

@@ -127,7 +127,7 @@
let autoRouting = $state(true);
let listAddEnabled = $state(false);
let savedAtLeastOnce = $state(false);
const {
form,
@@ -182,13 +182,14 @@
?.trkpt?.at(0)?.$.lon;
}
if (page.params.id === "new") {
if (page.params.id === "new" && !savedAtLeastOnce) {
const createdTrail = await trails_create(
form as Trail,
photoFiles,
gpxFile,
);
form.id = createdTrail.id;
setFields(createdTrail);
trail.set(createdTrail);
} else {
const updatedTrail = await trails_update(
$trail,
@@ -199,7 +200,7 @@
setFields(updatedTrail);
}
listAddEnabled = true;
savedAtLeastOnce = true;
show_toast({
type: "success",
icon: "check",
@@ -447,18 +448,15 @@
let editedSummitLogIndex = $formData.expand!.summit_logs?.findIndex(
(s) => s.id == log.id,
);
if ((editedSummitLogIndex ?? -1) >= 0) {
if ((editedSummitLogIndex ?? -1) >= 0) {
$formData.expand!.summit_logs![editedSummitLogIndex!] = log;
} else {
} else {
log.id = cryptoRandomString({ length: 15 });
$formData.expand!.summit_logs = [
...($formData.expand!.summit_logs ?? []),
log,
];
}
console.log($formData.expand?.summit_logs);
}
function handleSummitLogMenuClick(
@@ -1093,7 +1091,7 @@
<Button
secondary={true}
tooltip={$_("save-your-trail-first")}
disabled={page.params.id == "new" && !listAddEnabled}
disabled={page.params.id == "new" && !savedAtLeastOnce}
type="button"
onclick={() => listSelectModal.openModal()}
><i class="fa fa-plus mr-2"></i>{$_("add-to-list")}</Button