Opslaan van 8 foto's met gewijzigde focus duurde 40 s op een snelle pc en ruim 3 minuten op de Pi (proxy-timeout 60 s gaf 504, hero niet bewaard). Upload bewaart het origineel en antwoordt; HeroRenderService knipt de set per afbeelding en schakelt over zodra de set compleet is (oude set blijft ondertussen live; afbeelding zonder set wordt niet gepubliceerd). Opslaan legt alleen de focus vast. Admin toont 'wordt verwerkt' en ververst zelf; Seed-Hero wacht tot alles klaar is. Metingen: HeroSaveTimingTests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
122 lines
4.3 KiB
TypeScript
122 lines
4.3 KiB
TypeScript
import { http, HttpResponse } from 'msw';
|
|
import { API_BASE } from '@/features/auth/mocks/fixtures';
|
|
import type { HeroAdminDto, 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' },
|
|
],
|
|
processing: false,
|
|
});
|
|
|
|
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);
|
|
// A changed focus takes effect with the save of the hero.
|
|
for (const edit of body.imageFocus ?? []) {
|
|
const current = images[edit.imageId] ?? hero.slides.find((s) => s.imageId === edit.imageId)?.image ?? seedImage(edit.imageId);
|
|
images[edit.imageId] = { ...current, focus: edit.focus, wideFocus: edit.wide, tallFocus: edit.tall };
|
|
}
|
|
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 });
|
|
}),
|
|
];
|