Files
slp-modular-cms/frontend/src/features/hero/mocks/handlers.ts
T

189 lines
7.9 KiB
TypeScript

import { http, HttpResponse } from 'msw';
import { API_BASE } from '@/features/auth/mocks/fixtures';
import type { 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' },
],
processing: false,
failed: false,
previewSrc: '/media/hero/0123456789abcdef/full-1280.webp',
});
export const emptyHero = (): HeroAdminDto => ({
id: null,
title: '',
tagline: null,
lead: null,
display: 'single',
buttons: [],
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;
retried = [];
focusRequests = [];
resetSlideRequests = [];
recutting = new Set();
};
export const setMockHero = (next: HeroAdminDto): void => {
hero = next;
images = Object.fromEntries(next.slides.map((s) => [s.imageId, s.image]));
};
let retried: string[] = [];
let recutting = new Set<string>();
/** The background work of the mock server is done: images whose crop was being made are ready. */
export const finishMockRecut = (): void => {
recutting.forEach((id) => {
if (images[id]) images[id] = { ...images[id], processing: false };
hero = { ...hero, slides: hero.slides.map((s) => (s.imageId === id ? { ...s, image: { ...s.image, processing: false } } : s)) };
});
recutting = new Set();
};
export const getRetriedImages = (): string[] => retried;
export const getSavedHeroRequests = (): HeroSaveRequest[] => saved;
let focusRequests: { imageId: string; slideId?: string; body: HeroFocusRequest }[] = [];
let resetSlideRequests: string[] = [];
export const getResetSlideRequests = (): string[] => resetSlideRequests;
/** The crops sent at once with PUT hero/images/{id}/focus (no save of the hero involved). */
export const getFocusRequests = (): { imageId: string; slideId?: string; body: HeroFocusRequest }[] => focusRequests;
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, processing: true };
recutting.add(edit.imageId);
}
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,
isLive: 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);
}),
// A crop for one slide (crop per use): the slide gets its own crop and shows the photo as processing.
http.put(`${API_BASE}/api/v1/hero/slides/:id/focus`, async ({ params, request }) => {
const slideId = String(params.id);
const body = (await request.json()) as HeroFocusRequest;
const slide = hero.slides.find((s) => s.id === slideId);
if (!slide) return new HttpResponse(null, { status: 404 });
focusRequests.push({ imageId: slide.imageId, slideId, body });
const updated = { ...slide, ownCrop: true, image: { ...slide.image, focus: body.focus, wideFocus: body.wide ?? null, tallFocus: body.tall ?? null, processing: true } };
recutting.add(slide.imageId);
hero = { ...hero, slides: hero.slides.map((s) => (s.id === slideId ? updated : s)) };
return HttpResponse.json(updated);
}),
http.delete(`${API_BASE}/api/v1/hero/slides/:id/focus`, ({ params }) => {
const slideId = String(params.id);
const slide = hero.slides.find((s) => s.id === slideId);
if (!slide) return new HttpResponse(null, { status: 404 });
resetSlideRequests.push(slideId);
const updated = { ...slide, ownCrop: false, image: { ...slide.image, processing: false } };
hero = { ...hero, slides: hero.slides.map((s) => (s.id === slideId ? updated : s)) };
return HttpResponse.json(updated);
}),
http.put(`${API_BASE}/api/v1/hero/images/:id/focus`, async ({ params, request }) => {
const id = String(params.id);
const body = (await request.json()) as HeroFocusRequest;
focusRequests.push({ imageId: id, body });
const current = images[id] ?? hero.slides.find((s) => s.imageId === id)?.image ?? seedImage(id);
images[id] = { ...current, focus: body.focus, wideFocus: body.wide ?? null, tallFocus: body.tall ?? null, processing: true };
recutting.add(id);
hero = { ...hero, slides: hero.slides.map((s) => (s.imageId === id ? { ...s, image: images[id] } : s)) };
return HttpResponse.json(images[id]);
}),
http.get(`${API_BASE}/api/v1/hero/images/limits`, () =>
HttpResponse.json({ formats: ['JPEG', 'PNG', 'GIF', 'BMP', 'TIFF', 'WebP', 'AVIF'], maxUploadBytes: 10 * 1024 * 1024, maxMegapixels: 64 }),
),
http.get(`${API_BASE}/api/v1/hero/images/:id`, ({ params }) => {
const image = images[String(params.id)];
return image ? HttpResponse.json(image) : new HttpResponse(null, { status: 404 });
}),
http.post(`${API_BASE}/api/v1/hero/images/:id/retry`, ({ params }) => {
retried.push(String(params.id));
return new HttpResponse(null, { status: 204 });
}),
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 });
}),
];