Offerings: banner-tabblad in de admin (lijst, formulier, planning), layout uit de frontend; gate-test bijgewerkt

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 11:04:50 +01:00
co-authored by Claude Sonnet 5.5
parent a2f4513205
commit 5386abb9ef
16 changed files with 1867 additions and 1178 deletions
@@ -0,0 +1,190 @@
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { useTranslation } from 'react-i18next';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { FieldError } from '@/components/ui/FieldError';
import { FormErrorBanner } from '@/components/ui/FormErrorBanner';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { NetworkError } from '@/lib/api-client';
import {
fromLocalInput,
offeringBannerFormSchema,
toLocalInput,
type OfferingBannerFormData,
} from '../schemas/offeringBanner';
import { useSaveOfferingBanner } from '../services/useSaveOfferingBanner';
import type { OfferingBannerDto } from '../services/types';
interface OfferingBannerDialogProps {
/** Open for editing this banner; null with `creating` = a new one. */
banner: OfferingBannerDto | null;
creating: boolean;
onClose: () => void;
}
const EMPTY: OfferingBannerFormData = {
title: '',
text: '',
linkLabel: '',
linkUrl: '',
isActive: true,
startsAt: '',
endsAt: '',
};
function toForm(banner: OfferingBannerDto | null): OfferingBannerFormData {
if (!banner) return EMPTY;
return {
title: banner.title,
text: banner.text,
linkLabel: banner.linkLabel ?? '',
linkUrl: banner.linkUrl ?? '',
isActive: banner.isActive,
startsAt: toLocalInput(banner.startsAt),
endsAt: toLocalInput(banner.endsAt),
};
}
export function OfferingBannerDialog({ banner, creating, onClose }: OfferingBannerDialogProps) {
const { t } = useTranslation();
const open = creating || banner !== null;
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')}
</DialogTitle>
</DialogHeader>
{/* Content is unmounted while closed, so every opening starts from fresh form state. */}
<OfferingBannerForm banner={banner} onClose={onClose} />
</DialogContent>
</Dialog>
);
}
function OfferingBannerForm({ banner, onClose }: { banner: OfferingBannerDto | null; onClose: () => void }) {
const { t } = useTranslation();
const [serverError, setServerError] = useState<string | null>(null);
const save = useSaveOfferingBanner();
const {
register,
handleSubmit,
formState: { errors },
} = useForm<OfferingBannerFormData>({
resolver: zodResolver(offeringBannerFormSchema),
mode: 'onTouched',
defaultValues: toForm(banner),
});
const onSubmit = async (values: OfferingBannerFormData) => {
setServerError(null);
try {
await save.mutateAsync({
id: banner?.id,
body: {
title: values.title,
text: values.text,
linkLabel: values.linkLabel || null,
linkUrl: values.linkUrl || null,
isActive: values.isActive,
startsAt: fromLocalInput(values.startsAt),
endsAt: fromLocalInput(values.endsAt),
},
});
toast.success(t('offerings.banners.savedToast'));
onClose();
} catch (err) {
setServerError(err instanceof NetworkError ? t('errors.network') : t('errors.generic'));
}
};
return (
<>
<FormErrorBanner
error={serverError !== null ? { message: serverError } : null}
onDismiss={() => setServerError(null)}
/>
<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>
<Input id="banner-title" data-testid="banner-title" maxLength={100} {...register('title')} />
<FieldError message={errors.title?.message} testId="banner-title-error" />
</div>
<div className="space-y-2">
<Label htmlFor="banner-text">{t('offerings.banners.textLabel')}</Label>
<textarea
id="banner-text"
data-testid="banner-text"
rows={3}
maxLength={300}
className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
{...register('text')}
/>
<FieldError message={errors.text?.message} testId="banner-text-error" />
</div>
<div className="grid gap-4 sm:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="banner-link-label">{t('offerings.banners.linkLabelLabel')}</Label>
<Input id="banner-link-label" data-testid="banner-link-label" maxLength={50} {...register('linkLabel')} />
<FieldError message={errors.linkLabel?.message} testId="banner-link-label-error" />
</div>
<div className="space-y-2">
<Label htmlFor="banner-link-url">{t('offerings.banners.linkUrlLabel')}</Label>
<Input
id="banner-link-url"
data-testid="banner-link-url"
maxLength={500}
placeholder="https://… of /pad"
{...register('linkUrl')}
/>
<FieldError message={errors.linkUrl?.message} testId="banner-link-url-error" />
</div>
</div>
<p className="text-xs text-muted-foreground">{t('offerings.banners.linkHint')}</p>
<div className="grid gap-4 sm:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="banner-starts">{t('offerings.banners.startsLabel')}</Label>
<Input id="banner-starts" type="datetime-local" data-testid="banner-starts" {...register('startsAt')} />
</div>
<div className="space-y-2">
<Label htmlFor="banner-ends">{t('offerings.banners.endsLabel')}</Label>
<Input id="banner-ends" type="datetime-local" data-testid="banner-ends" {...register('endsAt')} />
<FieldError message={errors.endsAt?.message} testId="banner-ends-error" />
</div>
</div>
<p className="text-xs text-muted-foreground">{t('offerings.banners.scheduleHint')}</p>
<div className="flex items-center gap-2">
<input
id="banner-active"
type="checkbox"
data-testid="banner-active"
className="size-4 rounded border-input"
{...register('isActive')}
/>
<Label htmlFor="banner-active">{t('offerings.banners.activeLabel')}</Label>
</div>
<div className="flex justify-end gap-2">
<Button type="button" variant="outline" onClick={onClose} data-testid="banner-cancel">
{t('common.cancel')}
</Button>
<Button type="submit" disabled={save.isPending} data-testid="banner-submit">
{save.isPending ? '…' : t('common.save')}
</Button>
</div>
</form>
</>
);
}
@@ -0,0 +1,146 @@
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Megaphone, Pencil, Plus, 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 { bannerStatus } from '../schemas/offeringBanner';
import { OfferingBannerDialog } from './OfferingBannerDialog';
import type { OfferingBannerDto } from '../services/types';
const fmt = (iso: string | null) =>
iso ? new Date(iso).toLocaleString('nl-NL', { dateStyle: 'short', timeStyle: 'short' }) : null;
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 [pendingDelete, setPendingDelete] = useState<OfferingBannerDto | null>(null);
const remove = useDeleteOfferingBanner();
const statusVariant = { live: 'default', scheduled: 'secondary', expired: 'outline', off: 'outline' } as const;
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>
{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>
)}
{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>
)}
</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>
)}
<OfferingBannerDialog
banner={editing}
creating={creating}
onClose={() => {
setEditing(null);
setCreating(false);
}}
/>
<Dialog open={pendingDelete !== null} onOpenChange={(open) => { if (!open) setPendingDelete(null); }}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<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 ?? '' })}
</p>
<div className="flex justify-end gap-2">
<Button variant="outline" onClick={() => setPendingDelete(null)} data-testid="banner-delete-cancel">
{t('common.cancel')}
</Button>
<Button
variant="destructive"
disabled={remove.isPending}
data-testid="banner-delete-confirm"
onClick={() => {
if (!pendingDelete) return;
remove.mutate(pendingDelete.id, {
onSuccess: () => {
setPendingDelete(null);
toast.success(t('offerings.banners.deletedToast'));
},
onError: () => toast.error(t('errors.generic')),
});
}}
>
{remove.isPending ? '…' : t('offerings.banners.deleteConfirm')}
</Button>
</div>
</DialogContent>
</Dialog>
</div>
);
}
@@ -42,7 +42,6 @@ export function OfferingForm({ initialValues, onSubmit, isSubmitting }: Offering
featured: false,
slug: '',
categoryId: null,
layout: 'card',
},
});
@@ -51,7 +50,6 @@ export function OfferingForm({ initialValues, onSubmit, isSubmitting }: Offering
const NO_CATEGORY = 'none';
const { data: categories, isFetching: isFetchingCategories } = useOfferingCategories();
const categoryId = useWatch({ control, name: 'categoryId' });
const layout = useWatch({ control, name: 'layout' });
const selectedCategory =
categoryId != null && categories?.some((c) => c.id === categoryId) ? categoryId : NO_CATEGORY;
@@ -293,24 +291,6 @@ export function OfferingForm({ initialValues, onSubmit, isSubmitting }: Offering
<Label htmlFor="offering-featured">{t('offerings.form.featuredLabel')}</Label>
</div>
<div className="space-y-2">
<Label htmlFor="offering-layout">{t('offerings.form.layoutLabel')}</Label>
<select
id="offering-layout"
data-testid="offering-layout"
className="h-11 w-full rounded-md border border-input bg-background px-3 text-sm"
{...register('layout')}
>
<option value="card">{t('offerings.form.layoutCard')}</option>
<option value="banner">{t('offerings.form.layoutBanner')}</option>
</select>
{layout === 'banner' && (
<p className="text-sm text-muted-foreground" data-testid="offering-layout-banner-hint">
{t('offerings.form.layoutBannerHint')}
</p>
)}
</div>
<Button type="submit" disabled={isSubmitting} data-testid="offering-form-submit">
{isSubmitting ? '…' : t('offerings.form.submitButton')}
</Button>