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
|
|
@ -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 };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue