Shop: show the bundle's separate price as a saving; hide unsellable bundles
Bundle cards show what the parts cost one by one, crossed out when it's more than the bundle price. Bundles with a part that isn't on sale are left out of the shop data, so their parts always have names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
This commit is contained in:
parent
338aef9e01
commit
23c5b70a93
5 changed files with 32 additions and 5 deletions
|
|
@ -3,7 +3,7 @@
|
|||
import type { BasketLine, BookableTime, ShopBlockData, ItemRecord } from '$shared/types';
|
||||
import { getBasketContext } from '$shared/basket.svelte';
|
||||
import { DEFAULT_CURRENCY, DEFAULT_TIMEZONE, addMonths, createCalendarMonth, formatInstant, formatPrice, startOfMonth, zonedDateKey, zonedInputToIso } from '$shared/calendar';
|
||||
import { formatDuration, lowestPrice } from '$shared/items';
|
||||
import { bundleListPrice, formatDuration, lowestPrice } from '$shared/items';
|
||||
import { addDaysToKey, formatDateKey } from '$shared/slots';
|
||||
|
||||
let { data = {}, items = [] } = $props<{
|
||||
|
|
@ -342,8 +342,13 @@
|
|||
<span class="text-xs text-charcoal/60">{formatDuration(item.durations[0].minutes)} – {formatDuration(item.durations[item.durations.length - 1].minutes)}</span>
|
||||
</div>
|
||||
{:else}
|
||||
{@const listPrice = item.type === 'bundle' ? bundleListPrice(item.bundleParts, items) : null}
|
||||
<div class="flex flex-wrap items-center gap-x-3 gap-y-1">
|
||||
<span class="text-lg font-semibold text-sage-dark">{formatPrice(duration?.price ?? lowestPrice(item.durations) ?? item.price, currency)}</span>
|
||||
{#if listPrice != null && listPrice > item.price}
|
||||
<!-- What the parts cost one by one: the bundle is a saving. -->
|
||||
<s class="text-sm text-charcoal/50"><span class="sr-only">Bought separately: </span>{formatPrice(listPrice, currency)}</s>
|
||||
{/if}
|
||||
{#if duration}
|
||||
<span class="text-xs text-charcoal/60">{formatDuration(duration.minutes)}</span>
|
||||
{:else if !isService && item.stock != null && item.stock > 0 && item.stock <= 5}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
// 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';
|
||||
import type { BundlePart, ItemDuration, ItemRecord } from './types';
|
||||
|
||||
/** A service's `durations` field (JSON, possibly hand-edited) → valid entries, shortest first, one per length. */
|
||||
export function parseDurations(raw: unknown): ItemDuration[] {
|
||||
|
|
@ -32,3 +32,20 @@ export function lowestPrice(durations: ItemDuration[]) {
|
|||
export function formatCode(code: string) {
|
||||
return code.length === 8 ? `${code.slice(0, 4)}-${code.slice(4)}` : code;
|
||||
}
|
||||
|
||||
/**
|
||||
* What a bundle's parts cost bought one by one (each service at the part's
|
||||
* length), to show next to the bundle price — or null when a part isn't on
|
||||
* sale (not in `items`, or a length the service no longer has).
|
||||
*/
|
||||
export function bundleListPrice(parts: BundlePart[], items: ItemRecord[]) {
|
||||
let total = 0;
|
||||
for (const part of parts) {
|
||||
const item = items.find((candidate) => candidate.id === part.item);
|
||||
if (!item || item.type === 'bundle') return null;
|
||||
const unit = item.type === 'item_with_time' ? item.durations.find((d) => d.minutes === part.minutes)?.price : item.price;
|
||||
if (unit == null) return null;
|
||||
total += unit * part.count;
|
||||
}
|
||||
return Math.round(total * 100) / 100;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import type PocketBase from 'pocketbase';
|
||||
import { getFileUrl } from '../pb';
|
||||
import { parseDurations } from '../items';
|
||||
import { bundleListPrice, parseDurations } from '../items';
|
||||
import type { BookableTime, BundlePart, ItemRecord, ItemType } from '../types';
|
||||
|
||||
/** A bundle's `bundleParts` field (JSON) → valid parts. */
|
||||
|
|
@ -42,7 +42,10 @@ export function mapItemRecord(record: Record<string, any>): ItemRecord {
|
|||
*/
|
||||
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) };
|
||||
const items = records.map(mapItemRecord);
|
||||
// A bundle with a part that isn't on sale (inactive, deleted, or a length
|
||||
// the service dropped) can't be bought — the shop hook refuses it too.
|
||||
return { items: items.filter((item) => item.type !== 'bundle' || bundleListPrice(item.bundleParts, items) !== null) };
|
||||
}
|
||||
|
||||
export type BookableTimesQuery = { item: string; minutes: string; from: string; to: string };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue