Offerings: banner krijgt positie top|bottom (backend, migratie, admin-tab, OpenAPI)
- OfferingBanner.Position ('top'|'bottom'); bestaande banners worden 'bottom' (migratie AddOfferingBannerPosition)
- GET /api/v1/offerings/banner?position=top|bottom: zonder parameter bottom, 204 zonder actieve banner, 400 bij ongeldige waarde; antwoord bevat position
- Max 1 actieve banner per positie (laatst gewijzigde binnen de planning wint); top en bottom kunnen tegelijk actief zijn
- Top: tekst max 120 tekens, titel optioneel; bottom: titel verplicht
- Admin: Banner-tab met twee secties (Boven/Onder), eigen aanmaken en aan/uit per plek
- OpenAPI-snapshot met de hand bijgewerkt (geen lokale MariaDB): CI bevestigt
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useState } from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { useForm, useWatch } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { toast } from 'sonner';
|
||||
@@ -12,21 +12,23 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/u
|
||||
import { NetworkError } from '@/lib/api-client';
|
||||
import {
|
||||
fromLocalInput,
|
||||
bannerTextMaxLength,
|
||||
offeringBannerFormSchema,
|
||||
toLocalInput,
|
||||
type OfferingBannerFormData,
|
||||
} from '../schemas/offeringBanner';
|
||||
import { useSaveOfferingBanner } from '../services/useSaveOfferingBanner';
|
||||
import type { OfferingBannerDto } from '../services/types';
|
||||
import type { BannerPosition, OfferingBannerDto } from '../services/types';
|
||||
|
||||
interface OfferingBannerDialogProps {
|
||||
/** Open for editing this banner; null with `creating` = a new one. */
|
||||
/** Open for editing this banner (its own position applies); null with `creatingPosition` = a new one there. */
|
||||
banner: OfferingBannerDto | null;
|
||||
creating: boolean;
|
||||
creatingPosition: BannerPosition | null;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
const EMPTY: OfferingBannerFormData = {
|
||||
const emptyForm = (position: BannerPosition): OfferingBannerFormData => ({
|
||||
position,
|
||||
title: '',
|
||||
text: '',
|
||||
linkLabel: '',
|
||||
@@ -34,11 +36,12 @@ const EMPTY: OfferingBannerFormData = {
|
||||
isActive: true,
|
||||
startsAt: '',
|
||||
endsAt: '',
|
||||
};
|
||||
});
|
||||
|
||||
function toForm(banner: OfferingBannerDto | null): OfferingBannerFormData {
|
||||
if (!banner) return EMPTY;
|
||||
function toForm(banner: OfferingBannerDto | null, position: BannerPosition): OfferingBannerFormData {
|
||||
if (!banner) return emptyForm(position);
|
||||
return {
|
||||
position: banner.position,
|
||||
title: banner.title,
|
||||
text: banner.text,
|
||||
linkLabel: banner.linkLabel ?? '',
|
||||
@@ -49,26 +52,35 @@ function toForm(banner: OfferingBannerDto | null): OfferingBannerFormData {
|
||||
};
|
||||
}
|
||||
|
||||
export function OfferingBannerDialog({ banner, creating, onClose }: OfferingBannerDialogProps) {
|
||||
export function OfferingBannerDialog({ banner, creatingPosition, onClose }: OfferingBannerDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const open = creating || banner !== null;
|
||||
const open = creatingPosition !== null || banner !== null;
|
||||
const position: BannerPosition = banner?.position ?? creatingPosition ?? 'bottom';
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={(next) => { if (!next) onClose(); }}>
|
||||
<DialogContent className="max-h-[90vh] overflow-y-auto sm:max-w-lg">
|
||||
<DialogHeader>
|
||||
<DialogTitle data-testid="banner-dialog-title">
|
||||
{banner ? t('offerings.banners.editTitle') : t('offerings.banners.createTitle')}
|
||||
{banner ? t(`offerings.banners.editTitle_${position}`) : t(`offerings.banners.createTitle_${position}`)}
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
{/* Content is unmounted while closed, so every opening starts from fresh form state. */}
|
||||
<OfferingBannerForm banner={banner} onClose={onClose} />
|
||||
<OfferingBannerForm banner={banner} position={position} onClose={onClose} />
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
function OfferingBannerForm({ banner, onClose }: { banner: OfferingBannerDto | null; onClose: () => void }) {
|
||||
function OfferingBannerForm({
|
||||
banner,
|
||||
position,
|
||||
onClose,
|
||||
}: {
|
||||
banner: OfferingBannerDto | null;
|
||||
position: BannerPosition;
|
||||
onClose: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [serverError, setServerError] = useState<string | null>(null);
|
||||
const save = useSaveOfferingBanner();
|
||||
@@ -76,12 +88,15 @@ function OfferingBannerForm({ banner, onClose }: { banner: OfferingBannerDto | n
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
control,
|
||||
formState: { errors },
|
||||
} = useForm<OfferingBannerFormData>({
|
||||
resolver: zodResolver(offeringBannerFormSchema),
|
||||
mode: 'onTouched',
|
||||
defaultValues: toForm(banner),
|
||||
defaultValues: toForm(banner, position),
|
||||
});
|
||||
const textMax = bannerTextMaxLength(position);
|
||||
const textLength = (useWatch({ control, name: 'text' }) ?? '').length;
|
||||
|
||||
const onSubmit = async (values: OfferingBannerFormData) => {
|
||||
setServerError(null);
|
||||
@@ -89,6 +104,7 @@ function OfferingBannerForm({ banner, onClose }: { banner: OfferingBannerDto | n
|
||||
await save.mutateAsync({
|
||||
id: banner?.id,
|
||||
body: {
|
||||
position: values.position,
|
||||
title: values.title,
|
||||
text: values.text,
|
||||
linkLabel: values.linkLabel || null,
|
||||
@@ -114,7 +130,7 @@ function OfferingBannerForm({ banner, onClose }: { banner: OfferingBannerDto | n
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4" noValidate data-testid="banner-form">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="banner-title">{t('offerings.banners.titleLabel')}</Label>
|
||||
<Label htmlFor="banner-title">{t(position === 'top' ? 'offerings.banners.titleLabelOptional' : 'offerings.banners.titleLabel')}</Label>
|
||||
<Input id="banner-title" data-testid="banner-title" maxLength={100} {...register('title')} />
|
||||
<FieldError message={errors.title?.message} testId="banner-title-error" />
|
||||
</div>
|
||||
@@ -124,11 +140,18 @@ function OfferingBannerForm({ banner, onClose }: { banner: OfferingBannerDto | n
|
||||
<textarea
|
||||
id="banner-text"
|
||||
data-testid="banner-text"
|
||||
rows={3}
|
||||
maxLength={300}
|
||||
rows={position === 'top' ? 2 : 3}
|
||||
maxLength={textMax}
|
||||
className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
|
||||
{...register('text')}
|
||||
/>
|
||||
<p
|
||||
className={`text-xs ${textLength >= textMax ? 'text-destructive' : 'text-muted-foreground'}`}
|
||||
data-testid="banner-text-counter"
|
||||
>
|
||||
{textLength}/{textMax}
|
||||
{position === 'top' && ` · ${t('offerings.banners.textHintTop')}`}
|
||||
</p>
|
||||
<FieldError message={errors.text?.message} testId="banner-text-error" />
|
||||
</div>
|
||||
|
||||
|
||||
@@ -1,111 +1,182 @@
|
||||
import { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Megaphone, Pencil, Plus, Trash2 } from 'lucide-react';
|
||||
import { Megaphone, Pencil, Plus, Power, Trash2 } from 'lucide-react';
|
||||
import { toast } from 'sonner';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
||||
import { useOfferingBanners } from '../services/useOfferingBanners';
|
||||
import { useDeleteOfferingBanner } from '../services/useDeleteOfferingBanner';
|
||||
import { useSaveOfferingBanner } from '../services/useSaveOfferingBanner';
|
||||
import { bannerStatus } from '../schemas/offeringBanner';
|
||||
import { OfferingBannerDialog } from './OfferingBannerDialog';
|
||||
import type { OfferingBannerDto } from '../services/types';
|
||||
import type { BannerPosition, OfferingBannerDto } from '../services/types';
|
||||
|
||||
const fmt = (iso: string | null) =>
|
||||
iso ? new Date(iso).toLocaleString('nl-NL', { dateStyle: 'short', timeStyle: 'short' }) : null;
|
||||
|
||||
const POSITIONS: BannerPosition[] = ['top', 'bottom'];
|
||||
|
||||
const statusVariant = { live: 'default', scheduled: 'secondary', expired: 'outline', off: 'outline' } as const;
|
||||
|
||||
/** The banner shown now for one position: among the live ones, the most recently changed wins. */
|
||||
function liveBannerOf(banners: OfferingBannerDto[], now = new Date()): OfferingBannerDto | null {
|
||||
const live = banners.filter((b) => bannerStatus(b, now) === 'live');
|
||||
if (live.length === 0) return null;
|
||||
return live.reduce((a, b) => (new Date(b.updatedAt) > new Date(a.updatedAt) ? b : a));
|
||||
}
|
||||
|
||||
export function OfferingBannersTab() {
|
||||
const { t } = useTranslation();
|
||||
const { data: banners, isPending, isError } = useOfferingBanners();
|
||||
const [editing, setEditing] = useState<OfferingBannerDto | null>(null);
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [creating, setCreating] = useState<BannerPosition | null>(null);
|
||||
const [pendingDelete, setPendingDelete] = useState<OfferingBannerDto | null>(null);
|
||||
const remove = useDeleteOfferingBanner();
|
||||
const save = useSaveOfferingBanner();
|
||||
|
||||
const statusVariant = { live: 'default', scheduled: 'secondary', expired: 'outline', off: 'outline' } as const;
|
||||
const toggleActive = (b: OfferingBannerDto) =>
|
||||
save.mutate(
|
||||
{
|
||||
id: b.id,
|
||||
body: {
|
||||
position: b.position,
|
||||
title: b.title,
|
||||
text: b.text,
|
||||
linkLabel: b.linkLabel,
|
||||
linkUrl: b.linkUrl,
|
||||
isActive: !b.isActive,
|
||||
startsAt: b.startsAt,
|
||||
endsAt: b.endsAt,
|
||||
},
|
||||
},
|
||||
{
|
||||
onSuccess: () => toast.success(t('offerings.banners.savedToast')),
|
||||
onError: () => toast.error(t('errors.generic')),
|
||||
},
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="space-y-4" data-testid="banners-tab">
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<p className="text-sm text-muted-foreground">{t('offerings.banners.intro')}</p>
|
||||
<Button type="button" onClick={() => setCreating(true)} data-testid="banner-add-button">
|
||||
<Plus className="mr-1 size-4" aria-hidden="true" />
|
||||
{t('offerings.banners.addButton')}
|
||||
</Button>
|
||||
</div>
|
||||
<div className="space-y-6" data-testid="banners-tab">
|
||||
<p className="text-sm text-muted-foreground">{t('offerings.banners.intro')}</p>
|
||||
|
||||
{isPending && <p className="text-sm text-muted-foreground">{t('common.loading')}</p>}
|
||||
{isError && <p className="text-sm text-destructive">{t('errors.generic')}</p>}
|
||||
|
||||
{!isPending && !isError && banners?.length === 0 && (
|
||||
<div className="flex flex-col items-center space-y-2 py-12 text-center" data-testid="banners-empty">
|
||||
<Megaphone className="size-10 text-muted-foreground" aria-hidden="true" />
|
||||
<p className="text-muted-foreground">{t('offerings.banners.empty')}</p>
|
||||
</div>
|
||||
)}
|
||||
{!isPending &&
|
||||
!isError &&
|
||||
POSITIONS.map((position) => {
|
||||
const items = (banners ?? []).filter((b) => b.position === position);
|
||||
const live = liveBannerOf(items);
|
||||
return (
|
||||
<section key={position} className="space-y-3" data-testid={`banners-section-${position}`}>
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<div className="space-y-1">
|
||||
<h2 className="text-base font-semibold">{t(`offerings.banners.section.${position}.title`)}</h2>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t(`offerings.banners.section.${position}.description`)}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground" data-testid={`banner-live-hint-${position}`}>
|
||||
{live
|
||||
? t('offerings.banners.liveNow', { title: live.title || live.text })
|
||||
: t('offerings.banners.nothingActive')}
|
||||
</p>
|
||||
</div>
|
||||
<Button type="button" onClick={() => setCreating(position)} data-testid={`banner-add-${position}`}>
|
||||
<Plus className="mr-1 size-4" aria-hidden="true" />
|
||||
{t(`offerings.banners.section.${position}.addButton`)}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{banners && banners.length > 0 && (
|
||||
<ul className="space-y-2" data-testid="banners-list">
|
||||
{banners.map((b) => {
|
||||
const status = bannerStatus(b);
|
||||
return (
|
||||
<li
|
||||
key={b.id}
|
||||
className="flex flex-col gap-2 rounded-md border p-3 sm:flex-row sm:items-center sm:justify-between"
|
||||
data-testid={`banner-row-${b.id}`}
|
||||
>
|
||||
<div className="min-w-0 space-y-1">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span className="font-medium">{b.title}</span>
|
||||
<Badge variant={statusVariant[status]} data-testid={`banner-status-${b.id}`}>
|
||||
{t(`offerings.banners.status.${status}`)}
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="truncate text-sm text-muted-foreground">{b.text}</p>
|
||||
{(b.startsAt || b.endsAt) && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{fmt(b.startsAt) ?? '…'} – {fmt(b.endsAt) ?? '…'}
|
||||
</p>
|
||||
)}
|
||||
{items.length === 0 ? (
|
||||
<div
|
||||
className="flex flex-col items-center space-y-2 py-8 text-center"
|
||||
data-testid={`banners-empty-${position}`}
|
||||
>
|
||||
<Megaphone className="size-8 text-muted-foreground" aria-hidden="true" />
|
||||
<p className="text-muted-foreground">{t('offerings.banners.empty')}</p>
|
||||
</div>
|
||||
<div className="flex shrink-0 gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
className="size-11"
|
||||
aria-label={t('offerings.banners.editButton')}
|
||||
title={t('offerings.banners.editButton')}
|
||||
data-testid={`banner-edit-${b.id}`}
|
||||
onClick={() => setEditing(b)}
|
||||
>
|
||||
<Pencil className="size-4" aria-hidden="true" />
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
className="size-11"
|
||||
aria-label={t('offerings.banners.deleteButton')}
|
||||
title={t('offerings.banners.deleteButton')}
|
||||
data-testid={`banner-delete-${b.id}`}
|
||||
onClick={() => setPendingDelete(b)}
|
||||
>
|
||||
<Trash2 className="size-4" aria-hidden="true" />
|
||||
</Button>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
) : (
|
||||
<ul className="space-y-2" data-testid={`banners-list-${position}`}>
|
||||
{items.map((b) => {
|
||||
const status = bannerStatus(b);
|
||||
const toggleLabel = t(
|
||||
b.isActive ? 'offerings.banners.turnOffButton' : 'offerings.banners.turnOnButton',
|
||||
);
|
||||
return (
|
||||
<li
|
||||
key={b.id}
|
||||
className="flex flex-col gap-2 rounded-md border p-3 sm:flex-row sm:items-center sm:justify-between"
|
||||
data-testid={`banner-row-${b.id}`}
|
||||
>
|
||||
<div className="min-w-0 space-y-1">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
{b.title && <span className="font-medium">{b.title}</span>}
|
||||
<Badge variant={statusVariant[status]} data-testid={`banner-status-${b.id}`}>
|
||||
{t(`offerings.banners.status.${status}`)}
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="truncate text-sm text-muted-foreground">{b.text}</p>
|
||||
{(b.startsAt || b.endsAt) && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{fmt(b.startsAt) ?? '…'} – {fmt(b.endsAt) ?? '…'}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex shrink-0 gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
className="size-11"
|
||||
aria-label={toggleLabel}
|
||||
title={toggleLabel}
|
||||
disabled={save.isPending}
|
||||
data-testid={`banner-toggle-${b.id}`}
|
||||
onClick={() => toggleActive(b)}
|
||||
>
|
||||
<Power className="size-4" aria-hidden="true" />
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
className="size-11"
|
||||
aria-label={t('offerings.banners.editButton')}
|
||||
title={t('offerings.banners.editButton')}
|
||||
data-testid={`banner-edit-${b.id}`}
|
||||
onClick={() => setEditing(b)}
|
||||
>
|
||||
<Pencil className="size-4" aria-hidden="true" />
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
className="size-11"
|
||||
aria-label={t('offerings.banners.deleteButton')}
|
||||
title={t('offerings.banners.deleteButton')}
|
||||
data-testid={`banner-delete-${b.id}`}
|
||||
onClick={() => setPendingDelete(b)}
|
||||
>
|
||||
<Trash2 className="size-4" aria-hidden="true" />
|
||||
</Button>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
})}
|
||||
|
||||
<OfferingBannerDialog
|
||||
banner={editing}
|
||||
creating={creating}
|
||||
creatingPosition={creating}
|
||||
onClose={() => {
|
||||
setEditing(null);
|
||||
setCreating(false);
|
||||
setCreating(null);
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -115,7 +186,7 @@ export function OfferingBannersTab() {
|
||||
<DialogTitle>{t('offerings.banners.deleteTitle')}</DialogTitle>
|
||||
</DialogHeader>
|
||||
<p className="text-sm text-muted-foreground" data-testid="banner-delete-message">
|
||||
{t('offerings.banners.deleteMessage', { title: pendingDelete?.title ?? '' })}
|
||||
{t('offerings.banners.deleteMessage', { title: pendingDelete?.title || pendingDelete?.text || '' })}
|
||||
</p>
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button variant="outline" onClick={() => setPendingDelete(null)} data-testid="banner-delete-cancel">
|
||||
|
||||
Reference in New Issue
Block a user