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
29
packages/shared/src/items.ts
Normal file
29
packages/shared/src/items.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Services and their durations — pure helpers shared by the admin editor,
|
||||
// the Shop block and the checkout. Which duration costs what is decided
|
||||
// (and charged) by the shop hooks in PocketBase; this only reads and shows it.
|
||||
|
||||
import type { ItemDuration } from './types';
|
||||
|
||||
/** A service's `durations` field (JSON, possibly hand-edited) → valid entries, shortest first, one per length. */
|
||||
export function parseDurations(raw: unknown): ItemDuration[] {
|
||||
const list = Array.isArray(raw) ? raw : [];
|
||||
const seen = new Set<number>();
|
||||
return list
|
||||
.map((d) => ({ minutes: Math.floor(Number(d?.minutes)), price: Number(d?.price) }))
|
||||
.filter((d) => d.minutes > 0 && Number.isFinite(d.price) && d.price >= 0 && !seen.has(d.minutes) && seen.add(d.minutes))
|
||||
.sort((a, b) => a.minutes - b.minutes);
|
||||
}
|
||||
|
||||
/** `45` → "45 min", `60` → "1 hour", `120` → "2 hours", `90` → "1 h 30 min". */
|
||||
export function formatDuration(minutes: number) {
|
||||
if (minutes < 60) return `${minutes} min`;
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const rest = minutes % 60;
|
||||
if (!rest) return `${hours} ${hours === 1 ? 'hour' : 'hours'}`;
|
||||
return `${hours} h ${rest} min`;
|
||||
}
|
||||
|
||||
/** The lowest price among a service's durations (what "from …" shows), or null without any. */
|
||||
export function lowestPrice(durations: ItemDuration[]) {
|
||||
return durations.length ? Math.min(...durations.map((d) => d.price)) : null;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue