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:
@@ -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) => (
|
||||
|
||||
Reference in New Issue
Block a user