Lumen & Gesso / website / app / lib / draft-order.ts
| 1 | 'use client'; |
| 2 | |
| 3 | // In-memory draft order — the customer's in-flight commission as they |
| 4 | // move between /builder and /checkout. Lives at module scope so the |
| 5 | // state survives client-side navigation between routes (both pages |
| 6 | // import from this module → same singleton). |
| 7 | // |
| 8 | // What's stored: every field needed to (re)POST /api/orders, including |
| 9 | // the actual photo File objects. We intentionally do NOT serialize the |
| 10 | // Files to sessionStorage — base64-encoding multi-megabyte photos is |
| 11 | // slow and storage is bounded. Refresh on /checkout therefore loses the |
| 12 | // in-memory state; the page bounces back to /builder in that case. |
| 13 | // |
| 14 | // Builder writes here on every relevant state change (debounced through |
| 15 | // React state-flush). /checkout reads on mount. |
| 16 | |
| 17 | import type { ProductId, Orientation } from './pricing'; |
| 18 | |
| 19 | export type DraftPet = { |
| 20 | /** Stable slot index (0..3). */ |
| 21 | slot: number; |
| 22 | file: File | null; |
| 23 | name: string; |
| 24 | }; |
| 25 | |
| 26 | export type DraftConfig = { |
| 27 | styleId: string; |
| 28 | productId: ProductId; |
| 29 | size: string; |
| 30 | /** Portrait (catalog default) or landscape — landscape transposes the |
| 31 | * size's aspect for generation, crop, preview, and the Prodigi asset. */ |
| 32 | orientation: Orientation; |
| 33 | frame: string; |
| 34 | matteId: string; |
| 35 | /** Edge wrap for the frameless stretched canvas (Image / Mirror / |
| 36 | * White / Black). Ignored by framed products. */ |
| 37 | wrap: string; |
| 38 | bgColorId: string; |
| 39 | bgCustom: string; |
| 40 | fontId: string; |
| 41 | includeName: boolean; |
| 42 | commissionName: string | null; |
| 43 | /** Free-text note from the customer to the studio artist — captured |
| 44 | * alongside a custom style upload ("make her ears perkier", "match |
| 45 | * the mood of the reference but lighter", etc.). Empty string when |
| 46 | * not provided. Stored in the order config; surfaced in the admin |
| 47 | * editor. */ |
| 48 | artistNote: string; |
| 49 | }; |
| 50 | |
| 51 | export type Draft = { |
| 52 | config: DraftConfig; |
| 53 | pets: DraftPet[]; |
| 54 | customStyle: File | null; |
| 55 | }; |
| 56 | |
| 57 | let current: Draft | null = null; |
| 58 | const subscribers = new Set<() => void>(); |
| 59 | |
| 60 | export function getDraft(): Draft | null { |
| 61 | return current; |
| 62 | } |
| 63 | |
| 64 | export function setDraft(draft: Draft | null): void { |
| 65 | current = draft; |
| 66 | subscribers.forEach((fn) => fn()); |
| 67 | } |
| 68 | |
| 69 | export function subscribe(fn: () => void): () => void { |
| 70 | subscribers.add(fn); |
| 71 | return () => subscribers.delete(fn); |
| 72 | } |
| 73 | |
| 74 | /** Build a FormData payload identical to what the old inline submit |
| 75 | * used. Centralised so /checkout doesn't duplicate the field naming |
| 76 | * rules from the existing /api/orders route. */ |
| 77 | export function draftToFormData(draft: Draft, email: string): FormData { |
| 78 | const fd = new FormData(); |
| 79 | fd.append('email', email.trim()); |
| 80 | fd.append('config', JSON.stringify(draft.config)); |
| 81 | draft.pets.forEach((pet, i) => { |
| 82 | if (pet.file) fd.append(`pet_${i}_file`, pet.file); |
| 83 | if (pet.name) fd.append(`pet_${i}_name`, pet.name); |
| 84 | }); |
| 85 | if (draft.customStyle) fd.append('custom_style_file', draft.customStyle); |
| 86 | return fd; |
| 87 | } |
| 88 | |
| 89 | /** How many pets have an uploaded photo. Mirrors the `uploaded` count |
| 90 | * the Builder uses to gate Submit. */ |
| 91 | export function uploadedCount(draft: Draft | null): number { |
| 92 | return draft?.pets.filter((p) => p.file).length ?? 0; |
| 93 | } |