adds settings collection

This commit is contained in:
Christian Beutel
2024-04-13 20:51:05 +02:00
parent f9f8998617
commit feb2214b1b
33 changed files with 1321 additions and 81 deletions

View File

@@ -0,0 +1,55 @@
import { invalidateAll } from "$app/navigation";
import { Settings } from "$lib/models/settings";
import { ClientResponseError } from "pocketbase";
export async function settings_show(userId: string, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
const r = await f('/api/v1/settings/' + userId, {
method: 'GET',
})
const response = await r.json();
if (r.ok) {
return response;
} else {
throw new ClientResponseError(response)
}
}
export async function settings_create(settings: Settings) {
const r = await fetch('/api/v1/settings', {
method: 'PUT',
body: JSON.stringify(settings),
})
if (r.ok) {
return await r.json();
} else {
throw new ClientResponseError(await r.json())
}
}
export async function settings_update(settings: Settings) {
const r = await fetch('/api/v1/settings/' + settings.id, {
method: 'POST',
body: JSON.stringify(settings),
})
if (r.ok) {
await invalidateAll()
return await r.json();
} else {
throw new ClientResponseError(await r.json())
}
}
export async function settings_delete(settings: Settings) {
const r = await fetch('/api/v1/settings/' + settings.id, {
method: 'DELETE',
})
if (r.ok) {
return await r.json();
} else {
throw new ClientResponseError(await r.json())
}
}

View File

@@ -370,6 +370,18 @@ export async function trails_get_filter_values(f: (url: RequestInfo | URL, confi
}
}
export async function trails_get_bounding_box(f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch): Promise<TrailFilterValues> {
const r = await f('/api/v1/trail/bounding-box', {
method: 'GET',
})
if (r.ok) {
return await r.json();
} else {
throw new ClientResponseError(await r.json())
}
}
export async function fetchGPX(trail: Trail, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
if (!trail.gpx) {
return "";

View File

@@ -1,33 +1,26 @@
import { Settings } from "$lib/models/settings";
import type { User } from "$lib/models/user";
import { pb } from "$lib/pocketbase";
import { ClientResponseError, type AuthMethodsList } from "pocketbase";
import { writable, type Writable } from "svelte/store";
export type User = {
id: string,
username?: string,
email?: string,
password: string,
avatar?: string;
unit?: "metric" | "imperial";
language?: "en" | "de" | "fr" | "hu" | "nl" | "pl" | "pt" | "zh" ;
location?: { name: string, lat: number, lon: number }
}
import { settings_create } from "./settings_store";
export const currentUser: Writable<User | null> = writable<User | null>()
export async function users_create(user: User) {
user.unit = "metric";
const r = await fetch('/api/v1/user', {
let r = await fetch('/api/v1/user', {
method: 'PUT',
body: JSON.stringify({ ...user, passwordConfirm: user.password })
})
if (r.ok) {
return await r.json();
} else {
if (!r.ok) {
throw new ClientResponseError(await r.json())
}
const createdUser: User = await r.json();
const settings = new Settings("metric", "en", "trails", createdUser.id!)
await settings_create(settings);
}
export async function users_auth_methods(f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch): Promise<AuthMethodsList> {