Aanbod: categorieën beheren in een dialoog en categorie kiezen in het aanbodformulier
Knop 'Categorieën beheren' op de aanbodlijst opent een dialoog om categorieën toe te voegen, te hernoemen en te verwijderen, met Nederlandse meldingen voor een dubbele naam en een categorie die nog in gebruik is. Het formulier krijgt een keuzelijst met 'Geen categorie', de lijst toont de categorie als badge en de querykey ['offerings','list'] vervangt de oude admin-key zodat een hernoeming direct zichtbaar is. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,320 @@
|
||||
import { useState, type FormEvent } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Check, Pencil, Trash2, X } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { NetworkError, ProblemDetailsError } from '@/lib/api-client';
|
||||
import { categoryNameSchema } from '../schemas/offering';
|
||||
import { useOfferingCategories } from '../services/useOfferingCategories';
|
||||
import { useCreateOfferingCategory } from '../services/useCreateOfferingCategory';
|
||||
import { useRenameOfferingCategory } from '../services/useRenameOfferingCategory';
|
||||
import { useDeleteOfferingCategory } from '../services/useDeleteOfferingCategory';
|
||||
import type { OfferingCategoryDto } from '../services/types';
|
||||
|
||||
interface CategoriesDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages the categories offerings can be placed in. Lives inside the Offerings management
|
||||
* (opened from the list page) rather than on a page of its own.
|
||||
*/
|
||||
export function CategoriesDialog({ open, onOpenChange }: CategoriesDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const { data: categories, isPending, isError } = useOfferingCategories();
|
||||
const createCategory = useCreateOfferingCategory();
|
||||
const renameCategory = useRenameOfferingCategory();
|
||||
const deleteCategory = useDeleteOfferingCategory();
|
||||
|
||||
const [newName, setNewName] = useState('');
|
||||
const [editingId, setEditingId] = useState<string | null>(null);
|
||||
const [editName, setEditName] = useState('');
|
||||
const [deletePendingId, setDeletePendingId] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
function validate(name: string): string | null {
|
||||
const result = categoryNameSchema.safeParse(name);
|
||||
if (result.success) return null;
|
||||
return result.error.issues[0].code === 'too_big'
|
||||
? t('offerings.categories.errors.nameTooLong')
|
||||
: t('offerings.categories.errors.nameRequired');
|
||||
}
|
||||
|
||||
// Server rejections are translated here instead of showing the API's English titles.
|
||||
function describeError(err: unknown, context: { name?: string; usedBy?: number }): string {
|
||||
if (err instanceof NetworkError) return t('errors.network');
|
||||
if (err instanceof ProblemDetailsError && err.status === 409) {
|
||||
return context.usedBy !== undefined
|
||||
? t('offerings.categories.errors.inUse', { count: context.usedBy })
|
||||
: t('offerings.categories.errors.duplicate', { name: context.name ?? '' });
|
||||
}
|
||||
return t('errors.generic');
|
||||
}
|
||||
|
||||
async function handleAdd(event: FormEvent) {
|
||||
event.preventDefault();
|
||||
const trimmed = newName.trim();
|
||||
const problem = validate(trimmed);
|
||||
if (problem) {
|
||||
setError(problem);
|
||||
return;
|
||||
}
|
||||
setError(null);
|
||||
try {
|
||||
await createCategory.mutateAsync(trimmed);
|
||||
setNewName('');
|
||||
} catch (err) {
|
||||
setError(describeError(err, { name: trimmed }));
|
||||
}
|
||||
}
|
||||
|
||||
function startEdit(category: OfferingCategoryDto) {
|
||||
setError(null);
|
||||
setDeletePendingId(null);
|
||||
setEditingId(category.id);
|
||||
setEditName(category.name);
|
||||
}
|
||||
|
||||
function cancelEdit() {
|
||||
setEditingId(null);
|
||||
setError(null);
|
||||
}
|
||||
|
||||
async function confirmEdit(category: OfferingCategoryDto) {
|
||||
const trimmed = editName.trim();
|
||||
const problem = validate(trimmed);
|
||||
if (problem) {
|
||||
setError(problem);
|
||||
return;
|
||||
}
|
||||
if (trimmed === category.name) {
|
||||
cancelEdit();
|
||||
return;
|
||||
}
|
||||
setError(null);
|
||||
try {
|
||||
await renameCategory.mutateAsync({ id: category.id, name: trimmed });
|
||||
setEditingId(null);
|
||||
} catch (err) {
|
||||
setError(describeError(err, { name: trimmed }));
|
||||
}
|
||||
}
|
||||
|
||||
function startDelete(category: OfferingCategoryDto) {
|
||||
setError(null);
|
||||
setEditingId(null);
|
||||
setDeletePendingId(category.id);
|
||||
}
|
||||
|
||||
async function confirmDelete(category: OfferingCategoryDto) {
|
||||
setError(null);
|
||||
try {
|
||||
await deleteCategory.mutateAsync(category.id);
|
||||
setDeletePendingId(null);
|
||||
} catch (err) {
|
||||
setDeletePendingId(null);
|
||||
setError(describeError(err, { usedBy: category.offeringCount }));
|
||||
}
|
||||
}
|
||||
|
||||
function handleOpenChange(next: boolean) {
|
||||
if (!next) {
|
||||
setEditingId(null);
|
||||
setDeletePendingId(null);
|
||||
setError(null);
|
||||
setNewName('');
|
||||
}
|
||||
onOpenChange(next);
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleOpenChange}>
|
||||
<DialogContent
|
||||
className="sm:max-w-lg"
|
||||
data-testid="categories-dialog"
|
||||
onEscapeKeyDown={(event) => {
|
||||
// Escape while renaming only cancels the rename, it does not close the dialog.
|
||||
if (editingId !== null) {
|
||||
event.preventDefault();
|
||||
cancelEdit();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t('offerings.categories.title')}</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
{error !== null && (
|
||||
<p
|
||||
role="alert"
|
||||
className="text-sm text-destructive"
|
||||
data-testid="categories-error"
|
||||
>
|
||||
{error}
|
||||
</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 && categories.length === 0 && (
|
||||
<p className="text-sm text-muted-foreground" data-testid="categories-empty">
|
||||
{t('offerings.categories.emptyState')}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{!isPending && !isError && categories.length > 0 && (
|
||||
<ul className="divide-y rounded-md border" data-testid="categories-list">
|
||||
{categories.map((category) => (
|
||||
<li
|
||||
key={category.id}
|
||||
className="flex items-center justify-between gap-2 px-3 py-2"
|
||||
data-testid={`category-row-${category.id}`}
|
||||
>
|
||||
{editingId === category.id ? (
|
||||
<>
|
||||
<Input
|
||||
autoFocus
|
||||
className="h-8"
|
||||
value={editName}
|
||||
aria-label={t('offerings.categories.editButton')}
|
||||
data-testid={`category-edit-input-${category.id}`}
|
||||
onChange={(e) => setEditName(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
void confirmEdit(category);
|
||||
} else if (e.key === 'Escape') {
|
||||
e.stopPropagation();
|
||||
cancelEdit();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<div className="flex gap-1">
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
aria-label={t(
|
||||
'offerings.categories.confirmEditButton',
|
||||
)}
|
||||
data-testid={`category-edit-confirm-${category.id}`}
|
||||
onClick={() => void confirmEdit(category)}
|
||||
>
|
||||
<Check className="size-4" />
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
aria-label={t('offerings.categories.cancelButton')}
|
||||
data-testid={`category-edit-cancel-${category.id}`}
|
||||
onClick={cancelEdit}
|
||||
>
|
||||
<X className="size-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="min-w-0">
|
||||
<span
|
||||
className="font-medium"
|
||||
data-testid={`category-name-${category.id}`}
|
||||
>
|
||||
{category.name}
|
||||
</span>
|
||||
<span
|
||||
className="ml-2 text-sm text-muted-foreground"
|
||||
data-testid={`category-count-${category.id}`}
|
||||
>
|
||||
{t('offerings.categories.offeringCount', {
|
||||
count: category.offeringCount,
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{deletePendingId === category.id ? (
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<span>
|
||||
{t('offerings.categories.deleteConfirm')}
|
||||
</span>
|
||||
<Button
|
||||
type="button"
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
disabled={deleteCategory.isPending}
|
||||
data-testid={`category-delete-confirm-${category.id}`}
|
||||
onClick={() => void confirmDelete(category)}
|
||||
>
|
||||
{t('offerings.categories.deleteConfirmButton')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
data-testid={`category-delete-cancel-${category.id}`}
|
||||
onClick={() => setDeletePendingId(null)}
|
||||
>
|
||||
{t('offerings.categories.cancelButton')}
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex gap-1">
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
aria-label={t(
|
||||
'offerings.categories.editButton',
|
||||
)}
|
||||
data-testid={`category-edit-${category.id}`}
|
||||
onClick={() => startEdit(category)}
|
||||
>
|
||||
<Pencil className="size-4" />
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
aria-label={t(
|
||||
'offerings.categories.deleteButton',
|
||||
)}
|
||||
data-testid={`category-delete-${category.id}`}
|
||||
onClick={() => startDelete(category)}
|
||||
>
|
||||
<Trash2 className="size-4" />
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleAdd} className="flex gap-2" noValidate>
|
||||
<Input
|
||||
value={newName}
|
||||
placeholder={t('offerings.categories.newLabel')}
|
||||
aria-label={t('offerings.categories.newLabel')}
|
||||
data-testid="category-new-input"
|
||||
onChange={(e) => setNewName(e.target.value)}
|
||||
/>
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={createCategory.isPending}
|
||||
data-testid="category-add-button"
|
||||
>
|
||||
{t('offerings.categories.addButton')}
|
||||
</Button>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useForm, useWatch } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
@@ -7,6 +7,8 @@ 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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||
import { useOfferingCategories } from '../services/useOfferingCategories';
|
||||
import { slugifyPreview } from '@/lib/slugify';
|
||||
import { offeringFormSchema, type OfferingFormData } from '../schemas/offering';
|
||||
import type { OfferingDto } from '../services/types';
|
||||
@@ -39,9 +41,32 @@ export function OfferingForm({ initialValues, onSubmit, isSubmitting }: Offering
|
||||
ctaLabel: '',
|
||||
featured: false,
|
||||
slug: '',
|
||||
categoryId: null,
|
||||
},
|
||||
});
|
||||
|
||||
// Radix Select does not allow an empty value, so "no category" is the sentinel 'none' here
|
||||
// and is mapped back to null for the form value.
|
||||
const NO_CATEGORY = 'none';
|
||||
const { data: categories, isFetching: isFetchingCategories } = useOfferingCategories();
|
||||
const categoryId = useWatch({ control, name: 'categoryId' });
|
||||
const selectedCategory =
|
||||
categoryId != null && categories?.some((c) => c.id === categoryId) ? categoryId : NO_CATEGORY;
|
||||
|
||||
// A category that no longer exists (deleted meanwhile) falls back to "no category" so the
|
||||
// form never submits an id the API would reject. Only judged on fresh data: a cached list
|
||||
// that is being refetched may still be missing a category that does exist.
|
||||
useEffect(() => {
|
||||
if (
|
||||
categories !== undefined &&
|
||||
!isFetchingCategories &&
|
||||
categoryId != null &&
|
||||
!categories.some((c) => c.id === categoryId)
|
||||
) {
|
||||
setValue('categoryId', null);
|
||||
}
|
||||
}, [categories, isFetchingCategories, categoryId, setValue]);
|
||||
|
||||
// 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' });
|
||||
@@ -177,6 +202,37 @@ export function OfferingForm({ initialValues, onSubmit, isSubmitting }: Offering
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="offering-category">{t('offerings.form.categoryLabel')}</Label>
|
||||
<Select
|
||||
value={selectedCategory}
|
||||
onValueChange={(value) => {
|
||||
// Radix reports an empty value while its options are still loading (the
|
||||
// chosen category is not in the list yet); that is not a user choice.
|
||||
if (value === '') return;
|
||||
setValue('categoryId', value === NO_CATEGORY ? null : value, { shouldDirty: true });
|
||||
}}
|
||||
>
|
||||
<SelectTrigger id="offering-category" data-testid="offering-category">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={NO_CATEGORY} data-testid="offering-category-none">
|
||||
{t('offerings.form.noCategory')}
|
||||
</SelectItem>
|
||||
{categories?.map((category) => (
|
||||
<SelectItem
|
||||
key={category.id}
|
||||
value={category.id}
|
||||
data-testid={`offering-category-option-${category.id}`}
|
||||
>
|
||||
{category.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>{t('offerings.form.featuresLabel')}</Label>
|
||||
{features.map((_, index) => (
|
||||
|
||||
@@ -3,6 +3,7 @@ import { CSS } from '@dnd-kit/utilities';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { GripVertical, Star, ChevronUp, ChevronDown, Pencil, Trash2 } from 'lucide-react';
|
||||
import { Link } from '@tanstack/react-router';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { TableCell, TableRow } from '@/components/ui/table';
|
||||
import { useUpdateOffering } from '../services/useUpdateOffering';
|
||||
@@ -45,7 +46,14 @@ export function OfferingRow({ offering, isFirst, isLast, onDeleteRequested }: Of
|
||||
<GripVertical className="size-4" />
|
||||
</button>
|
||||
</TableCell>
|
||||
<TableCell>{offering.title}</TableCell>
|
||||
<TableCell>
|
||||
{offering.title}
|
||||
{offering.category && (
|
||||
<Badge variant="secondary" className="ml-2" data-testid={`offering-row-${offering.id}-category`}>
|
||||
{offering.category}
|
||||
</Badge>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell>{offering.price}</TableCell>
|
||||
<TableCell>
|
||||
<Button
|
||||
@@ -62,6 +70,8 @@ export function OfferingRow({ offering, isFirst, isLast, onDeleteRequested }: Of
|
||||
priceNote: offering.priceNote,
|
||||
features: offering.features,
|
||||
ctaLabel: offering.ctaLabel,
|
||||
// PUT replaces the whole offering: leaving the category out would clear it.
|
||||
categoryId: offering.categoryId ?? null,
|
||||
featured: !offering.featured,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user