Redesign Modern/Tijdreis, contactformulier via CMS, taalbestanden #17
@@ -29,8 +29,19 @@ function renderSection() {
|
||||
const BANNER_PATH = '/api/v1/offerings/banner';
|
||||
|
||||
/** Routes the optional CMS banner request (204 = none, or the given banner) apart from the packages call. */
|
||||
function stubFetch(offerings: () => Promise<Response>, banner: unknown = null) {
|
||||
function stubFetch(
|
||||
offerings: () => Promise<Response>,
|
||||
banner: unknown = null,
|
||||
topBanner: unknown = null,
|
||||
) {
|
||||
const fn = vi.fn((url: string) => {
|
||||
if (String(url).endsWith(`${BANNER_PATH}?position=top`)) {
|
||||
return Promise.resolve(
|
||||
topBanner === null
|
||||
? new Response(null, { status: 204 })
|
||||
: new Response(JSON.stringify(topBanner), { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (String(url).endsWith(BANNER_PATH)) {
|
||||
return Promise.resolve(
|
||||
banner === null
|
||||
@@ -168,6 +179,26 @@ describe('PackagesSection', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('shows a slim top banner next to the bottom banner when both are active', async () => {
|
||||
stubFetch(
|
||||
() => Promise.resolve(new Response(JSON.stringify(packages), { status: 200 })),
|
||||
{ title: 'Onder', text: 'Grote banner.', linkLabel: null, linkUrl: null },
|
||||
{
|
||||
title: '',
|
||||
text: 'Nu 10% korting.',
|
||||
linkLabel: 'Meer',
|
||||
linkUrl: 'https://example.com/top',
|
||||
},
|
||||
);
|
||||
renderSection();
|
||||
expect(await screen.findByTestId('offering-top-banner')).toHaveTextContent('Nu 10% korting.');
|
||||
expect(screen.getByTestId('offering-top-banner-cta')).toHaveAttribute(
|
||||
'href',
|
||||
'https://example.com/top',
|
||||
);
|
||||
expect(await screen.findByTestId('offering-banner')).toHaveTextContent('Onder');
|
||||
});
|
||||
|
||||
it('renders no banner without link when the CMS has none', async () => {
|
||||
mockApi(packages);
|
||||
renderSection();
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import type { OfferingBannerData } from '../hooks/useOfferingBannerQuery';
|
||||
|
||||
/**
|
||||
* Slim one-line bar above the package cards, filled from the CMS OfferingBanner with position
|
||||
* `top` (live, optional). May be shown together with the big banner under the cards.
|
||||
*/
|
||||
export function OfferingTopBanner({ banner }: { banner: OfferingBannerData }) {
|
||||
const hasLink = banner.linkLabel !== null && banner.linkUrl !== null;
|
||||
return (
|
||||
<div
|
||||
className="mb-6 flex flex-wrap items-center justify-center gap-x-4 gap-y-1 bg-inv px-5 py-3 text-center text-[15px] text-white"
|
||||
data-testid="offering-top-banner"
|
||||
>
|
||||
<p className="m-0">
|
||||
{banner.title ? <strong className="mr-2 font-bold">{banner.title}</strong> : null}
|
||||
<span className="text-[#cfcac5]">{banner.text}</span>
|
||||
</p>
|
||||
{hasLink ? (
|
||||
<a
|
||||
href={banner.linkUrl ?? undefined}
|
||||
className="font-bold text-white underline underline-offset-4 hover:text-[#cfcac5]"
|
||||
data-testid="offering-top-banner-cta"
|
||||
>
|
||||
{banner.linkLabel}
|
||||
</a>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -37,10 +37,13 @@ export function PackageList({ packages, loading = false, resetKey }: PackageList
|
||||
aria-label="Pakketten"
|
||||
tabIndex={0}
|
||||
data-testid="packages-grid"
|
||||
className="-mx-6 flex snap-x snap-mandatory items-stretch gap-4 overflow-x-auto px-6 pb-2 sm:mx-0 sm:grid sm:snap-none sm:grid-cols-2 sm:overflow-visible sm:px-0 sm:pb-0 min-[1001px]:grid-cols-3"
|
||||
className="-mx-6 flex snap-x snap-mandatory items-stretch gap-4 overflow-x-auto px-6 pb-2 sm:mx-0 sm:flex-wrap sm:justify-center sm:snap-none sm:overflow-visible sm:px-0 sm:pb-0"
|
||||
>
|
||||
{items.map((item, index) => (
|
||||
<div key={index} className="w-[85%] flex-none snap-center sm:w-auto">
|
||||
<div
|
||||
key={index}
|
||||
className="w-[85%] flex-none snap-center sm:w-[calc((100%-1rem)/2)] min-[1001px]:w-[calc((100%-2rem)/3)]"
|
||||
>
|
||||
{item}
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { usePackageFilter } from '../../../shared/packages/usePackageFilter';
|
||||
import { FilterChips } from './FilterChips';
|
||||
import { useOfferingBannerQuery } from '../hooks/useOfferingBannerQuery';
|
||||
import { OfferingBanner } from './OfferingBanner';
|
||||
import { OfferingTopBanner } from './OfferingTopBanner';
|
||||
import { PackageBanner } from './PackageBanner';
|
||||
import { PackageList } from './PackageList';
|
||||
|
||||
@@ -20,6 +21,7 @@ export function PackagesSection() {
|
||||
const filter = usePackageFilter(data ?? [], packagesSettings);
|
||||
const loaded = !isLoading && !isError && data !== undefined;
|
||||
const { data: offeringBanner } = useOfferingBannerQuery();
|
||||
const { data: topBanner } = useOfferingBannerQuery('top');
|
||||
// The CMS OfferingBanner (live) takes precedence over the legacy per-site package banner.
|
||||
const banner = offeringBanner ? (
|
||||
<OfferingBanner banner={offeringBanner} />
|
||||
@@ -43,6 +45,8 @@ export function PackagesSection() {
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{topBanner ? <OfferingTopBanner banner={topBanner} /> : null}
|
||||
|
||||
{isError ? (
|
||||
<div className="text-center" data-testid="packages-error" role="alert">
|
||||
<p className="mb-4 text-muted">Pakketten konden niet worden geladen.</p>
|
||||
|
||||
@@ -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