adds list
This commit is contained in:
54
web/src/lib/stores/list_store.ts
Normal file
54
web/src/lib/stores/list_store.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { List, type ListFilter } from "$lib/models/list";
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import { getFileURL } from "$lib/util/file_util";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
|
||||
export const lists: Writable<List[]> = writable([])
|
||||
export const list: Writable<List> = writable(new List("", []))
|
||||
|
||||
|
||||
export async function lists_index(filter?: ListFilter) {
|
||||
const dbResponse: List[] = (await pb.collection('lists').getList<List>(1, 5, {
|
||||
expand: "trails",
|
||||
sort: `${filter?.sortOrder ?? "+"}${filter?.sort ?? "name"}`
|
||||
})).items
|
||||
|
||||
for (const list of dbResponse) {
|
||||
list.avatar = getFileURL(list, list.avatar);
|
||||
}
|
||||
|
||||
lists.set(dbResponse);
|
||||
|
||||
if(dbResponse.length > 0) {
|
||||
list.set(dbResponse[0])
|
||||
}
|
||||
|
||||
return dbResponse;
|
||||
}
|
||||
|
||||
export async function lists_create(formData: { [key: string]: any; } | FormData) {
|
||||
if (!pb.authStore.model) {
|
||||
throw new Error("Unauthenticated");
|
||||
}
|
||||
|
||||
formData.append("author", pb.authStore.model!.id);
|
||||
|
||||
let model = await pb
|
||||
.collection("lists")
|
||||
.create<List>(formData);
|
||||
}
|
||||
|
||||
export async function lists_update(list:List, formData: { [key: string]: any; } | FormData) {
|
||||
if((formData.get("avatar") as File).size == 0) {
|
||||
formData.delete("avatar");
|
||||
}
|
||||
let model = await pb
|
||||
.collection("lists")
|
||||
.update<List>(list.id! ,formData);
|
||||
}
|
||||
|
||||
export async function lists_delete(list:List) {
|
||||
let success = await pb
|
||||
.collection("lists")
|
||||
.delete(list.id!);
|
||||
}
|
||||
@@ -137,7 +137,6 @@ export async function trails_create(trail: Trail, formData: { [key: string]: any
|
||||
|
||||
if (!pb.authStore.model) {
|
||||
throw new Error("Unauthenticated");
|
||||
;
|
||||
}
|
||||
formData.set("category", trail.expand.category!.id);
|
||||
|
||||
@@ -302,7 +301,6 @@ async function fetchGPX(trail: Trail) {
|
||||
}
|
||||
|
||||
function setFileURLs(trail: Trail) {
|
||||
trail.thumbnail = getFileURL(trail, trail.thumbnail);
|
||||
for (let i = 0; i < trail.photos.length; i++) {
|
||||
const photo = trail.photos[i];
|
||||
trail.photos[i] = getFileURL(trail, photo)
|
||||
|
||||
Reference in New Issue
Block a user