Shop block: share item loading with the admin preview, never render blank
The admin preview never received items/slots/events, so Shop and Upcoming calendar blocks were empty there. loadShopData() (shared) now feeds both the public layout and a new /preview loader. A Shop block with nothing to show (no active items, a filter matching none, or commerce data unavailable because POCKETBASE_ADMIN_* is unset) now says so instead of rendering an empty section. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
This commit is contained in:
parent
fb2157bbe0
commit
91e3e916d8
5 changed files with 92 additions and 43 deletions
22
app-admin/src/routes/preview/+page.server.ts
Normal file
22
app-admin/src/routes/preview/+page.server.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { eventOverlapsRange, getUpcomingCalendarRange, normalizeEvent } from '$lib/calendar';
|
||||
import { loadShopData } from '$shared/server/shop';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
// The draft blocks come from sessionStorage (client-side), so load the data
|
||||
// any block might need: shop items/slots and upcoming events. Failures leave
|
||||
// the blocks empty rather than breaking the preview.
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const range = getUpcomingCalendarRange();
|
||||
const [shop, events] = await Promise.all([
|
||||
loadShopData(locals.pb).catch((e) => {
|
||||
console.warn('Preview: shop data unavailable:', e instanceof Error ? e.message : e);
|
||||
return { items: [], slots: [] };
|
||||
}),
|
||||
locals.pb
|
||||
.collection('events')
|
||||
.getFullList({ filter: locals.pb.filter('published = true && start <= {:end}', { end: range.end }), sort: 'start' })
|
||||
.catch(() => [])
|
||||
]);
|
||||
const calendarEvents = events.map((event) => normalizeEvent(event)).filter((event) => eventOverlapsRange(event, range.start, range.end));
|
||||
return { ...shop, calendarEvents };
|
||||
};
|
||||
|
|
@ -27,7 +27,7 @@
|
|||
</svelte:head>
|
||||
|
||||
{#if blocks.length}
|
||||
<BlockRenderer {blocks} />
|
||||
<BlockRenderer {blocks} items={data.items} slots={data.slots} calendarEvents={data.calendarEvents} />
|
||||
{:else}
|
||||
<section class="mx-auto max-w-3xl px-6 py-20 text-center">
|
||||
<h1 class="text-4xl">No preview content</h1>
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
import { defaultColors, defaultFooter, defaultHeader } from '$lib/types';
|
||||
import type { ItemRecord, SlotRecord } from '$lib/types';
|
||||
import { getFileUrl } from '$lib/pb';
|
||||
import { getAdminPb } from '$lib/server/pb-admin';
|
||||
import { BASKET_COOKIE, loadBasket, sweepExpiredHolds } from '$lib/server/basket';
|
||||
import { loadShopData } from '$shared/server/shop';
|
||||
import type { LayoutServerLoad } from './$types';
|
||||
|
||||
async function getGlobal(pb: App.Locals['pb'], key: string, fallback: unknown) {
|
||||
try {
|
||||
const record = await pb.collection('globals').getFirstListItem(`key = "${key}"`);
|
||||
const record = await pb.collection('globals').getFirstListItem(pb.filter('key = {:key}', { key }));
|
||||
return record.data || fallback;
|
||||
} catch {
|
||||
return fallback;
|
||||
|
|
@ -35,44 +35,11 @@ export const load: LayoutServerLoad = async ({ locals, cookies }) => {
|
|||
const basketId = cookies.get(BASKET_COOKIE);
|
||||
if (basketId) basket = await loadBasket(pb, basketId);
|
||||
|
||||
const itemRecords = await pb
|
||||
.collection('items')
|
||||
.getFullList({ filter: 'active = true', sort: 'name', expand: 'image' })
|
||||
.catch(() => []);
|
||||
items = itemRecords.map((record) => {
|
||||
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,
|
||||
active: record.active
|
||||
};
|
||||
});
|
||||
|
||||
const nowIso = new Date().toISOString();
|
||||
const slotRecords = await pb
|
||||
.collection('slots')
|
||||
.getFullList({ filter: `status = 'available' && start >= "${nowIso}"`, sort: 'start' })
|
||||
.catch(() => []);
|
||||
slots = slotRecords.map((record) => ({
|
||||
id: record.id,
|
||||
item: record.item,
|
||||
start: record.start,
|
||||
end: record.end || '',
|
||||
status: record.status,
|
||||
heldUntil: record.heldUntil || null,
|
||||
order: record.order || null,
|
||||
notes: record.notes || ''
|
||||
}));
|
||||
({ items, slots } = await loadShopData(pb));
|
||||
} catch (e) {
|
||||
// Admin PB not configured (e.g. fresh dev env) — commerce features off.
|
||||
console.warn('Commerce data unavailable:', e instanceof Error ? e.message : e);
|
||||
// Admin PB not configured (e.g. fresh dev env) — commerce features off;
|
||||
// Shop blocks then show their "nothing available" message.
|
||||
console.warn('Commerce data unavailable (shop blocks will be empty):', e instanceof Error ? e.message : e);
|
||||
}
|
||||
|
||||
return { header, footer, colors, user: locals.user, basket, items, slots };
|
||||
|
|
|
|||
|
|
@ -130,6 +130,14 @@
|
|||
<h2 class="mb-12 text-center font-serif text-3xl text-charcoal md:text-4xl">{data.title}</h2>
|
||||
{/if}
|
||||
|
||||
{#if !visibleItems.length}
|
||||
<!-- Never an empty section: no active items, a filter that matches none,
|
||||
or commerce data unavailable (see the public layout's server log). -->
|
||||
<p class="rounded-2xl border border-dashed border-charcoal/15 px-6 py-10 text-center text-charcoal/60">
|
||||
Nothing is available to book right now — please check back soon.
|
||||
</p>
|
||||
{/if}
|
||||
|
||||
<div class={isList ? 'flex flex-col gap-8' : 'grid gap-8 sm:grid-cols-2 lg:grid-cols-3'}>
|
||||
{#each visibleItems as item (item.id)}
|
||||
{@const ui = cardStates[item.id]}
|
||||
|
|
@ -204,7 +212,7 @@
|
|||
class="inline-flex items-center justify-center gap-2 rounded-full px-6 py-2.5 text-sm font-medium transition {ui?.pickerOpen
|
||||
? 'border border-charcoal/15 text-charcoal hover:bg-charcoal/5'
|
||||
: itemSlotsByDay?.size
|
||||
? 'bg-sage text-white hover:bg-sage-dark'
|
||||
? 'bg-sage text-primary-content hover:bg-sage-dark'
|
||||
: 'cursor-not-allowed bg-charcoal/10 text-charcoal/50'}"
|
||||
disabled={!itemSlotsByDay?.size && !ui?.pickerOpen}
|
||||
onclick={() => togglePicker(item.id)}
|
||||
|
|
@ -214,7 +222,7 @@
|
|||
{:else}
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center justify-center gap-2 rounded-full bg-sage px-6 py-2.5 text-sm font-medium text-white transition hover:bg-sage-dark disabled:cursor-not-allowed disabled:bg-charcoal/10 disabled:text-charcoal/50"
|
||||
class="inline-flex items-center justify-center gap-2 rounded-full bg-sage px-6 py-2.5 text-sm font-medium text-primary-content transition hover:bg-sage-dark disabled:cursor-not-allowed disabled:bg-charcoal/10 disabled:text-charcoal/50"
|
||||
disabled={soldOut || ui?.adding}
|
||||
onclick={() => addItem(item.id)}
|
||||
>
|
||||
|
|
@ -252,7 +260,7 @@
|
|||
aria-pressed={selected}
|
||||
onclick={() => (ui.selectedDayKey = day.dateKey)}
|
||||
class="aspect-square rounded-full text-sm transition {selected
|
||||
? 'bg-sage font-semibold text-white'
|
||||
? 'bg-sage font-semibold text-primary-content'
|
||||
: selectable
|
||||
? 'cursor-pointer bg-sage/15 font-medium text-charcoal hover:bg-sage/35'
|
||||
: 'cursor-default text-charcoal/25'} {!day.inMonth ? 'opacity-40' : ''} {day.isToday &&
|
||||
|
|
|
|||
52
packages/shared/src/server/shop.ts
Normal file
52
packages/shared/src/server/shop.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import type PocketBase from 'pocketbase';
|
||||
import { getFileUrl } from '../pb';
|
||||
import type { ItemRecord, SlotRecord } from '../types';
|
||||
|
||||
/**
|
||||
* Active items and future available slots, shaped for the Shop/Checkout
|
||||
* blocks. Used by the public layout and the admin draft preview so both
|
||||
* render the same shop.
|
||||
*
|
||||
* `pb` must be able to read items, slots and media (admin client on the
|
||||
* public site, the logged-in superuser in the admin). Errors propagate.
|
||||
*/
|
||||
export async function loadShopData(pb: PocketBase): Promise<{ items: ItemRecord[]; slots: SlotRecord[] }> {
|
||||
const [itemRecords, slotRecords] = await Promise.all([
|
||||
pb.collection('items').getFullList({ filter: 'active = true', sort: 'name', expand: 'image' }),
|
||||
pb.collection('slots').getFullList({
|
||||
filter: pb.filter("status = 'available' && start >= {:now}", { now: new Date() }),
|
||||
sort: 'start'
|
||||
})
|
||||
]);
|
||||
|
||||
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,
|
||||
active: record.active
|
||||
};
|
||||
});
|
||||
|
||||
const slots = slotRecords.map(
|
||||
(record): SlotRecord => ({
|
||||
id: record.id,
|
||||
item: record.item,
|
||||
start: record.start,
|
||||
end: record.end || '',
|
||||
status: record.status,
|
||||
heldUntil: record.heldUntil || null,
|
||||
order: record.order || null,
|
||||
notes: record.notes || ''
|
||||
})
|
||||
);
|
||||
|
||||
return { items, slots };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue