Enhance receipt management by adding a Description field to receipts, improving user input validation and error handling in the server actions. Update UI components to support the new Description field, ensuring a more comprehensive receipt creation and editing experience.
This commit is contained in:
@@ -10,11 +10,16 @@ const COLLECTION = "Stackq_Receipts";
|
||||
const TYPE_OPTIONS = ["Purchase", "Gas"] as const;
|
||||
|
||||
export const load = async ({ locals }: Parameters<PageServerLoad>[0]) => {
|
||||
const user = locals.user as { id: string; Admin?: boolean } | null;
|
||||
if (!user?.id) return { receipts: [] as StackqReceipt[] };
|
||||
|
||||
const isAdmin = user.Admin === true;
|
||||
const list = await locals.pb
|
||||
.collection(COLLECTION)
|
||||
.getFullList<StackqReceipt>({
|
||||
sort: "-created",
|
||||
expand: "User",
|
||||
...(isAdmin ? {} : { filter: `User = "${user.id}"` }),
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(COLLECTION, err);
|
||||
@@ -31,11 +36,15 @@ export const actions = {
|
||||
|
||||
const form = await request.formData();
|
||||
const Type = (form.get("Type") as string)?.trim() || "Purchase";
|
||||
const Description = (form.get("Description") as string)?.trim() ?? "";
|
||||
if (!Description) {
|
||||
return fail(400, { error: "Description is required.", Type });
|
||||
}
|
||||
const imageFile = form.get("Image") as File | null;
|
||||
const hasImage = imageFile && imageFile.size > 0;
|
||||
if (hasImage) {
|
||||
const imgError = validateImageUpload(imageFile);
|
||||
if (imgError) return fail(400, { error: imgError, Type });
|
||||
if (imgError) return fail(400, { error: imgError, Type, Description });
|
||||
}
|
||||
|
||||
let imageToSave: File | null = hasImage ? imageFile : null;
|
||||
@@ -46,6 +55,7 @@ export const actions = {
|
||||
return fail(400, {
|
||||
error: "Could not convert HEIC image. Try saving as JPEG (iPhone: Settings → Camera → Formats) or use a different photo.",
|
||||
Type,
|
||||
Description,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -53,6 +63,7 @@ export const actions = {
|
||||
const body: Record<string, unknown> = {
|
||||
Status: "New",
|
||||
Type: TYPE_OPTIONS.includes(Type as (typeof TYPE_OPTIONS)[number]) ? Type : "Purchase",
|
||||
Description,
|
||||
User: user.id,
|
||||
};
|
||||
if (imageToSave) body.Image = imageToSave;
|
||||
@@ -64,7 +75,7 @@ export const actions = {
|
||||
err && typeof err === "object" && "message" in err
|
||||
? String((err as { message: string }).message)
|
||||
: "Failed to create receipt.";
|
||||
return fail(500, { error: message, Type });
|
||||
return fail(500, { error: message, Type, Description });
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
|
||||
Reference in New Issue
Block a user