Pakketten: laatste rij gecentreerd op desktop, tweede (smalle) OfferingBanner boven de pakketten (?position=top)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,23 +8,29 @@ export interface OfferingBannerData {
|
||||
linkUrl: string | null;
|
||||
}
|
||||
|
||||
function bannerEndpoint() {
|
||||
return `${import.meta.env.VITE_API_BASE_URL ?? ''}/api/v1/offerings/banner`;
|
||||
/** Two places: `bottom` (default, big banner under the cards) and `top` (slim one-line bar above them). */
|
||||
export type OfferingBannerPosition = 'top' | 'bottom';
|
||||
|
||||
function bannerEndpoint(position: OfferingBannerPosition) {
|
||||
const base = `${import.meta.env.VITE_API_BASE_URL ?? ''}/api/v1/offerings/banner`;
|
||||
// No parameter means `bottom` (backwards compatible with the first API version).
|
||||
return position === 'top' ? `${base}?position=top` : base;
|
||||
}
|
||||
|
||||
/** 204 (no active banner) and any failure resolve to `null`: the banner is optional, never an error. */
|
||||
async function fetchBanner(): Promise<OfferingBannerData | null> {
|
||||
async function fetchBanner(position: OfferingBannerPosition): Promise<OfferingBannerData | null> {
|
||||
try {
|
||||
const response = await fetch(bannerEndpoint());
|
||||
const response = await fetch(bannerEndpoint(position));
|
||||
if (response.status === 204 || !response.ok) {
|
||||
return null;
|
||||
}
|
||||
const data = (await response.json()) as Partial<OfferingBannerData> | null;
|
||||
if (!data || typeof data.title !== 'string' || typeof data.text !== 'string') {
|
||||
// A top banner may have an empty title; text is always required.
|
||||
if (!data || typeof data.text !== 'string' || data.text === '') {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
title: data.title,
|
||||
title: typeof data.title === 'string' ? data.title : '',
|
||||
text: data.text,
|
||||
linkLabel: data.linkLabel ?? null,
|
||||
linkUrl: data.linkUrl ?? null,
|
||||
@@ -34,10 +40,10 @@ async function fetchBanner(): Promise<OfferingBannerData | null> {
|
||||
}
|
||||
}
|
||||
|
||||
export function useOfferingBannerQuery() {
|
||||
export function useOfferingBannerQuery(position: OfferingBannerPosition = 'bottom') {
|
||||
return useQuery({
|
||||
queryKey: ['offering-banner'],
|
||||
queryFn: fetchBanner,
|
||||
queryKey: ['offering-banner', position],
|
||||
queryFn: () => fetchBanner(position),
|
||||
// Short staleness so a change in the CMS shows up quickly (the API itself caches for 60 s).
|
||||
staleTime: 60_000,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user