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:
agent 2026-10-25 20:12:46 +00:00
parent 23df2ceced
commit eac99b2abe
18 changed files with 479 additions and 210 deletions

View file

@ -53,7 +53,10 @@ export function postJson<T>(path: string, data: unknown): Promise<T> {
type BasketPayload = { record: Record<string, any>; lines: Record<string, any>[] } | null;
/** Map the API's raw basket records to the `Basket` type used by the UI. */
/** PocketBase's "2026-01-01 10:00:00.000Z" → ISO. */
const pbInstant = (value: string) => new Date(String(value).replace(' ', 'T')).toISOString();
/** Map the API's raw basket records (plus `unitPrice`/`end` per line) to the `Basket` type used by the UI. */
export function mapBasket(payload: BasketPayload): Basket | null {
if (!payload) return null;
const { record, lines } = payload;
@ -68,13 +71,15 @@ export function mapBasket(payload: BasketPayload): Basket | null {
slot: line.slot || null,
quantity: line.quantity,
lineType: line.lineType,
start: line.start ? pbInstant(line.start) : undefined,
end: line.end || undefined,
minutes: line.minutes || undefined,
itemName: item?.name,
itemPrice: item?.price,
itemPrice: line.unitPrice ?? null,
itemImageUrl: media ? getFileUrl('media', media.id, media.file) : undefined,
slotStart: slot?.start,
slotEnd: slot?.end,
slotKind: slot?.kind,
slotCapacity: slot?.capacity,
heldUntil: line.heldUntil ? new Date(String(line.heldUntil).replace(' ', 'T')).toISOString() : undefined
heldUntil: line.heldUntil ? pbInstant(line.heldUntil) : undefined
};
});
return {
@ -116,7 +121,8 @@ export function mapOrder(order: Record<string, any>, lines: Record<string, any>[
lineType: l.lineType,
name: l.name,
price: l.price,
start: l.start || null
start: l.start || null,
minutes: l.minutes || null
}))
};
}

View file

@ -1,5 +1,5 @@
import { defaultColors, defaultFooter, defaultHeader } from '$lib/types';
import type { ItemRecord, SlotRecord } from '$lib/types';
import type { ItemRecord } from '$lib/types';
import { BASKET_COOKIE, getBasket } from '$lib/server/shop';
import { loadShopData } from '$shared/server/shop';
import { DEFAULT_TIMEZONE, shopCurrency } from '$shared/calendar';
@ -17,8 +17,8 @@ async function getGlobal(pb: App.Locals['pb'], key: string, fallback: unknown) {
export const load: LayoutServerLoad = async ({ locals, cookies, url }) => {
// Reading the path makes this load run again on every navigation, so the
// slots and basket are fresh on each page — places free up or get taken
// (holds lapse, other customers book) while the visitor browses.
// basket is fresh on each page (holds lapse while the visitor browses).
// Shop blocks fetch bookable times themselves (GET /api/slots).
void url.pathname;
const [header, footer, colors] = await Promise.all([
@ -30,8 +30,8 @@ export const load: LayoutServerLoad = async ({ locals, cookies, url }) => {
if (!header.homePage) header.homePage = defaultHeader.homePage;
// Commerce data for the public site. The listing is public data (API
// rules allow active items / available slots); the basket comes from the
// shop API in PocketBase. Failures leave the shop empty, never break a page.
// rules allow active items); the basket comes from the shop API in
// PocketBase. Failures leave the shop empty, never break a page.
const [shop, basket] = await Promise.all([
loadShopData(locals.pb).catch((e) => {
console.warn('Shop data unavailable (shop blocks will be empty):', e instanceof Error ? e.message : e);
@ -40,12 +40,12 @@ export const load: LayoutServerLoad = async ({ locals, cookies, url }) => {
getBasket(cookies.get(BASKET_COOKIE)).catch(() => null)
]);
const items: ItemRecord[] = shop?.items ?? [];
const slots: SlotRecord[] = shop?.slots ?? [];
// Booking times are shown in the studio's timezone, wherever the visitor is.
const timezone = env.SITE_TIMEZONE || DEFAULT_TIMEZONE;
// Prices are shown in the currency Stripe charges in.
const currency = shopCurrency(env.STRIPE_CURRENCY);
return { header, footer, colors, user: locals.user, basket, items, slots, timezone, currency };
return { header, footer, colors, user: locals.user, basket, items, timezone, currency };
};

View file

@ -8,4 +8,4 @@
<title>{data.page.title}</title>
</svelte:head>
<BlockRenderer blocks={data.page.blocks || []} calendarEvents={data.calendarEvents || []} items={data.items || []} slots={data.slots || []} />
<BlockRenderer blocks={data.page.blocks || []} calendarEvents={data.calendarEvents || []} items={data.items || []} />

View file

@ -7,4 +7,4 @@
<title>{data.page.title}</title>
</svelte:head>
<BlockRenderer blocks={data.page.blocks || []} calendarEvents={data.calendarEvents || []} items={data.items || []} slots={data.slots || []} />
<BlockRenderer blocks={data.page.blocks || []} calendarEvents={data.calendarEvents || []} items={data.items || []} />

View file

@ -21,6 +21,9 @@ export const POST: RequestHandler = jsonErrors(async ({ request, cookies }) => {
action: body.action,
itemId: body.itemId,
slotId: body.slotId,
minutes: body.minutes,
start: body.start,
seats: body.seats,
lineId: body.lineId,
quantity: body.quantity,
basketId: cookies.get(BASKET_COOKIE) || ''

View file

@ -0,0 +1,6 @@
import { bookableTimesResponse } from '$shared/server/shop';
import type { RequestHandler } from './$types';
// Bookable times for the Shop block's picker (one service and duration).
// Public data from the shop hook in PocketBase — this only forwards it.
export const GET: RequestHandler = ({ locals, url }) => bookableTimesResponse(locals.pb, url);