H5: admin-scherm voor de hero (formulier, slides, upload, focus-editor met voorbeeld, TypeScript-tweeling van FocusMath)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 16:07:49 +01:00
co-authored by Claude Sonnet 5.5
parent 4369c905cd
commit 5559d459c3
21 changed files with 1635 additions and 15 deletions
@@ -0,0 +1,126 @@
import { http, HttpResponse } from 'msw';
import { API_BASE } from '@/features/auth/mocks/fixtures';
import type { FocusDto, HeroAdminDto, HeroFocusRequest, HeroImageDto, HeroSaveRequest, HeroSlideAdminDto } from '@/features/hero/services/types';
const seedImage = (id: string, width = 1600, height = 1067): HeroImageDto => ({
id,
width,
height,
focus: { x: 0.5, y: 0.5, zoom: 1 },
wideFocus: null,
tallFocus: null,
src: `/media/hero/0123456789abcdef/wide-${width}.webp`,
variants: [
{ ratio: 'wide', width: 960, height: 640, type: 'image/webp', url: '/media/hero/0123456789abcdef/wide-960.webp' },
{ ratio: 'wide', width, height, type: 'image/webp', url: `/media/hero/0123456789abcdef/wide-${width}.webp` },
{ ratio: 'tall', width: 640, height: 800, type: 'image/webp', url: '/media/hero/0123456789abcdef/tall-640.webp' },
],
});
export const emptyHero = (): HeroAdminDto => ({
id: null,
title: '',
tagline: null,
lead: null,
display: 'single',
buttons: [],
isActive: true,
startsAt: null,
endsAt: null,
slides: [],
isLive: false,
updatedAt: null,
maxPublishedSlides: 50,
});
export const slide = (index: number, over: Partial<HeroSlideAdminDto> = {}): HeroSlideAdminDto => ({
id: `00000000-0000-0000-0000-00000000000${index}`,
imageId: `10000000-0000-0000-0000-00000000000${index}`,
alt: `Foto ${index}`,
decorative: false,
isActive: true,
sortOrder: index - 1,
image: seedImage(`10000000-0000-0000-0000-00000000000${index}`),
...over,
});
let hero: HeroAdminDto = emptyHero();
let images: Record<string, HeroImageDto> = {};
let saved: HeroSaveRequest[] = [];
let uploads = 0;
let uploadFailure: { status: number; error: string; message: string } | null = null;
export const resetMockHero = (): void => {
hero = emptyHero();
images = {};
saved = [];
uploads = 0;
uploadFailure = null;
};
export const setMockHero = (next: HeroAdminDto): void => {
hero = next;
images = Object.fromEntries(next.slides.map((s) => [s.imageId, s.image]));
};
export const getSavedHeroRequests = (): HeroSaveRequest[] => saved;
export const failNextUpload = (failure: { status: number; error: string; message: string } | null): void => {
uploadFailure = failure;
};
export const heroHandlers = [
http.get(`${API_BASE}/api/v1/hero/all`, () => HttpResponse.json(hero)),
http.put(`${API_BASE}/api/v1/hero`, async ({ request }) => {
const body = (await request.json()) as HeroSaveRequest;
saved.push(body);
hero = {
...hero,
id: hero.id ?? '99999999-9999-9999-9999-999999999999',
title: body.title,
tagline: body.tagline,
lead: body.lead,
display: body.display,
buttons: body.buttons,
isActive: body.isActive,
startsAt: body.startsAt,
endsAt: body.endsAt,
isLive: body.isActive && body.title.trim() !== '',
updatedAt: '2026-11-16T12:00:00Z',
slides: body.slides.map((s, i) => ({
id: s.id ?? `20000000-0000-0000-0000-00000000000${i}`,
imageId: s.imageId,
alt: s.alt ?? '',
decorative: s.decorative,
isActive: s.isActive,
sortOrder: i,
image: images[s.imageId] ?? seedImage(s.imageId),
})),
};
return HttpResponse.json(hero);
}),
http.post(`${API_BASE}/api/v1/hero/images`, () => {
if (uploadFailure) {
const { status, error, message } = uploadFailure;
uploadFailure = null;
return HttpResponse.json({ error, message }, { status });
}
uploads += 1;
const image = seedImage(`30000000-0000-0000-0000-00000000000${uploads}`);
images[image.id] = image;
return HttpResponse.json(image, { status: 201 });
}),
http.put(`${API_BASE}/api/v1/hero/images/:id/focus`, async ({ params, request }) => {
const body = (await request.json()) as HeroFocusRequest;
const id = String(params.id);
const current = images[id] ?? hero.slides.find((s) => s.imageId === id)?.image ?? seedImage(id);
const asDto = (f: FocusDto | null) => (f ? { ...f } : null);
const next: HeroImageDto = { ...current, focus: body.focus, wideFocus: asDto(body.wide), tallFocus: asDto(body.tall) };
images[id] = next;
hero = { ...hero, slides: hero.slides.map((s) => (s.imageId === id ? { ...s, image: next } : s)) };
return HttpResponse.json(next);
}),
];