Merge branch 'feature'

This commit is contained in:
Christian Beutel
2024-04-03 23:42:41 +02:00
16 changed files with 707 additions and 46 deletions

View File

@@ -0,0 +1,53 @@
import { Comment } from "$lib/models/comment";
import { pb } from "$lib/pocketbase";
import { ClientResponseError } from "pocketbase";
import { writable, type Writable } from "svelte/store";
export const comments: Writable<Comment[]> = writable([])
export async function comments_create(comment: Comment) {
if (!pb.authStore.model) {
throw new Error("Unauthenticated");
}
comment.author = pb.authStore.model!.id;
let r = await fetch('/api/v1/comment', {
method: 'PUT',
body: JSON.stringify(comment),
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
}
const model: Comment = await r.json();
return model;
}
export async function comments_update(comment: Comment) {
let r = await fetch('/api/v1/comment/' + comment.id, {
method: 'POST',
body: JSON.stringify(comment),
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
}
const model: Comment = await r.json();
return model;
}
export async function comments_delete(comment: Comment) {
const r = await fetch('/api/v1/comment/' + comment.id, {
method: 'DELETE',
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
}
}

View File

@@ -170,7 +170,7 @@ export async function trails_search_bounding_box(northEast: LatLng, southWest: L
export async function trails_show(id: string, loadGPX?: boolean, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
const r = await f(`/api/v1/trail/${id}?` + new URLSearchParams({
expand: "category,waypoints,summit_logs",
expand: "category,waypoints,summit_logs,comments_via_trail.author",
}), {
method: 'GET',
})
@@ -251,7 +251,7 @@ export async function trails_create(trail: Trail, photos: File[], gpx: File | Bl
}
}
export async function trails_update(oldTrail: Trail, newTrail: Trail, photos: File[], gpx: File | Blob | null) {
export async function trails_update(oldTrail: Trail, newTrail: Trail, photos?: File[], gpx?: File | Blob | null) {
const waypointUpdates = compareObjectArrays<Waypoint>(oldTrail.expand.waypoints ?? [], newTrail.expand.waypoints ?? []);
@@ -306,10 +306,13 @@ export async function trails_update(oldTrail: Trail, newTrail: Trail, photos: Fi
formData.append("gpx", gpx);
}
for (const photo of photos) {
formData.append("photos", photo)
if (photos) {
for (const photo of photos) {
formData.append("photos", photo)
}
}
const deletedPhotos = oldTrail.photos.filter(oldPhoto => !newTrail.photos.find(newPhoto => newPhoto === oldPhoto));
for (const deletedPhoto of deletedPhotos) {