Shop: duration slider on services, times fetched per service and length
Service cards step through their lengths with a slider and the price follows. The time picker loads bookable times for the chosen length and the months on screen from GET /api/slots (a proxy to the shop hook in each app), and reloads when the basket's bookings change. Checkout, emails and the basket show each booking's own length. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
This commit is contained in:
parent
23df2ceced
commit
eac99b2abe
18 changed files with 479 additions and 210 deletions
|
|
@ -148,14 +148,12 @@ function orderEmailContext(data: OrderEmailData, settings: EmailSettings) {
|
|||
const lineRecords = [...data.lines].sort((a, b) => String(a.start || '').localeCompare(String(b.start || '')));
|
||||
|
||||
const lines: EmailLine[] = lineRecords.map((line) => {
|
||||
// A booking ends after its own length (order lines from before
|
||||
// lengths were stored fall back to their slot's end).
|
||||
const start = line.start ? new Date(line.start).toISOString() : null;
|
||||
const minutes = Number(line.minutes) || 0;
|
||||
const slotEnd = line.expand?.slot?.end;
|
||||
const duration = Number(line.expand?.item?.duration) || 0;
|
||||
const end = slotEnd
|
||||
? new Date(slotEnd).toISOString()
|
||||
: start && duration
|
||||
? new Date(new Date(start).getTime() + duration * 60_000).toISOString()
|
||||
: null;
|
||||
const end = start && minutes ? new Date(new Date(start).getTime() + minutes * 60_000).toISOString() : slotEnd ? new Date(slotEnd).toISOString() : null;
|
||||
return {
|
||||
name: String(line.name || 'Item'),
|
||||
price: Number(line.price) || 0,
|
||||
|
|
|
|||
|
|
@ -1,39 +1,63 @@
|
|||
import type PocketBase from 'pocketbase';
|
||||
import { getFileUrl } from '../pb';
|
||||
import type { ItemRecord, SlotRecord } from '../types';
|
||||
import { parseDurations } from '../items';
|
||||
import type { BookableTime, ItemRecord } from '../types';
|
||||
|
||||
/** An `items` record (with `expand.image`) → `ItemRecord`. */
|
||||
export function mapItemRecord(record: Record<string, any>): ItemRecord {
|
||||
const media = record.expand?.image;
|
||||
const type = record.type === 'item_with_time' ? 'item_with_time' : 'item';
|
||||
return {
|
||||
id: record.id,
|
||||
name: String(record.name || ''),
|
||||
description: String(record.description || ''),
|
||||
price: Number(record.price) || 0,
|
||||
image: String(record.image || ''),
|
||||
imageUrl: media ? getFileUrl('media', media.id, media.file) : '',
|
||||
type,
|
||||
stock: record.stock ?? null,
|
||||
durations: type === 'item_with_time' ? parseDurations(record.durations) : [],
|
||||
bufferMinutes: record.bufferMinutes ?? null,
|
||||
capacity: record.capacity ?? null,
|
||||
active: record.active !== false
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Active items and bookable future slots, shaped for the Shop/Checkout
|
||||
* blocks. Used by the public layout and the admin draft preview so both
|
||||
* render the same shop.
|
||||
*
|
||||
* Works with an anonymous client: active items and media are public, and
|
||||
* slots come from the shop hook (GET /api/shop/slots), which leaves out
|
||||
* full slots and slots blocked by an overlapping booking. Errors propagate.
|
||||
* Active items, shaped for the Shop/Checkout blocks. Used by the public
|
||||
* layout and the admin draft preview so both render the same shop. Works
|
||||
* with an anonymous client (active items and media are public). Bookable
|
||||
* times are fetched by the Shop block itself, per service and duration
|
||||
* (see `loadBookableTimes`). Errors propagate.
|
||||
*/
|
||||
export async function loadShopData(pb: PocketBase): Promise<{ items: ItemRecord[]; slots: SlotRecord[] }> {
|
||||
const [itemRecords, { slots }] = await Promise.all([
|
||||
pb.collection('items').getFullList({ filter: 'active = true', sort: 'name', expand: 'image' }),
|
||||
pb.send<{ slots: SlotRecord[] }>('/api/shop/slots', { requestKey: null })
|
||||
]);
|
||||
|
||||
const items = itemRecords.map((record): ItemRecord => {
|
||||
const media = record.expand?.image;
|
||||
return {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
description: record.description || '',
|
||||
price: record.price,
|
||||
image: record.image || '',
|
||||
imageUrl: media ? getFileUrl('media', media.id, media.file) : '',
|
||||
type: record.type,
|
||||
stock: record.stock ?? null,
|
||||
duration: record.duration ?? null,
|
||||
bufferMinutes: record.bufferMinutes ?? null,
|
||||
capacity: record.capacity ?? null,
|
||||
active: record.active
|
||||
};
|
||||
});
|
||||
|
||||
return { items, slots };
|
||||
export async function loadShopData(pb: PocketBase): Promise<{ items: ItemRecord[] }> {
|
||||
const records = await pb.collection('items').getFullList({ filter: 'active = true', sort: 'name', expand: 'image' });
|
||||
return { items: records.map(mapItemRecord) };
|
||||
}
|
||||
|
||||
export type BookableTimesQuery = { item: string; minutes: string; from: string; to: string };
|
||||
|
||||
/**
|
||||
* Times a customer can book for one service and duration, from the shop
|
||||
* hook (GET /api/shop/slots), which applies every availability rule. When
|
||||
* none start in [from, to), `next` is the first one after (or null).
|
||||
*/
|
||||
export function loadBookableTimes(pb: PocketBase, query: BookableTimesQuery) {
|
||||
return pb.send<{ times: BookableTime[]; next: string | null }>('/api/shop/slots', { query, requestKey: null });
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/slots in both apps (the Shop block's time picker): forwards
|
||||
* ?item=&minutes=&from=&to= to the shop hook. A PocketBase error keeps its
|
||||
* status and message; PocketBase being down is a 503.
|
||||
*/
|
||||
export async function bookableTimesResponse(pb: PocketBase, url: URL): Promise<Response> {
|
||||
const param = (key: string) => url.searchParams.get(key) ?? '';
|
||||
try {
|
||||
return Response.json(await loadBookableTimes(pb, { item: param('item'), minutes: param('minutes'), from: param('from'), to: param('to') }));
|
||||
} catch (e) {
|
||||
const status = Number((e as { status?: number })?.status) || 0;
|
||||
const message = (e as { response?: { message?: string } })?.response?.message;
|
||||
return Response.json({ message: message || 'Could not load the available times — please try again' }, { status: status >= 400 ? status : 503 });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue