Mediabibliotheek en Hero: kopie-schakelaar in de deploy-workflow (test), kiezer enkel en meervoud, hint bij gedeelde uitsnede, plan uitsnede per gebruik
Continuous Integration / config-sls (pull_request) Successful in 8s
Continuous Integration / config-asf (pull_request) Successful in 6s
Continuous Integration / changes (pull_request) Successful in 13s
Continuous Integration / backend-build (pull_request) Successful in 1m53s
Continuous Integration / compile-sites (pull_request) Successful in 1m54s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m56s
Continuous Integration / frontend-prepare (pull_request) Successful in 2m24s
Continuous Integration / frontend-build (pull_request) Successful in 35s
Continuous Integration / frontend-lint (pull_request) Successful in 38s
Continuous Integration / backend-test (pull_request) Successful in 4m18s
Continuous Integration / frontend-test (pull_request) Successful in 2m35s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 17:20:09 +01:00
co-authored by Claude Sonnet 5.5
parent e09304c2e9
commit 9bc4153c70
11 changed files with 228 additions and 23 deletions
@@ -1282,6 +1282,17 @@ describe('Choose a photo from the library for a slide', () => {
expect(getSavedHeroRequests()[0].slides.map((x) => x.imageId)).toEqual(['10000000-0000-0000-0000-000000000002', '10000000-0000-0000-0000-000000000002']);
});
it('tells that the crop is shared when the same photo is in more than one slide', async () => {
const one = slide(1);
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [one, { ...slide(2), imageId: one.imageId, image: one.image }, slide(3)] });
await openPage();
expect(screen.getByTestId('hero-slide-0-shared-crop')).toBeInTheDocument();
expect(screen.getByTestId('hero-slide-1-shared-crop')).toBeInTheDocument();
expect(screen.queryByTestId('hero-slide-2-shared-crop')).not.toBeInTheDocument();
});
it('uploads a new photo on the spot with the hero upload and puts it in the slide', async () => {
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
server.use(http.get('*/media-library/photos', () => HttpResponse.json({ photos: [], modules: ['hero'] })));
@@ -574,6 +574,11 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
<Button type="button" variant="outline" size="sm" onClick={() => setLibraryFor(index)} data-testid={`hero-slide-${index}-library`}>
{t('hero.library.choose')}
</Button>
{watchedSlides.some((other, otherIndex) => otherIndex !== index && other?.imageId === field.imageId) && (
<p className="max-w-28 text-xs text-muted-foreground" data-testid={`hero-slide-${index}-shared-crop`}>
{t('hero.library.sharedCrop')}
</p>
)}
</div>
<div className="min-w-[12rem] flex-1 space-y-2" role="group" aria-labelledby={`hero-slide-title-${field.id}`}>
<h3 id={`hero-slide-title-${field.id}`} className="text-xs font-medium uppercase tracking-wide text-muted-foreground" data-testid={`hero-slide-${index}-title`}>
@@ -88,3 +88,57 @@ describe('ImagePicker', () => {
expect(screen.getByTestId('picker-empty')).toHaveTextContent('nog geen foto');
});
});
function MultiHarness({ onUpload, initial = [] }: { onUpload?: (file: File) => Promise<PhotoItem>; initial?: string[] }) {
const [values, setValues] = useState<string[]>(initial);
return (
<>
<ImagePicker mode="multiple" items={items} values={values} onChangeMany={setValues} onUpload={onUpload} maxUploadBytes={10 * MB} label="Foto's van de galerij" />
<output data-testid="chosen">{values.join(',')}</output>
</>
);
}
describe('ImagePicker in multiple mode', () => {
it('chooses several photos with checkboxes, in the order chosen, and unchooses one', async () => {
render(<MultiHarness />);
expect(screen.getByRole('group', { name: "Foto's van de galerij" })).toBeInTheDocument();
await userEvent.click(screen.getByRole('checkbox', { name: 'Foto B' }));
await userEvent.click(screen.getByRole('checkbox', { name: 'Foto A' }));
expect(screen.getByTestId('chosen')).toHaveTextContent('b,a');
await userEvent.click(screen.getByRole('checkbox', { name: 'Foto B' }));
expect(screen.getByTestId('chosen')).toHaveTextContent('a');
expect(screen.getByRole('checkbox', { name: 'Foto B' })).not.toBeChecked();
});
it('uploads several files on the spot and adds them all to the choice', async () => {
let n = 0;
const upload = vi.fn().mockImplementation(async () => photo(`new${++n}`, `Nieuw ${n}`));
render(<MultiHarness onUpload={upload} initial={['a']} />);
await userEvent.upload(screen.getByTestId('picker-file'), [file('1.jpg'), file('2.jpg')]);
await waitFor(() => expect(screen.getByTestId('chosen')).toHaveTextContent('a,new1,new2'));
expect(upload).toHaveBeenCalledTimes(2);
expect(screen.getByTestId('picker-file')).toHaveAttribute('multiple');
});
it('stops at a file the host would refuse and keeps what was chosen', async () => {
const upload = vi.fn().mockResolvedValue(photo('new1', 'Nieuw'));
render(<MultiHarness onUpload={upload} />);
await userEvent.upload(screen.getByTestId('picker-file'), [file('ok.jpg'), file('big.jpg', 'image/jpeg', 10 * MB + 1), file('later.jpg')]);
await waitFor(() => expect(screen.getByTestId('picker-problem')).toHaveTextContent('maximaal 10 MB'));
expect(upload).toHaveBeenCalledTimes(1);
expect(screen.getByTestId('chosen')).toHaveTextContent('new1');
});
it('single mode still takes one file only and has no multiple attribute', () => {
render(<Harness onUpload={vi.fn()} />);
expect(screen.getByTestId('picker-file')).not.toHaveAttribute('multiple');
});
});
@@ -6,12 +6,9 @@ import { cn } from '@/lib/utils';
import { validateUpload } from '../lib/validateUpload';
import type { PhotoItem } from './PhotoSelectionList';
interface ImagePickerProps {
interface ImagePickerCommon {
/** The photos of the library to choose from, newest first. */
items: readonly PhotoItem[];
/** The id of the chosen photo, or null. */
value: string | null;
onChange: (id: string) => void;
/**
* Uploads one file and answers with the photo as the library lists it. The module decides how (the hero uses its own
* upload and background processing); the picker only shows the state and chooses the result. Without it there is no
@@ -26,30 +23,71 @@ interface ImagePickerProps {
label: string;
}
/** One photo: a radio group (the hero slides use this). */
export interface ImagePickerSingleProps extends ImagePickerCommon {
mode?: 'single';
/** The id of the chosen photo, or null. */
value: string | null;
onChange: (id: string) => void;
}
/** Several photos: checkboxes (ready for the gallery). Uploaded photos are added to the choice. */
export interface ImagePickerMultipleProps extends ImagePickerCommon {
mode: 'multiple';
/** The ids of the chosen photos, in the order they were chosen. */
values: readonly string[];
onChangeMany: (ids: string[]) => void;
}
export type ImagePickerProps = ImagePickerSingleProps | ImagePickerMultipleProps;
/**
* "Choose an image or upload one": the photos of the library as a group of radio buttons (arrow keys move and choose)
* and an upload button that adds the new photo to the library and chooses it at once. One component for every module that
* needs an image. It holds no data of its own and calls nothing by itself: the module passes the list and the upload.
* "Choose an image or upload one", one component for every module that needs an image. In <c>single</c> mode the library
* photos are a radio group (arrow keys move and choose); in <c>multiple</c> mode they are checkboxes and several photos can
* be chosen. An upload button adds the new photo to the library and chooses it at once. The picker holds no data of its own
* and calls nothing by itself: the module passes the list and the upload.
*/
export function ImagePicker({ items, value, onChange, onUpload, maxUploadBytes, limitsText, label }: ImagePickerProps) {
export function ImagePicker(props: ImagePickerProps) {
const { items, onUpload, maxUploadBytes, limitsText, label } = props;
const multiple = props.mode === 'multiple';
const { t } = useTranslation();
const groupName = useId();
const fileInput = useRef<HTMLInputElement>(null);
const [uploading, setUploading] = useState(false);
const [problem, setProblem] = useState<string | null>(null);
const upload = async (file: File | undefined) => {
if (!file || !onUpload || uploading) return;
const refused = validateUpload(file, maxUploadBytes);
if (refused) {
setProblem(refused === 'tooLarge' ? t('media.picker.tooLarge', { max: Math.round(maxUploadBytes / (1024 * 1024)) }) : t('media.picker.notAnImage'));
return;
const chosen = (id: string) => (props.mode === 'multiple' ? props.values.includes(id) : props.value === id);
const toggle = (id: string) => {
if (props.mode === 'multiple') {
props.onChangeMany(props.values.includes(id) ? props.values.filter((v) => v !== id) : [...props.values, id]);
} else {
props.onChange(id);
}
};
const upload = async (files: FileList | null) => {
const list = Array.from(files ?? []);
if (list.length === 0 || !onUpload || uploading) return;
setProblem(null);
setUploading(true);
let current = props.mode === 'multiple' ? [...props.values] : [];
try {
const photo = await onUpload(file);
onChange(photo.id);
for (const file of multiple ? list : list.slice(0, 1)) {
const refused = validateUpload(file, maxUploadBytes);
if (refused) {
setProblem(refused === 'tooLarge' ? t('media.picker.tooLarge', { max: Math.round(maxUploadBytes / (1024 * 1024)) }) : t('media.picker.notAnImage'));
break;
}
const photo = await onUpload(file);
if (props.mode === 'multiple') {
current = [...current, photo.id];
props.onChangeMany(current);
} else {
props.onChange(photo.id);
}
}
} catch {
setProblem(t('media.picker.failed'));
} finally {
@@ -66,10 +104,11 @@ export function ImagePicker({ items, value, onChange, onUpload, maxUploadBytes,
ref={fileInput}
type="file"
accept="image/*"
multiple={multiple}
className="sr-only"
aria-label={t('media.picker.upload')}
data-testid="picker-file"
onChange={(event) => void upload(event.target.files?.[0])}
onChange={(event) => void upload(event.target.files)}
/>
<Button type="button" variant="outline" disabled={uploading} onClick={() => fileInput.current?.click()} data-testid="picker-upload-button">
<ImagePlus className="mr-2 size-4" aria-hidden="true" />
@@ -88,15 +127,15 @@ export function ImagePicker({ items, value, onChange, onUpload, maxUploadBytes,
{t('media.picker.empty')}
</p>
) : (
<div role="radiogroup" aria-label={label} className="grid grid-cols-2 gap-3 sm:grid-cols-4" data-testid="picker-grid">
<div role={multiple ? 'group' : 'radiogroup'} aria-label={label} className="grid grid-cols-2 gap-3 sm:grid-cols-4" data-testid="picker-grid">
{items.map((item) => (
<label key={item.id} className="relative block cursor-pointer" data-testid={`picker-item-${item.id}`}>
<input
type="radio"
type={multiple ? 'checkbox' : 'radio'}
name={groupName}
value={item.id}
checked={value === item.id}
onChange={() => onChange(item.id)}
checked={chosen(item.id)}
onChange={() => toggle(item.id)}
aria-label={item.name}
className="peer sr-only"
/>
@@ -647,7 +647,8 @@
"choose": "Choose from library",
"title": "Image for slide {{n}}",
"description": "Choose a photo from the media library or upload a new one. A photo that is already in another slide is fine too: it is the same photo, so its crop is shared.",
"group": "Image of slide {{n}}"
"group": "Image of slide {{n}}",
"sharedCrop": "This photo is in more slides; the crop applies to all of them."
},
"slides": {
"title": "Images",
@@ -647,7 +647,8 @@
"choose": "Kies uit bibliotheek",
"title": "Afbeelding voor slide {{n}}",
"description": "Kies een foto uit de mediabibliotheek of upload een nieuwe foto. Een foto die al in een andere slide staat mag ook: het is dezelfde foto, dus de uitsnede is gedeeld.",
"group": "Afbeelding van slide {{n}}"
"group": "Afbeelding van slide {{n}}",
"sharedCrop": "Deze foto staat in meer slides; de uitsnede geldt voor allemaal."
},
"slides": {
"title": "Afbeeldingen",