Pakkettenblok: live CMS-OfferingBanner (GET /api/v1/offerings/banner), optionele knop, 204 = geen banner
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
/** Active offering banner as served by `GET /api/v1/offerings/banner` (no planning fields). */
|
||||
export interface OfferingBannerData {
|
||||
title: string;
|
||||
text: string;
|
||||
linkLabel: string | null;
|
||||
linkUrl: string | null;
|
||||
}
|
||||
|
||||
function bannerEndpoint() {
|
||||
return `${import.meta.env.VITE_API_BASE_URL ?? ''}/api/v1/offerings/banner`;
|
||||
}
|
||||
|
||||
/** 204 (no active banner) and any failure resolve to `null`: the banner is optional, never an error. */
|
||||
async function fetchBanner(): Promise<OfferingBannerData | null> {
|
||||
try {
|
||||
const response = await fetch(bannerEndpoint());
|
||||
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') {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
title: data.title,
|
||||
text: data.text,
|
||||
linkLabel: data.linkLabel ?? null,
|
||||
linkUrl: data.linkUrl ?? null,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function useOfferingBannerQuery() {
|
||||
return useQuery({
|
||||
queryKey: ['offering-banner'],
|
||||
queryFn: fetchBanner,
|
||||
// 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