Hero: een gewijzigd focuspunt geldt pas bij het opslaan van de hero (Bryan); het focus-venster houdt de wijziging lokaal
Het eindpunt PUT /hero/images/{id}/focus is vervallen; de focus gaat mee in de opslag (imageFocus) en de varianten worden dan in nieuwe bestanden opnieuw gesneden.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,7 +15,7 @@ import { FocusDialog } from '../components/FocusDialog';
|
||||
import { mediaUrl } from '../lib/media';
|
||||
import { fromLocalInput, heroFormSchema, toLocalInput, type HeroFormData } from '../schemas/hero';
|
||||
import { useHero, useSaveHero, useUploadHeroImage } from '../services/hooks';
|
||||
import type { HeroAdminDto, HeroImageDto, HeroSaveRequest } from '../services/types';
|
||||
import type { HeroAdminDto, HeroFocusRequest, HeroImageDto, HeroSaveRequest } from '../services/types';
|
||||
|
||||
const textareaClass =
|
||||
'w-full rounded-md border border-input bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
||||
@@ -35,7 +35,7 @@ function toForm(hero: HeroAdminDto): HeroFormData {
|
||||
};
|
||||
}
|
||||
|
||||
function toRequest(values: HeroFormData): HeroSaveRequest {
|
||||
function toRequest(values: HeroFormData, focusEdits: Record<string, HeroFocusRequest>): HeroSaveRequest {
|
||||
return {
|
||||
title: values.title,
|
||||
tagline: values.tagline || null,
|
||||
@@ -46,6 +46,7 @@ function toRequest(values: HeroFormData): HeroSaveRequest {
|
||||
startsAt: fromLocalInput(values.startsAt),
|
||||
endsAt: fromLocalInput(values.endsAt),
|
||||
slides: values.slides.map((s) => ({ id: s.id, imageId: s.imageId, alt: s.decorative ? null : s.alt, decorative: s.decorative, isActive: s.isActive })),
|
||||
imageFocus: Object.entries(focusEdits).map(([imageId, edit]) => ({ imageId, ...edit })),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -125,6 +126,8 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
const [uploadError, setUploadError] = useState<string | null>(null);
|
||||
const [localImages, setLocalImages] = useState<Record<string, HeroImageDto>>({});
|
||||
const [focusImageId, setFocusImageId] = useState<string | null>(null);
|
||||
// Changed focus points are kept here until the hero is saved: they are sent with the save and dropped when the page is left.
|
||||
const [focusEdits, setFocusEdits] = useState<Record<string, HeroFocusRequest>>({});
|
||||
const fileInput = useRef<HTMLInputElement>(null);
|
||||
|
||||
const {
|
||||
@@ -143,14 +146,21 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
return map;
|
||||
}, [hero.slides, localImages]);
|
||||
|
||||
const hasPendingFocus = Object.keys(focusEdits).length > 0;
|
||||
useEffect(() => {
|
||||
if (!isDirty) return undefined;
|
||||
if (!isDirty && !hasPendingFocus) return undefined;
|
||||
const warn = (event: BeforeUnloadEvent) => {
|
||||
event.preventDefault();
|
||||
};
|
||||
window.addEventListener('beforeunload', warn);
|
||||
return () => window.removeEventListener('beforeunload', warn);
|
||||
}, [isDirty]);
|
||||
}, [isDirty, hasPendingFocus]);
|
||||
|
||||
// The image as the dialog shows it: the saved one with a not yet saved change of its focus applied.
|
||||
const withPendingFocus = (image: HeroImageDto | null): HeroImageDto | null => {
|
||||
const edit = image ? focusEdits[image.id] : undefined;
|
||||
return image && edit ? { ...image, focus: edit.focus, wideFocus: edit.wide, tallFocus: edit.tall } : image;
|
||||
};
|
||||
|
||||
const onUpload = async (files: FileList | null) => {
|
||||
const file = files?.[0];
|
||||
@@ -176,7 +186,8 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
const onSubmit = async (values: HeroFormData) => {
|
||||
setServerError(null);
|
||||
try {
|
||||
await save.mutateAsync(toRequest(values));
|
||||
await save.mutateAsync(toRequest(values, focusEdits));
|
||||
setFocusEdits({});
|
||||
toast.success(t('hero.savedToast'));
|
||||
} catch (error) {
|
||||
setServerError(serverMessage(error, t('errors.generic'), t('errors.network')));
|
||||
@@ -297,6 +308,9 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
<Label htmlFor={`hero-slide-${index}-alt`}>{t('hero.slides.alt', { n: index + 1 })}</Label>
|
||||
<Input id={`hero-slide-${index}-alt`} maxLength={300} disabled={decorative} data-testid={`hero-slide-${index}-alt`} {...register(`slides.${index}.alt` as const)} />
|
||||
<FieldError message={err(heroErrors.slides?.[index]?.alt?.message)} testId={`hero-slide-${index}-alt-error`} />
|
||||
{focusEdits[field.imageId] && (
|
||||
<p className="text-xs font-medium" data-testid={`hero-slide-${index}-focus-pending`}>{t('hero.slides.focusPending')}</p>
|
||||
)}
|
||||
<div className="flex flex-wrap gap-4 text-sm">
|
||||
<label className="flex items-center gap-2">
|
||||
<input type="checkbox" className="size-4 rounded border-input" data-testid={`hero-slide-${index}-decorative`} {...register(`slides.${index}.decorative` as const)} />
|
||||
@@ -315,7 +329,7 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
<Button type="button" variant="outline" size="icon" aria-label={t('hero.slides.down')} disabled={index === slides.fields.length - 1} onClick={() => slides.move(index, index + 1)} data-testid={`hero-slide-${index}-down`}>
|
||||
<ArrowDown className="size-4" aria-hidden="true" />
|
||||
</Button>
|
||||
<Button type="button" variant="outline" size="icon" aria-label={t('hero.slides.focus')} disabled={!image} onClick={() => setFocusImageId(field.imageId)} data-testid={`hero-slide-${index}-focus`}>
|
||||
<Button type="button" variant={focusEdits[field.imageId] ? 'default' : 'outline'} size="icon" aria-label={t('hero.slides.focus')} disabled={!image} onClick={() => setFocusImageId(field.imageId)} data-testid={`hero-slide-${index}-focus`}>
|
||||
<Crosshair className="size-4" aria-hidden="true" />
|
||||
</Button>
|
||||
<Button type="button" variant="outline" size="icon" aria-label={t('hero.slides.remove')} onClick={() => slides.remove(index)} data-testid={`hero-slide-${index}-remove`}>
|
||||
@@ -339,7 +353,11 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<FocusDialog image={focusImageId ? images[focusImageId] ?? null : null} onClose={() => setFocusImageId(null)} />
|
||||
<FocusDialog
|
||||
image={withPendingFocus(focusImageId ? images[focusImageId] ?? null : null)}
|
||||
onApply={(imageId, edit) => setFocusEdits((current) => ({ ...current, [imageId]: edit }))}
|
||||
onClose={() => setFocusImageId(null)}
|
||||
/>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user