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
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