adds user management
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { pb } from "$lib/constants";
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import type { Category } from "$lib/models/category";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { pb } from "$lib/constants";
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import { SummitLog } from "$lib/models/summit_log";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
|
||||
@@ -12,9 +12,17 @@ export async function summit_logs_create(bodyParams?: { [key: string]: any; } |
|
||||
return model;
|
||||
}
|
||||
|
||||
export async function summit_logs_update(updatedSummitLog: SummitLog) {
|
||||
const model = await pb
|
||||
.collection("summit_logs")
|
||||
.update(updatedSummitLog.id!, updatedSummitLog);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
export async function summit_logs_delete(id: string) {
|
||||
const success = await pb
|
||||
.collection("summit_logs_delete")
|
||||
.collection("summit_logs")
|
||||
.delete(id);
|
||||
|
||||
return success;
|
||||
|
||||
@@ -7,4 +7,11 @@ export type Toast = {
|
||||
}
|
||||
|
||||
|
||||
export const toast: Writable<Toast> = writable();
|
||||
export const toast: Writable<Toast | null> = writable();
|
||||
|
||||
|
||||
export function show_toast(newToast: Toast, duration: number = 3000) {
|
||||
toast.set(newToast);
|
||||
|
||||
setTimeout(() => toast.set(null), duration);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { pb } from "$lib/constants";
|
||||
import { Trail } from "$lib/models/trail";
|
||||
import { getFileURL } from "$lib/util/file_util";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
import { waypoints_create, waypoints_delete } from "./waypoint_store";
|
||||
import { summit_logs_create, summit_logs_delete } from "./summit_log_store";
|
||||
import type { Waypoint } from "$lib/models/waypoint";
|
||||
import type { SummitLog } from "$lib/models/summit_log";
|
||||
import { Trail } from "$lib/models/trail";
|
||||
import type { Waypoint } from "$lib/models/waypoint";
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import { getFileURL } from "$lib/util/file_util";
|
||||
import { util } from "$lib/vendor/svelte-form-lib/util";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
import { summit_logs_create, summit_logs_delete, summit_logs_update } from "./summit_log_store";
|
||||
import { waypoints_create, waypoints_delete, waypoints_update } from "./waypoint_store";
|
||||
|
||||
export const trails: Writable<Trail[]> = writable([])
|
||||
export const trail: Writable<Trail> = writable(new Trail(""));
|
||||
@@ -25,7 +26,7 @@ export async function trails_index() {
|
||||
}
|
||||
|
||||
export async function trails_show(id: string, loadGPX?: boolean) {
|
||||
const response: Trail = await pb.collection('trails').getOne<Trail>(id, { expand: "category,waypoints,summit_logs" })
|
||||
const response: Trail = await pb.collection('trails').getOne<Trail>(id, { expand: "category,waypoints,summit_logs" })
|
||||
|
||||
if (loadGPX) {
|
||||
const gpxData: string = await fetchGPX(response);
|
||||
@@ -45,6 +46,11 @@ export async function trails_show(id: string, loadGPX?: boolean) {
|
||||
}
|
||||
|
||||
export async function trails_create(trail: Trail, formData: { [key: string]: any; } | FormData) {
|
||||
|
||||
if (!pb.authStore.model) {
|
||||
throw new Error("Unauthenticated");
|
||||
;
|
||||
}
|
||||
formData.set("category", trail.expand.category!.id);
|
||||
|
||||
for (const file of trail._photoFiles) {
|
||||
@@ -62,6 +68,13 @@ export async function trails_create(trail: Trail, formData: { [key: string]: any
|
||||
const model = await summit_logs_create(summitLog);
|
||||
formData.append("summit_logs", model.id!);
|
||||
}
|
||||
|
||||
if (!formData.get("public")) {
|
||||
formData.set("public", 0);
|
||||
}
|
||||
|
||||
formData.append("author", pb.authStore.model!.id);
|
||||
|
||||
let model = await pb
|
||||
.collection("trails")
|
||||
.create<Trail>(formData);
|
||||
@@ -93,6 +106,13 @@ export async function trails_update(oldTrail: Trail, newTrail: Trail, formData:
|
||||
formData.append("waypoints+", model.id!);
|
||||
}
|
||||
|
||||
for (const updatedWaypoint of waypointUpdates.updated) {
|
||||
const model = await waypoints_update({
|
||||
...updatedWaypoint,
|
||||
marker: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
for (const deletedWaypoint of waypointUpdates.deleted) {
|
||||
const success = await waypoints_delete(deletedWaypoint.id!);
|
||||
}
|
||||
@@ -104,6 +124,10 @@ export async function trails_update(oldTrail: Trail, newTrail: Trail, formData:
|
||||
formData.append("summit_logs+", model.id!);
|
||||
}
|
||||
|
||||
for (const updatedSummitLog of summitLogUpdates.updated) {
|
||||
const model = await summit_logs_update(updatedSummitLog);
|
||||
}
|
||||
|
||||
for (const deletedSummitLog of summitLogUpdates.deleted) {
|
||||
const success = await summit_logs_delete(deletedSummitLog.id!);
|
||||
}
|
||||
@@ -122,6 +146,10 @@ export async function trails_update(oldTrail: Trail, newTrail: Trail, formData:
|
||||
formData.delete("gpx");
|
||||
}
|
||||
|
||||
if (!formData.get("public")) {
|
||||
formData.set("public", 0);
|
||||
}
|
||||
|
||||
const thumbnailIndex = newTrail.photos.findIndex(
|
||||
(p) => p == newTrail.thumbnail,
|
||||
);
|
||||
@@ -137,7 +165,7 @@ export async function trails_update(oldTrail: Trail, newTrail: Trail, formData:
|
||||
|
||||
model = await pb
|
||||
.collection("trails")
|
||||
.update<Trail>(model.id!, { thumbnail: thumbnail });
|
||||
.update<Trail>(model.id!, { thumbnail: thumbnail }, { expand: "category,waypoints,summit_logs" });
|
||||
|
||||
trail.set(model);
|
||||
|
||||
@@ -145,10 +173,21 @@ export async function trails_update(oldTrail: Trail, newTrail: Trail, formData:
|
||||
}
|
||||
|
||||
|
||||
export async function trails_delete(id: string) {
|
||||
export async function trails_delete(trail: Trail) {
|
||||
if (trail.expand.waypoints) {
|
||||
for (const waypoint of trail.expand.waypoints) {
|
||||
waypoints_delete(waypoint.id!);
|
||||
}
|
||||
}
|
||||
if (trail.expand.summit_logs) {
|
||||
for (const summit_log of trail.expand.summit_logs) {
|
||||
summit_logs_delete(summit_log.id!);
|
||||
}
|
||||
}
|
||||
|
||||
const success = await pb
|
||||
.collection("trails")
|
||||
.delete(id);
|
||||
.delete(trail.id!);
|
||||
|
||||
return success;
|
||||
}
|
||||
@@ -173,12 +212,22 @@ function setFileURLs(trail: Trail) {
|
||||
}
|
||||
|
||||
function compareObjectArrays<T extends { id?: string }>(oldArray: T[], newArray: T[]) {
|
||||
const newObjects = newArray.filter(newObj => !oldArray.find(oldObj => oldObj.id === newObj.id));
|
||||
const newObjects = [];
|
||||
const updatedObjects = [];
|
||||
for (const newObj of newArray) {
|
||||
const oldObj = oldArray.find(oldObj => oldObj.id === newObj.id)
|
||||
if (!oldObj) {
|
||||
newObjects.push(newObj);
|
||||
} else if (!util.deepEqual(newObj, oldObj)) {
|
||||
updatedObjects.push(newObj);
|
||||
}
|
||||
}
|
||||
const deletedObjects = oldArray.filter(oldObj => !newArray.find(newObj => newObj.id === oldObj.id));
|
||||
|
||||
return {
|
||||
added: newObjects,
|
||||
deleted: deletedObjects
|
||||
deleted: deletedObjects,
|
||||
updated: updatedObjects
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
25
web/src/lib/stores/user_store.ts
Normal file
25
web/src/lib/stores/user_store.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import { type AuthModel } from 'pocketbase';
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
|
||||
export type User = {
|
||||
username?: string,
|
||||
email?: string,
|
||||
password: string,
|
||||
}
|
||||
|
||||
export const currentUser: Writable<AuthModel | null> = writable<AuthModel | null>()
|
||||
|
||||
export async function users_create(user: User) {
|
||||
const model = await pb.collection('users').create({ ...user, passwordConfirm: user.password });
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
export async function login(user: User) {
|
||||
const authData = await pb.collection('users').authWithPassword(user.email ?? user.username!, user.password);
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
pb.authStore.clear();
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { pb } from "$lib/constants";
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import { Waypoint } from "$lib/models/waypoint";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
|
||||
@@ -13,6 +13,14 @@ export async function waypoints_create(bodyParams?: { [key: string]: any; } | Fo
|
||||
return model;
|
||||
}
|
||||
|
||||
export async function waypoints_update(updatedWaypoint: Waypoint) {
|
||||
const model = await pb
|
||||
.collection("waypoints")
|
||||
.update(updatedWaypoint.id!, updatedWaypoint);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
export async function waypoints_delete(id: string) {
|
||||
const success = await pb
|
||||
.collection("waypoints")
|
||||
|
||||
Reference in New Issue
Block a user