Checkout: reservation countdown, 'keep it 10 more minutes', and a message when a hold runs out
Basket changes that free or take places reload the page data, so places removed from the basket show up in the shop again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VaXE9U7AmW3dfD2w6cbLfk
This commit is contained in:
parent
28773f2599
commit
021d06d3e4
6 changed files with 187 additions and 28 deletions
|
|
@ -151,7 +151,7 @@ The shop/checkout blocks implement a basket-based booking flow: visitors add ser
|
||||||
|
|
||||||
**Slot availability** (`slotStates()` in `pocketbase/pb_hooks/shop/lib.js` — the one place these rules live):
|
**Slot availability** (`slotStates()` in `pocketbase/pb_hooks/shop/lib.js` — the one place these rules live):
|
||||||
|
|
||||||
- A slot has `capacity` places. Places are taken by paid order lines and by basket lines whose `heldUntil` hasn't passed (10 min after adding, 30 min once payment starts). A lapsed hold stops counting immediately; the cron only deletes the line.
|
- A slot has `capacity` places. Places are taken by paid order lines and by basket lines whose `heldUntil` hasn't passed (10 min after adding, 30 min once payment starts). A lapsed hold stops counting immediately; the cron only deletes the line. The checkout shows a countdown to the first hold running out, offers "Keep it 10 more minutes" (basket action `renew_holds`; holds are extended, never shortened) in the last 3 minutes, and says which times ran out when one does.
|
||||||
- One practitioner works every service, so a slot with **any** place taken blocks every other slot that overlaps it — whatever the service — counting each service's get-ready time (`items.bufferMinutes`) after it. The client can offer three massages at 10:00; once one is in a basket or booked, the other two disappear. A group session with one place booked blocks massages at that time too, while staying bookable itself until full.
|
- One practitioner works every service, so a slot with **any** place taken blocks every other slot that overlaps it — whatever the service — counting each service's get-ready time (`items.bufferMinutes`) after it. The client can offer three massages at 10:00; once one is in a basket or booked, the other two disappear. A group session with one place booked blocks massages at that time too, while staying bookable itself until full.
|
||||||
- `GET /api/shop/slots` lists the bookable future slots (public; feeds the Shop block). With `?detail=1` a signed-in admin gets every slot in a range with booked/held places and what blocks it (the `/slots` page).
|
- `GET /api/shop/slots` lists the bookable future slots (public; feeds the Shop block). With `?detail=1` a signed-in admin gets every slot in a range with booked/held places and what blocks it (the `/slots` page).
|
||||||
- Adding to a basket (`add_slot`, with `seats`) and changing a group line's places re-check capacity and blocking inside one transaction; PocketBase runs transactions one at a time, so concurrent customers can't both get the last place or two overlapping times.
|
- Adding to a basket (`add_slot`, with `seats`) and changing a group line's places re-check capacity and blocking inside one transaction; PocketBase runs transactions one at a time, so concurrent customers can't both get the last place or two overlapping times.
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,8 @@ export function mapBasket(payload: BasketPayload): Basket | null {
|
||||||
itemImageUrl: media ? getFileUrl('media', media.id, media.file) : undefined,
|
itemImageUrl: media ? getFileUrl('media', media.id, media.file) : undefined,
|
||||||
slotStart: slot?.start,
|
slotStart: slot?.start,
|
||||||
slotEnd: slot?.end,
|
slotEnd: slot?.end,
|
||||||
slotCapacity: slot?.capacity
|
slotCapacity: slot?.capacity,
|
||||||
|
heldUntil: line.heldUntil ? new Date(String(line.heldUntil).replace(' ', 'T')).toISOString() : undefined
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
|
|
@ -83,7 +84,8 @@ export function mapBasket(payload: BasketPayload): Basket | null {
|
||||||
email: record.email || null,
|
email: record.email || null,
|
||||||
customerDetails: record.customerDetails || null,
|
customerDetails: record.customerDetails || null,
|
||||||
stripePaymentIntentId: record.stripePaymentIntentId || null,
|
stripePaymentIntentId: record.stripePaymentIntentId || null,
|
||||||
lines: mappedLines
|
lines: mappedLines,
|
||||||
|
serverNow: Date.now()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { getContext, setContext } from 'svelte';
|
import { getContext, setContext } from 'svelte';
|
||||||
|
import { invalidateAll } from '$app/navigation';
|
||||||
import type { Basket, BasketLine } from './types';
|
import type { Basket, BasketLine } from './types';
|
||||||
|
|
||||||
const BASKET_KEY = 'basket';
|
const BASKET_KEY = 'basket';
|
||||||
|
|
@ -8,12 +9,20 @@ export type BasketStore = ReturnType<typeof createBasketStore>;
|
||||||
export function createBasketStore(initial: Basket | null) {
|
export function createBasketStore(initial: Basket | null) {
|
||||||
let lines = $state<BasketLine[]>(initial?.lines ?? []);
|
let lines = $state<BasketLine[]>(initial?.lines ?? []);
|
||||||
let status = $state<Basket['status']>(initial?.status ?? 'active');
|
let status = $state<Basket['status']>(initial?.status ?? 'active');
|
||||||
|
// Browser clock minus server clock, so hold countdowns survive a wrong clock.
|
||||||
|
let clockOffset = $state(clockOffsetOf(initial));
|
||||||
|
|
||||||
const total = $derived(
|
const total = $derived(
|
||||||
lines.reduce((sum, line) => sum + (line.itemPrice ?? 0) * line.quantity, 0)
|
lines.reduce((sum, line) => sum + (line.itemPrice ?? 0) * line.quantity, 0)
|
||||||
);
|
);
|
||||||
const count = $derived(lines.reduce((sum, line) => sum + line.quantity, 0));
|
const count = $derived(lines.reduce((sum, line) => sum + line.quantity, 0));
|
||||||
|
|
||||||
|
/** When the first slot reservation in the basket lapses, in browser-clock ms (null: none). */
|
||||||
|
const holdDeadline = $derived.by(() => {
|
||||||
|
const ends = lines.filter((l) => l.lineType === 'slot' && l.heldUntil).map((l) => Date.parse(l.heldUntil!));
|
||||||
|
return ends.length ? Math.min(...ends) + clockOffset : null;
|
||||||
|
});
|
||||||
|
|
||||||
async function callApi(body: Record<string, unknown>): Promise<Basket | null> {
|
async function callApi(body: Record<string, unknown>): Promise<Basket | null> {
|
||||||
const res = await fetch('/api/basket', {
|
const res = await fetch('/api/basket', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
@ -31,13 +40,27 @@ export function createBasketStore(initial: Basket | null) {
|
||||||
function applyBasket(basket: Basket | null) {
|
function applyBasket(basket: Basket | null) {
|
||||||
lines = basket?.lines ?? [];
|
lines = basket?.lines ?? [];
|
||||||
status = basket?.status ?? 'active';
|
status = basket?.status ?? 'active';
|
||||||
|
clockOffset = clockOffsetOf(basket);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply a basket change that can free or take slot places, then reload
|
||||||
|
* page data so shop listings (places left, blocked times) match it.
|
||||||
|
*/
|
||||||
|
async function applySlotChange(basket: Basket | null) {
|
||||||
|
applyBasket(basket);
|
||||||
|
await invalidateAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function refresh() {
|
async function refresh() {
|
||||||
const res = await fetch('/api/basket');
|
const res = await fetch('/api/basket');
|
||||||
if (!res.ok) return;
|
if (!res.ok) return;
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
const slotLines = () => lines.filter((l) => l.lineType === 'slot').length;
|
||||||
|
const before = slotLines();
|
||||||
applyBasket(data.basket);
|
applyBasket(data.basket);
|
||||||
|
// Lapsed reservations were dropped: their places are free again.
|
||||||
|
if (slotLines() < before) await invalidateAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Overwrite local state from server-provided layout data. */
|
/** Overwrite local state from server-provided layout data. */
|
||||||
|
|
@ -50,15 +73,20 @@ export function createBasketStore(initial: Basket | null) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addSlotLine(slotId: string) {
|
async function addSlotLine(slotId: string) {
|
||||||
applyBasket(await callApi({ action: 'add_slot', slotId }));
|
await applySlotChange(await callApi({ action: 'add_slot', slotId }));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateQuantity(lineId: string, quantity: number) {
|
async function updateQuantity(lineId: string, quantity: number) {
|
||||||
applyBasket(await callApi({ action: 'update_quantity', lineId, quantity }));
|
await applySlotChange(await callApi({ action: 'update_quantity', lineId, quantity }));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function removeLine(lineId: string) {
|
async function removeLine(lineId: string) {
|
||||||
applyBasket(await callApi({ action: 'remove_line', lineId }));
|
await applySlotChange(await callApi({ action: 'remove_line', lineId }));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Hold the basket's time slots for another 10 minutes. */
|
||||||
|
async function renewHolds() {
|
||||||
|
await applySlotChange(await callApi({ action: 'renew_holds' }));
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -74,15 +102,23 @@ export function createBasketStore(initial: Basket | null) {
|
||||||
get status() {
|
get status() {
|
||||||
return status;
|
return status;
|
||||||
},
|
},
|
||||||
|
get holdDeadline() {
|
||||||
|
return holdDeadline;
|
||||||
|
},
|
||||||
refresh,
|
refresh,
|
||||||
syncFrom,
|
syncFrom,
|
||||||
addItemLine,
|
addItemLine,
|
||||||
addSlotLine,
|
addSlotLine,
|
||||||
updateQuantity,
|
updateQuantity,
|
||||||
removeLine
|
removeLine,
|
||||||
|
renewHolds
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clockOffsetOf(basket: Basket | null) {
|
||||||
|
return basket?.serverNow ? Date.now() - basket.serverNow : 0;
|
||||||
|
}
|
||||||
|
|
||||||
export function setBasketContext(store: BasketStore) {
|
export function setBasketContext(store: BasketStore) {
|
||||||
setContext(BASKET_KEY, store);
|
setContext(BASKET_KEY, store);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { onMount } from 'svelte';
|
||||||
import { browser } from '$app/environment';
|
import { browser } from '$app/environment';
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
import type { CheckoutBlockData } from '$shared/types';
|
import { page } from '$app/state';
|
||||||
|
import type { BasketLine, CheckoutBlockData } from '$shared/types';
|
||||||
import { getBasketContext } from '$shared/basket.svelte';
|
import { getBasketContext } from '$shared/basket.svelte';
|
||||||
|
import { DEFAULT_TIMEZONE, formatInstant, formatPrice } from '$shared/calendar';
|
||||||
import { loadStripe } from '$shared/stripe-client';
|
import { loadStripe } from '$shared/stripe-client';
|
||||||
import type { Stripe, StripeElements, StripePaymentElement } from '@stripe/stripe-js';
|
import type { Stripe, StripeElements, StripePaymentElement } from '@stripe/stripe-js';
|
||||||
|
|
||||||
|
|
@ -38,23 +41,79 @@
|
||||||
lastSig = sig;
|
lastSig = sig;
|
||||||
});
|
});
|
||||||
|
|
||||||
const currency = new Intl.NumberFormat(undefined, { style: 'currency', currency: 'EUR' });
|
|
||||||
const payLabel = $derived(
|
const payLabel = $derived(
|
||||||
payState === 'confirming'
|
payState === 'confirming'
|
||||||
? 'Confirming…'
|
? 'Confirming…'
|
||||||
: payState === 'creating'
|
: payState === 'creating'
|
||||||
? 'Preparing payment…'
|
? 'Preparing payment…'
|
||||||
: `Pay ${currency.format(basket.total)}`
|
: `Pay ${formatPrice(basket.total)}`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const timezone = $derived<string>(page.data.timezone ?? DEFAULT_TIMEZONE);
|
||||||
|
|
||||||
|
/** "Fri 15 Jan, 10:00" in the studio's timezone. */
|
||||||
function formatSlotTime(iso: string) {
|
function formatSlotTime(iso: string) {
|
||||||
return new Date(iso).toLocaleString(undefined, {
|
return formatInstant(iso, timezone, { weekday: 'short', day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' });
|
||||||
weekday: 'short',
|
}
|
||||||
month: 'short',
|
|
||||||
day: 'numeric',
|
// ── Reservation countdown ───────────────────────────────────────────
|
||||||
hour: '2-digit',
|
// Time slots are held for a while after being added (longer once payment
|
||||||
minute: '2-digit'
|
// starts). Show how long is left, offer more time near the end, and say
|
||||||
});
|
// so when a reservation runs out instead of the line silently vanishing.
|
||||||
|
|
||||||
|
/** Offer "keep my reservation" once less than this is left. */
|
||||||
|
const RENEW_OFFER_MS = 3 * 60 * 1000;
|
||||||
|
|
||||||
|
// null until mounted, so the server render shows no (soon wrong) countdown.
|
||||||
|
let now = $state<number | null>(null);
|
||||||
|
onMount(() => {
|
||||||
|
now = Date.now();
|
||||||
|
const timer = setInterval(() => (now = Date.now()), 1000);
|
||||||
|
return () => clearInterval(timer);
|
||||||
|
});
|
||||||
|
|
||||||
|
const holdLeftMs = $derived(now === null || basket.holdDeadline === null ? null : Math.max(0, basket.holdDeadline - now));
|
||||||
|
const slotLineCount = $derived(basket.lines.filter((l) => l.lineType === 'slot').length);
|
||||||
|
let renewing = $state(false);
|
||||||
|
/** Reservations that ran out while this page was open, e.g. "Group class, Fri 15 Jan, 10:00". */
|
||||||
|
let lapsed = $state<string[]>([]);
|
||||||
|
let nextLapseCheck = 0;
|
||||||
|
|
||||||
|
function formatCountdown(ms: number) {
|
||||||
|
const seconds = Math.ceil(ms / 1000);
|
||||||
|
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function describeLine(line: BasketLine) {
|
||||||
|
return [line.itemName ?? 'A time slot', line.slotStart ? formatSlotTime(line.slotStart) : ''].filter(Boolean).join(', ');
|
||||||
|
}
|
||||||
|
|
||||||
|
// At zero, reload the basket: the server drops the lapsed line. Retry every
|
||||||
|
// few seconds in case its clock is slightly behind.
|
||||||
|
$effect(() => {
|
||||||
|
if (holdLeftMs !== 0 || now === null || now < nextLapseCheck) return;
|
||||||
|
nextLapseCheck = now + 5000;
|
||||||
|
void checkLapsed();
|
||||||
|
});
|
||||||
|
|
||||||
|
async function checkLapsed() {
|
||||||
|
const before = basket.lines.filter((l) => l.lineType === 'slot');
|
||||||
|
await basket.refresh();
|
||||||
|
const gone = before.filter((l) => !basket.lines.some((line) => line.id === l.id));
|
||||||
|
if (gone.length) lapsed = [...lapsed, ...gone.map(describeLine)];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function renewHolds() {
|
||||||
|
if (renewing) return;
|
||||||
|
renewing = true;
|
||||||
|
errorMsg = '';
|
||||||
|
try {
|
||||||
|
await basket.renewHolds();
|
||||||
|
} catch (e) {
|
||||||
|
errorMsg = e instanceof Error ? e.message : 'Could not extend your reservation.';
|
||||||
|
} finally {
|
||||||
|
renewing = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetPayment() {
|
function resetPayment() {
|
||||||
|
|
@ -115,6 +174,8 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const { clientSecret } = await res.json();
|
const { clientSecret } = await res.json();
|
||||||
|
// Starting payment holds the slots longer; show the new countdown.
|
||||||
|
await basket.refresh();
|
||||||
|
|
||||||
stripe = await loadStripe();
|
stripe = await loadStripe();
|
||||||
if (!stripe) throw new Error('Payment system failed to load. Please refresh and try again.');
|
if (!stripe) throw new Error('Payment system failed to load. Please refresh and try again.');
|
||||||
|
|
@ -209,14 +270,59 @@
|
||||||
<p class="mt-2 text-fg-body/70">{data.heading}</p>
|
<p class="mt-2 text-fg-body/70">{data.heading}</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if lapsed.length}
|
||||||
|
<div class="mt-8 flex items-start justify-between gap-3 rounded-2xl border border-terracotta/40 bg-terracotta/10 px-5 py-4 text-sm text-charcoal" role="alert">
|
||||||
|
<div>
|
||||||
|
<p class="font-medium">
|
||||||
|
{lapsed.length === 1 ? 'Your reservation ran out' : 'Some reservations ran out'}, so {lapsed.length === 1 ? 'it was' : 'they were'} taken out of your basket:
|
||||||
|
</p>
|
||||||
|
<ul class="mt-1 list-disc pl-5">
|
||||||
|
{#each lapsed as description, i (i)}
|
||||||
|
<li>{description}</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
<p class="mt-1 text-charcoal/70">You can pick the time again in the shop if it’s still free.</p>
|
||||||
|
</div>
|
||||||
|
<button type="button" class="shrink-0 font-bold" aria-label="Dismiss" onclick={() => (lapsed = [])}>✕</button>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
{#if basket.lines.length === 0}
|
{#if basket.lines.length === 0}
|
||||||
<div class="mt-12 rounded-2xl border border-charcoal/10 bg-white/60 px-6 py-16 text-center">
|
<div class="mt-12 rounded-2xl border border-charcoal/10 bg-white/60 px-6 py-16 text-center">
|
||||||
<p class="font-serif text-xl">Your basket is empty</p>
|
<p class="font-serif text-xl">Your basket is empty</p>
|
||||||
<p class="mt-2 text-sm text-fg-body/70">Pick a service or product above.</p>
|
<p class="mt-2 text-sm text-fg-body/70">Pick a service or product above.</p>
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
|
{#if holdLeftMs !== null && slotLineCount}
|
||||||
|
{@const ending = holdLeftMs < RENEW_OFFER_MS}
|
||||||
|
<div
|
||||||
|
class="mt-8 flex flex-wrap items-center justify-between gap-3 rounded-2xl border px-5 py-3 text-sm {ending
|
||||||
|
? 'border-terracotta/40 bg-terracotta/10'
|
||||||
|
: 'border-sage/30 bg-sage/10'}"
|
||||||
|
role="timer"
|
||||||
|
aria-live={ending ? 'polite' : 'off'}
|
||||||
|
>
|
||||||
|
<p>
|
||||||
|
{slotLineCount === 1 ? 'Your time is' : 'Your times are'} reserved for
|
||||||
|
<strong class="tabular-nums">{formatCountdown(holdLeftMs)}</strong>{ending
|
||||||
|
? ' — after that it goes back to the shop.'
|
||||||
|
: '.'}
|
||||||
|
</p>
|
||||||
|
{#if ending && payState !== 'confirming'}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="rounded-full border border-charcoal/20 bg-white px-4 py-1.5 font-medium transition hover:bg-charcoal/5 disabled:opacity-60"
|
||||||
|
disabled={renewing}
|
||||||
|
onclick={renewHolds}
|
||||||
|
>
|
||||||
|
{renewing ? 'Extending…' : 'Keep it 10 more minutes'}
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<!-- Basket lines -->
|
<!-- Basket lines -->
|
||||||
<ul class="mt-8 space-y-4">
|
<ul class="{holdLeftMs !== null && slotLineCount ? 'mt-4' : 'mt-8'} space-y-4">
|
||||||
{#each basket.lines as line (line.id)}
|
{#each basket.lines as line (line.id)}
|
||||||
{@const pending = pendingLines.has(line.id)}
|
{@const pending = pendingLines.has(line.id)}
|
||||||
<li class="flex items-center gap-4 rounded-2xl border border-charcoal/10 bg-white p-4 shadow-sm">
|
<li class="flex items-center gap-4 rounded-2xl border border-charcoal/10 bg-white p-4 shadow-sm">
|
||||||
|
|
@ -237,7 +343,7 @@
|
||||||
{#if line.lineType === 'slot' && line.slotStart}
|
{#if line.lineType === 'slot' && line.slotStart}
|
||||||
<p class="text-sm text-fg-body/70">{formatSlotTime(line.slotStart)}</p>
|
<p class="text-sm text-fg-body/70">{formatSlotTime(line.slotStart)}</p>
|
||||||
{/if}
|
{/if}
|
||||||
<p class="text-sm text-fg-body/70">{currency.format(line.itemPrice ?? 0)}</p>
|
<p class="text-sm text-fg-body/70">{formatPrice(line.itemPrice ?? 0)}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Products, and places in a group session (capacity > 1); the server checks what's left. -->
|
<!-- Products, and places in a group session (capacity > 1); the server checks what's left. -->
|
||||||
|
|
@ -266,7 +372,7 @@
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<p class="w-20 shrink-0 text-right font-medium">
|
<p class="w-20 shrink-0 text-right font-medium">
|
||||||
{currency.format((line.itemPrice ?? 0) * line.quantity)}
|
{formatPrice((line.itemPrice ?? 0) * line.quantity)}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
|
|
@ -286,7 +392,7 @@
|
||||||
<div class="mt-6 rounded-2xl border border-charcoal/10 bg-white p-5">
|
<div class="mt-6 rounded-2xl border border-charcoal/10 bg-white p-5">
|
||||||
<div class="flex items-center justify-between">
|
<div class="flex items-center justify-between">
|
||||||
<span class="text-fg-body/70">Subtotal</span>
|
<span class="text-fg-body/70">Subtotal</span>
|
||||||
<span class="font-serif text-xl font-bold">{currency.format(basket.total)}</span>
|
<span class="font-serif text-xl font-bold">{formatPrice(basket.total)}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -368,7 +474,7 @@
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if basketChanged}
|
{#if basketChanged && !lapsed.length}
|
||||||
<p class="mt-4 rounded-xl border border-sage/30 bg-sage/10 px-4 py-3 text-sm">
|
<p class="mt-4 rounded-xl border border-sage/30 bg-sage/10 px-4 py-3 text-sm">
|
||||||
Basket changed — please press Pay again.
|
Basket changed — please press Pay again.
|
||||||
</p>
|
</p>
|
||||||
|
|
@ -388,7 +494,7 @@
|
||||||
disabled={payState === 'confirming'}
|
disabled={payState === 'confirming'}
|
||||||
onclick={confirmPayment}
|
onclick={confirmPayment}
|
||||||
>
|
>
|
||||||
{payState === 'confirming' ? 'Confirming…' : `Confirm payment — ${currency.format(basket.total)}`}
|
{payState === 'confirming' ? 'Confirming…' : `Confirm payment — ${formatPrice(basket.total)}`}
|
||||||
</button>
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -144,6 +144,8 @@ export type BasketLine = {
|
||||||
slotStart?: string;
|
slotStart?: string;
|
||||||
slotEnd?: string;
|
slotEnd?: string;
|
||||||
slotCapacity?: number;
|
slotCapacity?: number;
|
||||||
|
/** Slot lines: when the reservation lapses and the line is dropped (ISO). */
|
||||||
|
heldUntil?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Basket = {
|
export type Basket = {
|
||||||
|
|
@ -154,6 +156,8 @@ export type Basket = {
|
||||||
customerDetails: CustomerDetails | null;
|
customerDetails: CustomerDetails | null;
|
||||||
stripePaymentIntentId: string | null;
|
stripePaymentIntentId: string | null;
|
||||||
lines: BasketLine[];
|
lines: BasketLine[];
|
||||||
|
/** Server clock (epoch ms) when this was read, so browsers with a wrong clock count holds down correctly. */
|
||||||
|
serverNow?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CustomerDetails = {
|
export type CustomerDetails = {
|
||||||
|
|
|
||||||
|
|
@ -275,8 +275,9 @@ function createBasket(app) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renew the hold on every slot line in a basket. Used at checkout start
|
* Renew the hold on every slot line in a basket. Used at checkout start
|
||||||
* (30 min) and after a failed payment (10 min, so the customer can retry).
|
* (30 min), after a failed payment (10 min, so the customer can retry) and
|
||||||
* A live hold is simply extended (its seats are already counted). A lapsed
|
* when the customer asks for more time (`renew_holds`, 10 min). A live hold
|
||||||
|
* is extended, never shortened (its seats are already counted). A lapsed
|
||||||
* one that hasn't been swept yet is rescued only if its seats are still
|
* one that hasn't been swept yet is rescued only if its seats are still
|
||||||
* free and nothing has blocked the time since — otherwise it's left for
|
* free and nothing has blocked the time since — otherwise it's left for
|
||||||
* the sweep, and checkout tells the customer the slot expired.
|
* the sweep, and checkout tells the customer the slot expired.
|
||||||
|
|
@ -285,9 +286,12 @@ function extendBasketHolds(app, basketId, durationMs) {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const until = isoIn(durationMs);
|
const until = isoIn(durationMs);
|
||||||
for (const line of findAll(app, 'basket_lines', "basket = {:basket} && lineType = 'slot'", { basket: basketId })) {
|
for (const line of findAll(app, 'basket_lines', "basket = {:basket} && lineType = 'slot'", { basket: basketId })) {
|
||||||
if (toMs(line.getString('heldUntil')) > now) {
|
const heldUntil = toMs(line.getString('heldUntil'));
|
||||||
line.set('heldUntil', until);
|
if (heldUntil > now) {
|
||||||
app.save(line);
|
if (heldUntil < now + durationMs) {
|
||||||
|
line.set('heldUntil', until);
|
||||||
|
app.save(line);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|
@ -379,6 +383,13 @@ function basketAction(app, body) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 'renew_holds': {
|
||||||
|
// "Keep my reservation" at checkout. Holds that already lapsed were
|
||||||
|
// swept above; the customer is told and picks the time again.
|
||||||
|
extendBasketHolds(app, basket.id, SLOT_HOLD_MS);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case 'remove_line': {
|
case 'remove_line': {
|
||||||
const line = lines.find((l) => l.id === String(body.lineId || ''));
|
const line = lines.find((l) => l.id === String(body.lineId || ''));
|
||||||
if (!line) throw new NotFoundError('Line not found in your basket');
|
if (!line) throw new NotFoundError('Line not found in your basket');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue