Maak slug in CMS zichtbaar en inline aanpasbaar
De CMS-admin toonde het nieuwe Slug-veld nog niet — dat bestond alleen in de API-respons. Voegt een read-only slugweergave onder het titelveld toe met een potlood-icoon om 'm inline te bewerken. Backend: Create/UpdateOfferingRequest accepteren nu een optionele Slug-override. Zonder expliciete waarde blijft het bestaande gedrag (auto-genereren uit titel, regenereren bij titelwijziging). Met een expliciete waarde wordt die genormaliseerd en op uniekheid gecontroleerd; een botsing geeft 409 Conflict (RFC 9457 ProblemDetails, naar het patroon van MasterControlledAvailabilityException). Frontend: de slug wordt alleen meegestuurd als de admin 'm daadwerkelijk bewerkt heeft (dirtyFields.slug), zodat een ongemoeide slug bij een titelwijziging gewoon blijft auto-regenereren. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { useState } from 'react';
|
||||
import { useForm, useWatch } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Plus, Trash2 } from 'lucide-react';
|
||||
import { Check, Pencil, Plus, Trash2, X } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
@@ -23,7 +24,8 @@ export function OfferingForm({ initialValues, onSubmit, isSubmitting }: Offering
|
||||
control,
|
||||
handleSubmit,
|
||||
setValue,
|
||||
formState: { errors },
|
||||
resetField,
|
||||
formState: { errors, dirtyFields },
|
||||
} = useForm<OfferingFormData>({
|
||||
resolver: zodResolver(offeringFormSchema),
|
||||
mode: 'onTouched',
|
||||
@@ -35,12 +37,35 @@ export function OfferingForm({ initialValues, onSubmit, isSubmitting }: Offering
|
||||
features: [''],
|
||||
ctaLabel: '',
|
||||
featured: false,
|
||||
slug: '',
|
||||
},
|
||||
});
|
||||
|
||||
// useFieldArray requires object-shaped array items — features is a plain string[],
|
||||
// so add/remove are handled directly via setValue instead.
|
||||
const features = useWatch({ control, name: 'features' });
|
||||
const slug = useWatch({ control, name: 'slug' });
|
||||
const [isEditingSlug, setIsEditingSlug] = useState(false);
|
||||
|
||||
function confirmSlugEdit() {
|
||||
setIsEditingSlug(false);
|
||||
}
|
||||
|
||||
function cancelSlugEdit() {
|
||||
resetField('slug');
|
||||
setIsEditingSlug(false);
|
||||
}
|
||||
|
||||
// Slug is auto-generated from the title server-side unless explicitly overridden here —
|
||||
// only send it along when the admin actually touched this field, so an untouched slug
|
||||
// keeps auto-regenerating when the title changes (see OfferingsService.UpdateAsync).
|
||||
function submitWithSlugPolicy(values: OfferingFormData) {
|
||||
const payload = { ...values };
|
||||
if (!dirtyFields.slug) {
|
||||
delete payload.slug;
|
||||
}
|
||||
onSubmit(payload);
|
||||
}
|
||||
|
||||
function addFeature() {
|
||||
setValue('features', [...features, ''], { shouldValidate: true });
|
||||
@@ -51,13 +76,68 @@ export function OfferingForm({ initialValues, onSubmit, isSubmitting }: Offering
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit(onSubmit)} noValidate className="space-y-4">
|
||||
<form onSubmit={handleSubmit(submitWithSlugPolicy)} noValidate className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="offering-title">{t('offerings.form.titleLabel')}</Label>
|
||||
<Input id="offering-title" data-testid="offering-title" aria-invalid={errors.title !== undefined} {...register('title')} />
|
||||
{errors.title && <FieldError message={errors.title.message} testId="offering-title-error" />}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="offering-slug">{t('offerings.form.slugLabel')}</Label>
|
||||
{isEditingSlug ? (
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
id="offering-slug"
|
||||
data-testid="offering-slug"
|
||||
autoFocus
|
||||
aria-invalid={errors.slug !== undefined}
|
||||
{...register('slug')}
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
data-testid="offering-slug-confirm"
|
||||
aria-label={t('offerings.form.confirmSlugButton')}
|
||||
onClick={confirmSlugEdit}
|
||||
>
|
||||
<Check className="size-4" />
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
data-testid="offering-slug-cancel"
|
||||
aria-label={t('offerings.form.cancelSlugEditButton')}
|
||||
onClick={cancelSlugEdit}
|
||||
>
|
||||
<X className="size-4" />
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center gap-2">
|
||||
<code
|
||||
data-testid="offering-slug-display"
|
||||
className="font-mono text-sm text-muted-foreground"
|
||||
>
|
||||
{slug && slug.length > 0 ? slug : t('offerings.form.slugAutoHint')}
|
||||
</code>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
data-testid="offering-slug-edit"
|
||||
aria-label={t('offerings.form.editSlugButton')}
|
||||
onClick={() => setIsEditingSlug(true)}
|
||||
>
|
||||
<Pencil className="size-4" />
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
{errors.slug && <FieldError message={errors.slug.message} testId="offering-slug-error" />}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="offering-description">{t('offerings.form.descriptionLabel')}</Label>
|
||||
<textarea
|
||||
|
||||
Reference in New Issue
Block a user