adds zod API validation

This commit is contained in:
Christian Beutel
2024-12-27 01:56:35 +01:00
parent 1f81779a64
commit 75c94a3ce4
80 changed files with 847 additions and 559 deletions

View File

@@ -8,7 +8,7 @@ import { notifications_index } from '$lib/stores/notification_store';
export const load: LayoutServerLoad = async ({ locals, url, fetch }) => {
let notifications
if (locals.user) {
if (locals.user?.id) {
notifications = await notifications_index({ recipient: locals.user.id }, 1, 10, fetch);
}
return { settings: locals.settings as Settings, notifications, origin: env.ORIGIN }

View File

@@ -1,18 +1,13 @@
import type { Activity } from "$lib/models/activity";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, list } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const filter = event.url.searchParams.get("filter") ?? "";
try {
const r = await pb.collection('activities')
.getList<Activity>(parseInt(page), parseInt(perPage), { filter: filter ?? "" })
const r = await list<Activity>(event, Collection.activities);
return json(r)
} catch (e: any) {
throw error(e.status, e);
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,17 +1,16 @@
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";
import { z } from "zod";
export async function POST(event: RequestEvent) {
try {
let data;
try {
data = await event.request.json()
} catch (e) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid json" } })
}
const r = await pb.collection('users').confirmPasswordReset(data.token, data.password, data.passwordConfirm);
const data = await event.request.json()
const safeData = z.object({
token: z.string(),
password: z.string(),
passwordConfirm: z.string()
}).refine(d => d.password === d.passwordConfirm).parse(data)
const r = await pb.collection('users').confirmPasswordReset(safeData.token, safeData.password, safeData.passwordConfirm);
return json(r);
} catch (e: any) {
throw error(e.status, e);

View File

@@ -1,17 +1,18 @@
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";
import { z } from "zod";
export async function POST(event: RequestEvent) {
try {
let data;
try {
data = await event.request.json()
} catch (e) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid json" } })
}
const data = await event.request.json();
const safeData = z.object({
email: z.string().email().optional(),
username: z.string().optional(),
password: z.string()
}).refine(d => d.email !== undefined || d.username !== undefined).parse(data);
const r = await pb.collection('users').authWithPassword(data.email ?? data.username!, data.password);
const r = await pb.collection('users').authWithPassword(safeData.email ?? safeData.username!, data.password);
return json(r);
} catch (e: any) {
throw error(e.status, e);

View File

@@ -3,6 +3,7 @@ import { env as private_env } from "$env/dynamic/private";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { z } from "zod";
const redirectURL = private_env.ORIGIN + "/login/redirect"
@@ -23,12 +24,18 @@ export async function GET(event: RequestEvent) {
}
export async function POST(event: RequestEvent) {
const data = await event.request.json();
try {
const data = await event.request.json();
const safeData = z.object({
name: z.string(),
code: z.string(),
codeVerifier: z.string()
}).parse(data)
const r = await pb.collection('users').authWithOAuth2Code(
data.name,
data.code,
data.codeVerifier,
safeData.name,
safeData.code,
safeData.codeVerifier,
redirectURL,
)
return json(r)

View File

@@ -1,20 +1,20 @@
import { pb } from "$lib/pocketbase";
import { handleError } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";
import { z } from "zod";
export async function POST(event: RequestEvent) {
try {
let data;
try {
data = await event.request.json()
} catch (e) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid json" } })
}
const data = await event.request.json()
const safeData = z.object({
email: z.string().email()
}).parse(data)
const r = await pb.collection('users').requestPasswordReset(data.email);
const r = await pb.collection('users').requestPasswordReset(safeData.email);
return json(r);
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}

View File

@@ -1,13 +1,13 @@
import type { Category } from "$lib/models/category";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, list } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r: Category[] = await pb.collection('categories').getFullList<Category>()
const r = await list<Category>(event, Collection.categories);
return json(r)
} catch (e: any) {
throw error(e.status, e);
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,37 +1,30 @@
import { CommentCreateSchema } from '$lib/models/api/comment_schema';
import type { Comment } from '$lib/models/comment';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
import { Collection, create, handleError, list } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const filter = event.url.searchParams.get('filter') ?? ""
try {
const r: Comment[] = await pb.collection('comments').getFullList<Comment>({
expand: "author",
filter: filter,
sort: "-created",
})
for (const comment of r) {
if(!comment.expand?.author) {
const r = await list<Comment>(event, Collection.comments);
for (const comment of r.items) {
if (!comment.expand?.author) {
comment.expand = {
author: await pb.collection('users_anonymous').getOne(comment.author)
}
}
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
} catch (e) {
throw handleError(e)
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('comments').create<Comment>(data)
const r = await create<Comment>(event, CommentCreateSchema, Collection.comments)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,33 +1,35 @@
import { CommentUpdateSchema } from "$lib/models/api/comment_schema";
import type { Comment } from "$lib/models/comment";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('comments')
.getOne<Comment>(event.params.id as string, { expand: "author" })
const r = await show<Comment>(event, Collection.comments)
r.expand = {
author: await pb.collection('users_anonymous').getOne(r.author)
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e)
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection('comments').update<Comment>(event.params.id as string, data)
const r = await update<Comment>(event, CommentUpdateSchema, Collection.comments)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('comments').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.comments)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,22 +1,32 @@
import { pb } from "$lib/pocketbase";
import { error, type RequestEvent } from "@sveltejs/kit";
import { z } from "zod";
export async function GET(event: RequestEvent) {
const safeParams = z.object({
collection: z.string().length(15),
record: z.string().length(15),
file: z.string()
}).safeParse(event.params)
if (!safeParams.success) {
throw error(400, 'Invalid params')
}
const parts = [];
parts.push("api");
parts.push("files");
parts.push(encodeURIComponent(event.params.collection as string));
parts.push(encodeURIComponent(event.params.record as string));
parts.push(encodeURIComponent(event.params.file as string));
parts.push(encodeURIComponent(safeParams.data.collection));
parts.push(encodeURIComponent(safeParams.data.record));
parts.push(encodeURIComponent(safeParams.data.file));
let fileURL = pb.buildUrl(parts.join("/"));
try {
const r = await event.fetch(fileURL)
return new Response(r.body, {headers: r.headers});
return new Response(r.body, { headers: r.headers });
} catch (e: any) {
console.log(e);
throw error(500, e);
}

View File

@@ -1,45 +1,31 @@
import { FollowCreateSchema } from '$lib/models/api/follow_schema';
import type { Follow } from '$lib/models/follow';
import type { UserAnonymous } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
import { Collection, create, handleError, list } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const sort = event.url.searchParams.get('sort') ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
try {
let r;
if (parseInt(perPage) < 0) {
r = {
items: await pb.collection('follows')
.getFullList<Follow>({ sort: sort, filter: filter, requestKey: filter })
}
} else {
r = await pb.collection('follows')
.getList<Follow>(parseInt(page), parseInt(perPage), { sort: sort ?? "", filter: filter ?? "", requestKey: filter })
}
const r = await list<Follow>(event, Collection.follows);
for (const follow of r.items) {
const follower = await pb.collection('users_anonymous').getOne<UserAnonymous>(follow.follower, { requestKey: filter })
const followee = await pb.collection('users_anonymous').getOne<UserAnonymous>(follow.followee, { requestKey: filter })
const follower = await pb.collection('users_anonymous').getOne<UserAnonymous>(follow.follower, { requestKey: null })
const followee = await pb.collection('users_anonymous').getOne<UserAnonymous>(follow.followee, { requestKey: null })
follow.expand = {
follower, followee
}
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
} catch (e) {
throw handleError(e)
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('follows').create<Follow>(data)
const r = await create<Follow>(event, FollowCreateSchema, Collection.follows)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,33 +1,13 @@
import type { Follow } from "$lib/models/follow";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, remove } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('follows')
.getOne<Follow>(event.params.id as string)
return json(r)
} catch (e: any) {
throw error(e.status, e);
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection('follows').update<Follow>(event.params.id as string, data)
return json(r);
} catch (e: any) {
throw error(e.status, e)
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('follows').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.follows)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,12 +1,11 @@
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, show } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('follow_counts')
.getOne(event.params.id as string)
const r = await show(event, Collection.follow_counts)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e)
}
}

View File

@@ -1,18 +1,15 @@
import { ListShareCreateSchema } from '$lib/models/api/list_share_schema';
import type { ListShare } from '$lib/models/list_share';
import type { User } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { Collection, create, handleError, list } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const sort = event.url.searchParams.get('sort') ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
try {
const r: ListShare[] = await pb.collection('list_share').getFullList<ListShare>({
sort: sort,
filter: filter
})
for (const share of r) {
const r = await list<ListShare>(event, Collection.list_share);
for (const share of r.items) {
const anonymous_user = await pb.collection('users_anonymous').getOne<User>(share.user)
share.expand = {
user: anonymous_user
@@ -20,17 +17,15 @@ export async function GET(event: RequestEvent) {
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('list_share').create<ListShare>(data)
const r = await create<ListShare>(event, ListShareCreateSchema, Collection.list_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,33 +1,31 @@
import { ListShareUpdateSchema } from "$lib/models/api/list_share_schema";
import type { ListShare } from "$lib/models/list_share";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('list_share')
.getOne<ListShare>(event.params.id as string)
const r = await show<ListShare>(event, Collection.list_share)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e)
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection('list_share').update<ListShare>(event.params.id as string, data)
const r = await update<ListShare>(event, ListShareUpdateSchema, Collection.list_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('list_share').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.list_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,27 +1,12 @@
import { ListCreateSchema } from '$lib/models/api/list_schema';
import type { List } from '$lib/models/list';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
import { Collection, create, handleError, list } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const sort = event.url.searchParams.get('sort') ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
const expand = "trails,trails.waypoints,trails.category,list_share_via_list";
try {
let r;
if (parseInt(perPage) < 0) {
r = {
items: await pb.collection('lists')
.getFullList<List>({ expand: expand, sort: sort, filter: filter })
}
} else {
r = await pb.collection('lists')
.getList<List>(parseInt(page), parseInt(perPage), { expand: expand, sort: sort ?? "", filter: filter ?? "" })
}
const r = await list<List>(event, Collection.lists);
for (const t of r.items) {
if (!t.author || !pb.authStore.model) {
continue;
@@ -31,20 +16,17 @@ export async function GET(event: RequestEvent) {
}
t.expand!.author = await pb.collection("users_anonymous").getOne(t.author);
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
} catch (e) {
throw handleError(e)
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('lists').create<List>(data)
const r = await create<List>(event, ListCreateSchema, Collection.lists)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,33 +1,36 @@
import { ListUpdateSchema } from "$lib/models/api/list_schema";
import type { List } from "$lib/models/list";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('lists')
.getOne<List>(event.params.id as string, { expand: "trails,trails.waypoints,trails.category,list_share_via_list" })
const r = await show<List>(event, Collection.lists)
r.expand = {
author: await pb.collection('users_anonymous').getOne(r.author!)
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e)
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection('lists').update<List>(event.params.id as string, data)
const r = await update<List>(event, ListUpdateSchema, Collection.lists)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('lists').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.lists)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,13 +1,13 @@
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, upload } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
import type { List } from "postcss/lib/list";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("lists").update<List>(event.params.id as string, data,);
const r = await upload<List>(event, Collection.lists);
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,28 +1,16 @@
import type { Notification } from '$lib/models/notification';
import type { UserAnonymous } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { Collection, list } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const sort = event.url.searchParams.get('sort') ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
try {
let r;
if (parseInt(perPage) < 0) {
r = {
items: await pb.collection('notifications')
.getFullList<Notification>({ sort: sort, filter: filter, requestKey: filter })
}
} else {
r = await pb.collection('notifications')
.getList<Notification>(parseInt(page), parseInt(perPage), { sort: sort ?? "", filter: filter ?? "", requestKey: filter })
}
const r = await list<Notification>(event, Collection.notifications);
for (const notification of r.items) {
const recipient = await pb.collection('users_anonymous').getOne<UserAnonymous>(notification.recipient, { requestKey: filter })
const author = await pb.collection('users_anonymous').getOne<UserAnonymous>(notification.author, { requestKey: filter })
const recipient = await pb.collection('users_anonymous').getOne<UserAnonymous>(notification.recipient, { requestKey: null })
const author = await pb.collection('users_anonymous').getOne<UserAnonymous>(notification.author, { requestKey: null })
notification.expand = {
recipient, author
}

View File

@@ -1,15 +1,13 @@
import type { Follow } from "$lib/models/follow";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { NotificationUpdateSchema } from "$lib/models/api/notification_schema";
import { Collection, handleError, update } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection('notifications').update<Notification>(event.params.id as string, { ...data, seen: true })
const r = await update<Comment>(event, NotificationUpdateSchema, Collection.notifications)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,12 +1,11 @@
import type { Settings } from '$lib/models/settings';
import { pb } from '$lib/pocketbase';
import { SettingsCreateSchema } from '$lib/models/api/settings_schema';
import { Collection, create } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('settings').create<Settings>(data)
const r = await create<Comment>(event, SettingsCreateSchema, Collection.settings)
return json(r);
} catch (e: any) {
throw error(e.status, e)

View File

@@ -1,32 +1,33 @@
import { SettingsCreateSchema } from '$lib/models/api/settings_schema';
import type { Settings } from "$lib/models/settings";
import { pb } from "$lib/pocketbase";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('settings').getFirstListItem<Settings>(`user="${event.params.id}"`)
const r = await show<Settings>(event, Collection.settings)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await await pb.collection("settings").update<Settings>(event.params.id as string, data);
const r = await update<Settings>(event, SettingsCreateSchema, Collection.settings)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('settings').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.settings)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}

View File

@@ -1,37 +1,32 @@
import { SummitLogCreateSchema } from '$lib/models/api/summit_log_schema';
import type { SummitLog } from '$lib/models/summit_log';
import { pb } from '$lib/pocketbase';
import { create, handleError } from '$lib/util/api_util';
import { Collection, list } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const filter = event.url.searchParams.get('filter') ?? ""
try {
const r: SummitLog[] = await pb.collection('summit_logs').getFullList<SummitLog>({
expand: "trails_via_summit_logs.category",
sort: "+date",
filter: filter
})
const r = await list<SummitLog>(event, Collection.summit_logs);
for (const t of r) {
for (const t of r.items) {
if (!t.author || !pb.authStore.model) {
continue;
}
if (!t.expand) {
t.expand = {} as any
}
t.expand.author = await pb.collection("users_anonymous").getOne(t.author);
t.expand!.author = await pb.collection("users_anonymous").getOne(t.author);
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('summit_logs').create<SummitLog>(data)
const r = await create<SummitLog>(event, SummitLogCreateSchema, Collection.summit_logs)
return json(r);
} catch (e: any) {
throw error(e.status, e)

View File

@@ -1,39 +1,39 @@
import { SummitLogUpdateSchema } from "$lib/models/api/summit_log_schema";
import type { SummitLog } from "$lib/models/summit_log";
import { pb } from "$lib/pocketbase";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('summit_logs')
.getOne<SummitLog>(event.params.id as string)
const r = await show<SummitLog>(event, Collection.summit_logs)
if (!r.expand) {
r.expand = {} as any
}
r.expand.author = await pb.collection("users_anonymous").getOne(r.author!);
r.expand!.author = await pb.collection("users_anonymous").getOne(r.author!);
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await await pb.collection("summit_logs").update<SummitLog>(event.params.id as string, data);
const r = await update<SummitLog>(event, SummitLogUpdateSchema, Collection.summit_logs)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('summit_logs').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.summit_logs)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}

View File

@@ -1,11 +1,10 @@
import type { SummitLog } from "$lib/models/summit_log";
import { pb } from "$lib/pocketbase";
import { Collection, upload } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("summit_logs").update<SummitLog>(event.params.id as string, data,);
const r = await upload<SummitLog>(event, Collection.summit_logs);
return json(r);
} catch (e: any) {
throw error(e.status, e)

View File

@@ -1,36 +1,30 @@
import { TrailShareCreateSchema } from '$lib/models/api/trail_share_schema';
import type { TrailShare } from '$lib/models/trail_share';
import type { User } from '$lib/models/user';
import type { UserAnonymous } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
import { Collection, handleError, list, create } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const sort = event.url.searchParams.get('sort') ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
try {
const r: TrailShare[] = await pb.collection('trail_share').getFullList<TrailShare>({
sort: sort,
filter: filter
})
for (const share of r) {
const anonymous_user = await pb.collection('users_anonymous').getOne<User>(share.user)
const r = await list<TrailShare>(event, Collection.trail_share);
for (const share of r.items) {
const anonymous_user = await pb.collection('users_anonymous').getOne<UserAnonymous>(share.user)
share.expand = {
user: anonymous_user
}
}
return json(r)
} catch (e: any) {
throw error(e.status, e);
handleError(e)
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('trail_share').create<TrailShare>(data)
const r = await create<TrailShare>(event, TrailShareCreateSchema, Collection.trail_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,33 +1,31 @@
import { TrailShareUpdateSchema } from "$lib/models/api/trail_share_schema";
import type { TrailShare } from "$lib/models/trail_share";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('trail_share')
.getOne<TrailShare>(event.params.id as string)
const r = await show<TrailShare>(event, Collection.trail_share)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e)
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection('trail_share').update<TrailShare>(event.params.id as string, data)
const r = await update<TrailShare>(event, TrailShareUpdateSchema, Collection.trail_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('trail_share').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.trail_share)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,49 +1,33 @@
import { TrailCreateSchema } from '$lib/models/api/trail_schema';
import type { Trail } from '$lib/models/trail';
import { pb } from '$lib/pocketbase';
import { Collection, create, handleError, list } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "21";
const expand = event.url.searchParams.get("expand") ?? ""
const sort = event.url.searchParams.get("sort") ?? ""
const filter = event.url.searchParams.get("filter") ?? "";
try {
let r;
if (parseInt(perPage) < 0) {
r = {
items: await pb.collection('trails')
.getFullList<Trail>({ expand: expand, sort: sort, filter: filter })
}
} else {
r = await pb.collection('trails')
.getList<Trail>(parseInt(page), parseInt(perPage), { expand: expand ?? "", sort: sort ?? "", filter: filter ?? "" })
}
const r = await list<Trail>(event, Collection.trails);
for (const t of r.items) {
if (!t.author || !pb.authStore.model) {
continue;
}
if(!t.expand) {
if (!t.expand) {
t.expand = {} as any
}
t.expand.author = await pb.collection("users_anonymous").getOne(t.author);
t.expand!.author = await pb.collection("users_anonymous").getOne(t.author);
}
return json(r)
} catch (e: any) {
console.error(e)
throw error(e.status, e);
throw handleError(e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('trails').create<Trail>(data)
const r = await create<Trail>(event, TrailCreateSchema, Collection.trails)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,20 +1,18 @@
import type { Trail } from "$lib/models/trail";
import { pb } from "$lib/pocketbase";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { TrailUpdateSchema } from '$lib/models/api/trail_schema';
export async function GET(event: RequestEvent) {
const expand = event.url.searchParams.get("expand") ?? ""
try {
const r = await pb.collection('trails')
.getOne<Trail>(event.params.id as string, { expand: expand ?? "" })
const r = await show<Trail>(event, Collection.trails)
if (pb.authStore.model) {
if (!r.expand) {
r.expand = {} as any
}
r.expand.author = await pb.collection("users_anonymous").getOne(r.author!);
r.expand!.author = await pb.collection("users_anonymous").getOne(r.author!);
}
// remove time from dates
@@ -25,33 +23,31 @@ export async function GET(event: RequestEvent) {
if (!log.expand) {
log.expand = {} as any
}
if(log.author) {
log.expand.author = await pb.collection("users_anonymous").getOne(log.author);
if (log.author) {
log.expand!.author = await pb.collection("users_anonymous").getOne(log.author);
}
}
return json(r)
} catch (e: any) {
console.error(e)
throw error(e.status || 500, e);
throw handleError(e)
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
const r = await pb.collection("trails").update<Trail>(event.params.id as string, data, { expand: "category,waypoints,summit_logs" });
const r = await update<Trail>(event, TrailUpdateSchema, Collection.trails)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('trails').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.trails)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,11 +1,11 @@
import type { Trail } from "$lib/models/trail";
import { pb } from "$lib/pocketbase";
import { Collection, upload } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("trails").update<Trail>(event.params.id as string, data,);
const r = await upload<Trail>(event, Collection.trails);
return json(r);
} catch (e: any) {
throw error(e.status, e)

View File

@@ -24,10 +24,10 @@ export async function PUT(event: RequestEvent) {
elevation_loss: trail.elevation_loss,
duration: trail.duration ? trail.duration * 60 : undefined,
})
log.expand.gpx_data = gpxData;
log.expand!.gpx_data = gpxData;
log._gpx = new File([gpxFile], (data.get("name") as string | null) ?? "file");
trail.expand.summit_logs.push(log);
trail.expand!.summit_logs.push(log);
} catch (e: any) {
throw new ClientResponseError({ status: 400, response: { message: "Invalid file" } })
}

View File

@@ -1,19 +1,20 @@
import { env } from '$env/dynamic/public';
import { UserCreateSchema } from '$lib/models/api/user_schema';
import type { User } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit'
import { Collection, create, handleError } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
import { ClientResponseError } from 'pocketbase';
export async function PUT(event: RequestEvent) {
const data = await event.request.json()
try {
if (env.PUBLIC_DISABLE_SIGNUP === "true") {
throw new ClientResponseError({ status: 401, response: { messgage: "Forbidden" } })
}
const r = await pb.collection('users').create<User>(data)
const r = await create<User>(event, UserCreateSchema, Collection.users)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}

View File

@@ -1,41 +1,49 @@
import { RecordIdSchema } from '$lib/models/api/base_schema';
import { UserUpdateSchema } from '$lib/models/api/user_schema';
import type { User } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { Collection, handleError, remove, show } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit'
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('users')
.getOne<User>(event.params.id as string)
const r = await show<User>(event, Collection.users)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}
export async function POST(event: RequestEvent) {
const data = await event.request.json()
try {
if (data.email != pb.authStore.model!.email) {
const r = await pb.collection('users').requestEmailChange(data.email);
pb.authStore.model!.email = data.email;
const params = event.params
const safeParams = RecordIdSchema.parse(params);
const safeData = UserUpdateSchema.parse(data);
if (safeData.email && safeData.email != pb.authStore.model!.email) {
const r = await pb.collection('users').requestEmailChange(safeData.email);
pb.authStore.model!.email = safeData.email;
}
const r = await pb.collection('users').update<User>(event.params.id as string, data)
if (data.password) {
const r = await pb.collection('users').authWithPassword(data.email ?? data.username!, data.password);
const r = await pb.collection('users').update<User>(safeParams.id, safeData)
if (safeData.password) {
const r = await pb.collection('users').authWithPassword(safeData.email ?? safeData.username!, safeData.password);
return json(r.record)
} else {
return json(r);
}
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('users').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.users)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}

View File

@@ -1,11 +1,10 @@
import { pb } from "$lib/pocketbase";
import type { User } from "$lib/stores/user_store";
import type { User } from "$lib/models/user";
import { Collection, upload } from "$lib/util/api_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("users").update<User>(event.params.id as string, data,);
const r = await upload<User>(event, Collection.users);
return json(r);
} catch (e: any) {
throw error(e.status, e)

View File

@@ -1,25 +1,24 @@
import type { User } from '$lib/models/user';
import type { UserAnonymous } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
import { Collection, handleError, list } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
const page = event.url.searchParams.get("page") ?? "0";
const perPage = event.url.searchParams.get("per-page") ?? "10";
const sort = event.url.searchParams.get("sort") ?? ""
let filter = event.url.searchParams.get("filter") ?? "";
let filter = event.url.searchParams.get("filter");
if (!filter.length) {
if (!filter) {
filter = "private=false"
} else {
filter += "&&private=false"
}
event.url.searchParams.set("filter", filter);
try {
const r = await pb.collection('users_anonymous')
.getList<User>(parseInt(page), parseInt(perPage), { sort: sort ?? "", filter: filter ?? "" })
const r = await list<UserAnonymous>(event, Collection.users_anonymous);
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}

View File

@@ -1,13 +1,13 @@
import type { User } from '$lib/models/user';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit'
import type { UserAnonymous } from "$lib/models/user";
import { Collection, handleError, show } from '$lib/util/api_util';
import { error, json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('users_anonymous')
.getOne<User>(event.params.id as string)
const r = await show<UserAnonymous>(event, Collection.users_anonymous)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}

View File

@@ -1,23 +1,22 @@
import { WaypointCreateSchema } from '$lib/models/api/waypoint_schema';
import type { Waypoint } from '$lib/models/waypoint';
import { pb } from '$lib/pocketbase';
import { error, json, type RequestEvent } from '@sveltejs/kit';
import { Collection, create, handleError, list } from '$lib/util/api_util';
import { json, type RequestEvent } from '@sveltejs/kit';
export async function GET(event: RequestEvent) {
try {
const r: Waypoint[] = await pb.collection('waypoints').getFullList<Waypoint>()
const r = await list<Waypoint>(event, Collection.waypoints);
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e);
}
}
export async function PUT(event: RequestEvent) {
const data = await event.request.json();
try {
const r = await pb.collection('waypoints').create<Waypoint>(data)
const r = await create<Waypoint>(event, WaypointCreateSchema, Collection.waypoints)
return json(r);
} catch (e: any) {
throw error(e.status, e)
} catch (e) {
throw handleError(e)
}
}

View File

@@ -1,32 +1,31 @@
import { WaypointUpdateSchema } from "$lib/models/api/waypoint_schema";
import type { Waypoint } from "$lib/models/waypoint";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, remove, show, update } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent) {
try {
const r = await pb.collection('waypoints')
.getOne<Waypoint>(event.params.id as string)
const r = await show<Waypoint>(event, Collection.waypoints)
return json(r)
} catch (e: any) {
throw error(e.status, e);
throw handleError(e)
}
}
export async function POST(event: RequestEvent){
const data = await event.request.json()
export async function POST(event: RequestEvent) {
try {
const r = await await pb.collection("waypoints").update<Waypoint>(event.params.id as string, data);
const r = await update<Waypoint>(event, WaypointUpdateSchema, Collection.waypoints)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}
export async function DELETE(event: RequestEvent) {
try {
const r = await pb.collection('waypoints').delete(event.params.id as string)
return json({ 'acknowledged': r });
const r = await remove(event, Collection.waypoints)
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e)
}
}

View File

@@ -1,13 +1,12 @@
import type { Waypoint } from "$lib/models/waypoint";
import { pb } from "$lib/pocketbase";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { Collection, handleError, upload } from "$lib/util/api_util";
import { json, type RequestEvent } from "@sveltejs/kit";
export async function POST(event: RequestEvent) {
const data = await event.request.formData()
try {
const r = await pb.collection("waypoints").update<Waypoint>(event.params.id as string, data,);
const r = await upload<Waypoint>(event, Collection.waypoints);
return json(r);
} catch (e: any) {
throw error(e.status, e)
throw handleError(e);
}
}

View File

@@ -104,7 +104,10 @@
) {
for (const trail of item.expand.trails) {
const gpxData: string = await fetchGPX(trail);
trail.expand.gpx_data = gpxData;
if(!trail.expand) {
(trail as any).expand = {}
}
trail.expand!.gpx_data = gpxData;
}
}
selectedList = item;

View File

@@ -1,8 +1,4 @@
import type { Follow } from "$lib/models/follow";
import type { Settings } from "$lib/models/settings";
import type { UserAnonymous } from "$lib/models/user";
import { follows_a_b, follows_counts, follows_index } from "$lib/stores/follow_store";
import { settings_show } from "$lib/stores/settings_store";
import { follows_a_b, follows_counts } from "$lib/stores/follow_store";
import { users_show } from "$lib/stores/user_store";
import { error, type ServerLoad } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";

View File

@@ -103,7 +103,7 @@
if (!trail.expand) {
(trail as any).expand = {};
}
trail.expand.gpx_data = gpxData;
trail.expand!.gpx_data = gpxData;
const trailFolder = zip.folder(`${trail.name}`);
let fileData: string = await trail2gpx(trail);
if (exportSettings.fileFormat == "json") {

View File

@@ -23,7 +23,6 @@
import { categories } from "$lib/stores/category_store";
import {
lists_add_trail,
lists_index,
lists_remove_trail,
} from "$lib/stores/list_store";
import { summitLog } from "$lib/stores/summit_log_store";
@@ -33,7 +32,6 @@
trails_create,
trails_update,
} from "$lib/stores/trail_store";
import { currentUser } from "$lib/stores/user_store.js";
import {
anchors,
appendToRoute,
@@ -71,7 +69,7 @@
let map: M.Map;
let mapTrail: Trail[] = [];
$: gpxData = $form.expand?.gpx_data;
$: gpxData = $form.expand!?.gpx_data;
$: if (gpxData) {
updateTrailOnMap();
}
@@ -119,7 +117,9 @@
const { form, errors, handleChange, handleSubmit } = createForm<Trail>({
initialValues: {
...data.trail,
public: $page.data.settings?.privacy.trails === "public",
public: data.trail.id
? data.trail.public
: $page.data.settings?.privacy.trails === "public",
category:
data.trail.category ||
$page.data.settings?.category ||
@@ -160,8 +160,8 @@
(p) => !p.startsWith("data:image/svg+xml;base64"),
);
if ($form.expand.gpx_data && overwriteGPX) {
gpxFile = new Blob([$form.expand.gpx_data], {
if ($form.expand!.gpx_data && overwriteGPX) {
gpxFile = new Blob([$form.expand!.gpx_data], {
type: "text/xml",
});
}
@@ -219,8 +219,8 @@
clearAnchorMarker();
clearRoute();
if ($form.expand.gpx_data) {
const gpx = await GPX.parse($form.expand.gpx_data);
if ($form.expand!.gpx_data) {
const gpx = await GPX.parse($form.expand!.gpx_data);
if (!(gpx instanceof Error)) {
setRoute(gpx);
initRouteAnchors(gpx);
@@ -255,7 +255,7 @@
const parseResult = await gpx2trail(gpxData, selectedFile.name);
$form = parseResult.trail;
$form.id = prevId;
$form.expand.gpx_data = gpxData;
$form.expand!.gpx_data = gpxData;
$form.category = $page.data.settings.category || $categories[0].id;
$form.public = $page.data.settings?.privacy.trails === "public";
@@ -266,18 +266,18 @@
duration: $form.duration ? $form.duration * 60 : undefined,
});
log.expand.gpx_data = gpxData;
log.expand!.gpx_data = gpxData;
const blob = new Blob([gpxData], { type: selectedFile.type });
log._gpx = new File([blob], selectedFile.name, {
type: selectedFile.type,
});
$form.expand.summit_logs.push(log);
$form.expand!.summit_logs.push(log);
setRoute(parseResult.gpx);
initRouteAnchors(parseResult.gpx);
for (const waypoint of $form.expand.waypoints) {
for (const waypoint of $form.expand!.waypoints) {
saveWaypoint(waypoint);
}
} catch (e) {
@@ -307,10 +307,10 @@
}
function clearWaypoints() {
for (const waypoint of $form.expand.waypoints) {
for (const waypoint of $form.expand!.waypoints) {
waypoint.marker?.remove();
}
$form.expand.waypoints = [];
$form.expand!.waypoints = [];
$form.waypoints = [];
}
@@ -365,22 +365,25 @@
}
function deleteWaypoint(index: number) {
$form.expand.waypoints.splice(index, 1);
$form.expand!.waypoints.splice(index, 1);
$form.waypoints.splice(index, 1);
$form.expand.waypoints = $form.expand.waypoints;
$form.expand!.waypoints = $form.expand!.waypoints;
}
function saveWaypoint(savedWaypoint: Waypoint) {
let editedWaypointIndex = $form.expand.waypoints.findIndex(
let editedWaypointIndex = $form.expand!.waypoints.findIndex(
(s) => s.id == savedWaypoint.id,
);
if (editedWaypointIndex >= 0) {
$form.expand.waypoints[editedWaypointIndex].marker?.remove();
$form.expand.waypoints[editedWaypointIndex] = savedWaypoint;
$form.expand!.waypoints[editedWaypointIndex].marker?.remove();
$form.expand!.waypoints[editedWaypointIndex] = savedWaypoint;
} else {
savedWaypoint.id = cryptoRandomString({ length: 15 });
$form.expand.waypoints = [...$form.expand.waypoints, savedWaypoint];
$form.expand!.waypoints = [
...$form.expand!.waypoints,
savedWaypoint,
];
}
const marker = createMarkerFromWaypoint(savedWaypoint, moveMarker);
@@ -390,16 +393,16 @@
function moveMarker(marker: M.Marker, wpId?: string) {
const position = marker.getLngLat();
const editableWaypointIndex = $form.expand.waypoints.findIndex(
const editableWaypointIndex = $form.expand!.waypoints.findIndex(
(w) => w.id == wpId,
);
const editableWaypoint = $form.expand.waypoints[editableWaypointIndex];
const editableWaypoint = $form.expand!.waypoints[editableWaypointIndex];
if (!editableWaypoint) {
return;
}
editableWaypoint.lat = position.lat;
editableWaypoint.lon = position.lng;
$form.expand.waypoints = [...$form.expand.waypoints];
$form.expand!.waypoints = [...$form.expand!.waypoints];
}
function beforeSummitLogModalOpen() {
@@ -410,16 +413,16 @@
function saveSummitLog(e: CustomEvent<SummitLog>) {
const savedSummitLog = e.detail;
let editedSummitLogIndex = $form.expand.summit_logs.findIndex(
let editedSummitLogIndex = $form.expand!.summit_logs.findIndex(
(s) => s.id == savedSummitLog.id,
);
if (editedSummitLogIndex >= 0) {
$form.expand.summit_logs[editedSummitLogIndex] = savedSummitLog;
$form.expand!.summit_logs[editedSummitLogIndex] = savedSummitLog;
} else {
savedSummitLog.id = cryptoRandomString({ length: 15 });
$form.expand.summit_logs = [
...$form.expand.summit_logs,
$form.expand!.summit_logs = [
...$form.expand!.summit_logs,
savedSummitLog,
];
}
@@ -434,9 +437,9 @@
summitLog.set(currentSummitLog);
openSummitLogModal();
} else if (e.detail.value === "delete") {
$form.expand.summit_logs.splice(index, 1);
$form.expand!.summit_logs.splice(index, 1);
$form.summit_logs.splice(index, 1);
$form.expand.summit_logs = $form.expand.summit_logs;
$form.expand!.summit_logs = $form.expand!.summit_logs;
}
}
@@ -446,11 +449,15 @@
}
try {
if (list.trails?.includes($form.id!)) {
await lists_remove_trail(list, $form);
list = await lists_remove_trail(list, $form);
} else {
await lists_add_trail(list, $form);
list = await lists_add_trail(list, $form);
}
await lists_index({ q: "", author: $currentUser?.id ?? "" }, 1, -1);
const index = data.lists.items.findIndex((l) => l.id == list.id);
if (index >= 0) {
data.lists.items[index] = list;
}
// await lists_index({ q: "", author: $currentUser?.id ?? "" }, 1, -1);
} catch (e) {
console.error(e);
show_toast({
@@ -612,7 +619,7 @@
$form.duration = totals.duration;
$form.elevation_gain = totals.elevationGain;
$form.elevation_loss = totals.elevationLoss;
$form.expand.gpx_data = route.toString();
$form.expand!.gpx_data = route.toString();
}
function updateTrailOnMap() {
@@ -794,7 +801,7 @@
{$_("waypoints", { values: { n: 2 } })}
</h3>
<ul>
{#each $form.expand.waypoints ?? [] as waypoint, i}
{#each $form.expand?.waypoints ?? [] as waypoint, i}
<li on:mouseenter={() => openMarkerPopup(waypoint)}>
<WaypointCard
{waypoint}
@@ -823,7 +830,7 @@
<hr class="border-separator" />
<h3 class="text-xl font-semibold">{$_("summit-book")}</h3>
<ul>
{#each $form.expand.summit_logs ?? [] as log, i}
{#each $form.expand?.summit_logs ?? [] as log, i}
<li>
<SummitLogCard
{log}