Implement receipt management features including creation, loading, and display of receipts. Refactor related components and enhance UI with dialogs for adding new receipts. Update type definitions and improve error handling for better user experience.
This commit is contained in:
@@ -1,6 +1,55 @@
|
||||
import type { PageServerLoad } from "./$types";
|
||||
import { fail } from "@sveltejs/kit";
|
||||
import type { Actions, PageServerLoad } from "./$types";
|
||||
import type { StackqReceipt } from "$lib/types/receipts";
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
// Placeholder: no PocketBase receipts collection yet. Return empty list.
|
||||
return { receipts: [] as { id: string }[] };
|
||||
const COLLECTION = "Stackq_Receipts";
|
||||
|
||||
const STATUS_OPTIONS = ["New", "Data Entered", "Reviewed", "Consolidated"] as const;
|
||||
const TYPE_OPTIONS = ["Purchase", "Gas"] as const;
|
||||
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const list = await locals.pb
|
||||
.collection(COLLECTION)
|
||||
.getFullList<StackqReceipt>({
|
||||
sort: "-created",
|
||||
expand: "User",
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(COLLECTION, err);
|
||||
return [] as StackqReceipt[];
|
||||
});
|
||||
|
||||
return { receipts: list };
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
create: async ({ request, locals }) => {
|
||||
const user = locals.user;
|
||||
if (!user?.id) return fail(401, { error: "You must be logged in to create a receipt." });
|
||||
|
||||
const form = await request.formData();
|
||||
const Status = (form.get("Status") as string)?.trim() || "New";
|
||||
const Type = (form.get("Type") as string)?.trim() || "Purchase";
|
||||
const imageFile = form.get("Image") as File | null;
|
||||
const hasImage = imageFile && imageFile.size > 0;
|
||||
|
||||
const body: Record<string, unknown> = {
|
||||
Status: STATUS_OPTIONS.includes(Status as (typeof STATUS_OPTIONS)[number]) ? Status : "New",
|
||||
Type: TYPE_OPTIONS.includes(Type as (typeof TYPE_OPTIONS)[number]) ? Type : "Purchase",
|
||||
User: user.id,
|
||||
};
|
||||
if (hasImage) body.Image = imageFile;
|
||||
|
||||
try {
|
||||
await locals.pb.collection(COLLECTION).create(body);
|
||||
} catch (err: unknown) {
|
||||
const message =
|
||||
err && typeof err === "object" && "message" in err
|
||||
? String((err as { message: string }).message)
|
||||
: "Failed to create receipt.";
|
||||
return fail(500, { error: message, Status, Type });
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user