diff --git a/frontend/src/features/hero/components/FocusDialog.tsx b/frontend/src/features/hero/components/FocusDialog.tsx index 37a0ed8..61ac2d6 100644 --- a/frontend/src/features/hero/components/FocusDialog.tsx +++ b/frontend/src/features/hero/components/FocusDialog.tsx @@ -18,11 +18,13 @@ interface FocusDialogProps { /** Keeps the change locally: it is sent and made live when the hero is saved, and dropped when the page is left. */ onApply: (imageId: string, request: HeroFocusRequest) => void; onClose: () => void; - /** The slide has a crop of its own: offers "use the crop of the photo again". */ + /** "Use the crop of the photo again": applied at once, the dialog stays open. The button is always shown. */ onReset?: () => void; + /** The slide has a crop of its own; without one there is nothing to reset and the button is disabled. */ + canReset?: boolean; } -export function FocusDialog({ image, onApply, onClose, onReset }: FocusDialogProps) { +export function FocusDialog({ image, onApply, onClose, onReset, canReset = false }: FocusDialogProps) { const { t } = useTranslation(); return ( { if (!next) onClose(); }}> @@ -31,7 +33,8 @@ export function FocusDialog({ image, onApply, onClose, onReset }: FocusDialogPro {t('hero.focus.title')} {/* Mounted only while open, so every opening starts from the stored values. */} - {image && } + {/* Keyed by the crop it shows, so after a reset it shows the photo's crop. */} + {image && } ); @@ -39,7 +42,7 @@ export function FocusDialog({ image, onApply, onClose, onReset }: FocusDialogPro const percent = (fraction: number) => Math.round(fraction * 1000) / 10; -function FocusEditor({ image, onApply, onClose, onReset }: { image: HeroImageDto; onApply: (imageId: string, request: HeroFocusRequest) => void; onClose: () => void; onReset?: () => void }) { +function FocusEditor({ image, onApply, onClose, onReset, canReset }: { image: HeroImageDto; onApply: (imageId: string, request: HeroFocusRequest) => void; onClose: () => void; onReset?: () => void; canReset: boolean }) { const { t } = useTranslation(); const area = useRef(null); // The whole picture: image.src is already a crop of the wide shape and would be shown stretched and out of place. @@ -238,16 +241,7 @@ function FocusEditor({ image, onApply, onClose, onReset }: { image: HeroImageDto
{onReset && ( - )} diff --git a/frontend/src/features/hero/pages/HeroPage.test.tsx b/frontend/src/features/hero/pages/HeroPage.test.tsx index 69b4e74..a48c170 100644 --- a/frontend/src/features/hero/pages/HeroPage.test.tsx +++ b/frontend/src/features/hero/pages/HeroPage.test.tsx @@ -1315,26 +1315,36 @@ describe('Add a slide with a photo from the library or an upload', () => { expect(screen.queryByTestId('hero-slide-0-shared-crop')).not.toBeInTheDocument(); }); - it('offers to go back to the crop of the photo for a slide with a crop of its own', async () => { + it('shows the reset button always: it applies at once and keeps the dialog open for a slide with its own crop', async () => { const one = slide(1); setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [{ ...one, ownCrop: true }, slide(2)] }); await openPage(); await userEvent.click(screen.getByTestId('hero-slide-0-focus')); - await screen.findByTestId('focus-dialog'); - await userEvent.click(screen.getByTestId('focus-reset')); + const dialog = await screen.findByTestId('focus-dialog'); + expect(within(dialog).getByTestId('focus-reset')).toBeEnabled(); + expect(within(dialog).queryByTestId('focus-apply-hint')).not.toBeInTheDocument(); + await userEvent.click(within(dialog).getByTestId('focus-reset')); await waitFor(() => expect(getResetSlideRequests()).toEqual([one.id])); - }); + expect(screen.getByTestId('focus-dialog')).toBeInTheDocument(); + await waitFor(() => expect(within(screen.getByTestId('focus-dialog')).getByTestId('focus-reset')).toBeDisabled()); + }); - it('does not offer the reset for a slide that uses the crop of its photo', async () => { + it('shows the reset button disabled for a slide that uses the crop of its photo, and Cancel changes nothing', async () => { setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] }); await openPage(); await userEvent.click(screen.getByTestId('hero-slide-0-focus')); - await screen.findByTestId('focus-dialog'); + const dialog = await screen.findByTestId('focus-dialog'); - expect(screen.queryByTestId('focus-reset')).not.toBeInTheDocument(); + expect(within(dialog).getByTestId('focus-reset')).toBeVisible(); + expect(within(dialog).getByTestId('focus-reset')).toBeDisabled(); + fireEvent.change(within(dialog).getByTestId('focus-x'), { target: { value: '10' } }); + await userEvent.click(within(dialog).getByTestId('focus-cancel')); + await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument()); + expect(getFocusRequests()).toHaveLength(0); + expect(getResetSlideRequests()).toHaveLength(0); }); it('asks to save a new slide first before its crop can be changed', async () => { diff --git a/frontend/src/features/hero/pages/HeroPage.tsx b/frontend/src/features/hero/pages/HeroPage.tsx index 6a46734..f787b29 100644 --- a/frontend/src/features/hero/pages/HeroPage.tsx +++ b/frontend/src/features/hero/pages/HeroPage.tsx @@ -690,14 +690,11 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { image={focusSlideIndex !== null ? imageOfSlide(focusSlideIndex) ?? null : null} onApply={(_imageId, request) => focusSlideIndex !== null && applyFocus(focusSlideIndex, request)} onClose={() => setFocusSlideIndex(null)} - onReset={ - focusSlideIndex !== null && savedSlideOf(focusSlideIndex)?.ownCrop - ? () => { - const slideId = watchedSlides[focusSlideIndex]?.id; - if (slideId) resetSlideFocus.mutate(slideId, { onError: (error) => setServerError(serverMessage(error, t('errors.generic'), t('errors.network'))) }); - } - : undefined - } + canReset={focusSlideIndex !== null && Boolean(savedSlideOf(focusSlideIndex)?.ownCrop)} + onReset={() => { + const slideId = focusSlideIndex !== null ? watchedSlides[focusSlideIndex]?.id : undefined; + if (slideId) resetSlideFocus.mutate(slideId, { onError: (error) => setServerError(serverMessage(error, t('errors.generic'), t('errors.network'))) }); + }} /> );