diff --git a/frontend/src/features/hero/components/CropThumbnail.tsx b/frontend/src/features/hero/components/CropThumbnail.tsx new file mode 100644 index 0000000..7a3aef9 --- /dev/null +++ b/frontend/src/features/hero/components/CropThumbnail.tsx @@ -0,0 +1,74 @@ +import { Crop } from 'lucide-react'; +import { useTranslation } from 'react-i18next'; +import { WIDE, cropFor } from '../lib/focusMath'; +import { mediaUrl } from '../lib/media'; +import type { HeroFocusRequest, HeroImageDto } from '../services/types'; + +interface CropThumbnailProps { + image: HeroImageDto; + /** The small picture of the published (saved) crop. */ + savedSrc: string; + /** A focus change that was applied in the dialog and is not saved yet: the thumbnail then shows that crop already. */ + edit?: HeroFocusRequest; + onClick: () => void; + testId: string; +} + +/** + * The picture of a slide, and at the same time the way to change its crop: a real button (so keyboard and screen reader + * users have it too). A hint with a crop icon and "Adjust crop" shows on hover and on keyboard focus, and a small crop + * icon is always there, so it can be found on a touch screen as well. After "Apply" in the dialog the thumbnail shows + * the new crop right away (the same calculation as the host) with a clear "not saved yet" note. + */ +export function CropThumbnail({ image, savedSrc, edit, onClick, testId }: CropThumbnailProps) { + const { t } = useTranslation(); + const local = edit && image.previewSrc ? localCrop(image, edit) : null; + + return ( + + ); +} + +/** Places the whole picture in the box so exactly the crop of the host shows (the same calculation as the dialog). */ +function localCrop(image: HeroImageDto, edit: HeroFocusRequest) { + const focus = edit.wide ?? edit.focus; + const crop = cropFor(image.width, image.height, WIDE, focus); + return { + width: `${(image.width / crop.width) * 100}%`, + left: `${(-crop.x / crop.width) * 100}%`, + // top is a percentage of the box height: the box width times denominator over numerator. + top: `${(-crop.y / crop.width) * (WIDE.numerator / WIDE.denominator) * 100}%`, + }; +} diff --git a/frontend/src/features/hero/pages/HeroPage.test.tsx b/frontend/src/features/hero/pages/HeroPage.test.tsx index e3dc35c..5fc0621 100644 --- a/frontend/src/features/hero/pages/HeroPage.test.tsx +++ b/frontend/src/features/hero/pages/HeroPage.test.tsx @@ -38,7 +38,7 @@ describe('Hero page', () => { await openPage(); expect(await screen.findByTestId('hero-slide-0-processing', {}, T)).toBeInTheDocument(); - expect(screen.getByTestId('hero-slide-0-focus')).toBeDisabled(); + expect(screen.queryByTestId('hero-slide-0-focus')).not.toBeInTheDocument(); }); it('shows how many photos are ready, with an accessible progress bar, and hides it when all are ready', async () => { @@ -134,6 +134,7 @@ describe('Hero page', () => { it('does not save without a title and says why', async () => { await openPage(); + await userEvent.type(screen.getByTestId('hero-lead'), 'Alleen een lead'); await userEvent.click(screen.getByTestId('hero-save')); @@ -339,6 +340,8 @@ describe('Focus dialog', () => { it('sends no focus edits when nothing was changed', async () => { await open(); await userEvent.click(screen.getByTestId('focus-cancel')); + // Something else changed, so there is something to save; the cancelled focus is not part of it. + await userEvent.type(screen.getByTestId('hero-tagline'), 'x'); await userEvent.click(screen.getByTestId('hero-save')); @@ -886,7 +889,7 @@ describe('Each photo shows by itself when its processing is finished', () => { await userEvent.upload(screen.getByTestId('hero-upload'), jpg('groot.jpg')); expect(await screen.findByTestId('hero-slide-0-processing', {}, T)).toHaveClass('hero-shimmer'); - expect(screen.getByTestId('hero-slide-0-focus')).toBeDisabled(); + expect(screen.queryByTestId('hero-slide-0-focus')).not.toBeInTheDocument(); expect(screen.queryByTestId('hero-slide-0-done-text')).not.toBeInTheDocument(); // The server is done; nobody saves or reloads: the page asks again by itself. @@ -934,7 +937,140 @@ describe('Each photo shows by itself when its processing is finished', () => { expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled(); expect(screen.getByTestId('hero-slide-1-processing')).toBeInTheDocument(); - expect(screen.getByTestId('hero-slide-1-focus')).toBeDisabled(); + expect(screen.queryByTestId('hero-slide-1-focus')).not.toBeInTheDocument(); expect(screen.getByTestId('hero-processing-count')).toHaveTextContent(/1 van 2|1 of 2/); }, 30000); }); + + +describe('The picture of a slide is the way to adjust its crop', () => { + beforeEach(() => { + resetMockHero(); + }); + + it('is a real button named Adjust crop, with a hint that is there without hovering, and it opens the dialog', async () => { + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] }); + await openPage(); + + const thumb = screen.getByTestId('hero-slide-0-focus'); + + expect(thumb.tagName).toBe('BUTTON'); + expect(thumb).toHaveAccessibleName(/Uitsnede aanpassen|Adjust crop/); + expect(thumb.querySelector('svg')).not.toBeNull(); + expect(within(thumb).getAllByText(/Uitsnede aanpassen|Adjust crop/).length).toBeGreaterThan(0); + expect(screen.queryByRole('button', { name: /Focus instellen|Set focus/ })).not.toBeInTheDocument(); + thumb.focus(); + await userEvent.keyboard('{Enter}'); + expect(await screen.findByTestId('focus-dialog', {}, T)).toBeInTheDocument(); + }); + + it('shows the new crop on the thumbnail right after Apply, with the not-saved note, and drops it when the change is undone', async () => { + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] }); + await openPage(); + expect(screen.queryByTestId('hero-slide-0-focus-local')).not.toBeInTheDocument(); + + await userEvent.click(screen.getByTestId('hero-slide-0-focus')); + fireEvent.change(await screen.findByTestId('focus-x', {}, T), { target: { value: '10' } }); + await userEvent.click(screen.getByTestId('focus-save')); + + await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument()); + const local = await screen.findByTestId('hero-slide-0-focus-local', {}, T); + expect(local.getAttribute('src')).toContain('full-1280.webp'); + expect(local.style.left).toMatch(/^-?\d/); + expect(screen.getByTestId('hero-slide-0-focus-pending')).toHaveTextContent(/Focus gewijzigd, nog niet opgeslagen|Focus changed, not saved yet/); + }); + + it('follows the whole chain: Apply, Save sends the focus, the new crop is made (animation), then it is finished', async () => { + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] }); + await openPage(); + await userEvent.click(screen.getByTestId('hero-slide-1-focus')); + fireEvent.change(await screen.findByTestId('focus-y', {}, T), { target: { value: '20' } }); + await userEvent.click(screen.getByTestId('focus-save')); + await screen.findByTestId('hero-slide-1-focus-pending', {}, T); + + await userEvent.click(screen.getByTestId('hero-save')); + + await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1)); + expect(getSavedHeroRequests()[0].imageFocus).toEqual([ + expect.objectContaining({ imageId: '10000000-0000-0000-0000-000000000002', focus: { x: 0.5, y: 0.2, zoom: 1 } }), + ]); + // Saved: the note of the unsaved change is gone and the new crop is being made, with the animation, on that photo only. + const recut = await screen.findByTestId('hero-slide-1-recut', {}, T); + expect(recut).toHaveClass('hero-shimmer'); + expect(recut).toHaveTextContent(/Nieuwe uitsnede wordt gemaakt|Making the new crop/); + expect(screen.queryByTestId('hero-slide-1-focus-pending')).not.toBeInTheDocument(); + expect(screen.queryByTestId('hero-slide-0-recut')).not.toBeInTheDocument(); + + finishMockRecut(); + expect(await screen.findByTestId('hero-slide-1-done-text', {}, { timeout: 12000 })).toHaveTextContent(/Verwerking afgerond|Processing finished/); + expect(screen.queryByTestId('hero-slide-1-recut')).not.toBeInTheDocument(); + expect(screen.getByTestId('hero-slide-1-focus').tagName).toBe('BUTTON'); + }, 30000); +}); + + +describe('The Save button', () => { + beforeEach(() => { + resetMockHero(); + }); + + it('is greyed out while there is nothing to save, active after a change, busy while saving, and greyed out again after saving', async () => { + setMockHero({ ...emptyHero(), id: 'h1', title: 'Titel', slides: [slide(1)] }); + let release: () => void = () => undefined; + const gate = new Promise((resolve) => { release = resolve; }); + server.use( + http.put(`${API_BASE}/api/v1/hero`, async ({ request }) => { + await gate; + const body = (await request.json()) as { title: string }; + return HttpResponse.json({ ...emptyHero(), id: 'h1', title: body.title, slides: [slide(1)] }); + }), + ); + await openPage(); + const save = screen.getByTestId('hero-save'); + expect(save).toBeDisabled(); + expect(save).toHaveAttribute('aria-disabled', 'true'); + + await userEvent.type(screen.getByTestId('hero-title-input'), '!'); + await waitFor(() => expect(save).toBeEnabled()); + expect(save).toHaveAttribute('aria-disabled', 'false'); + + await userEvent.click(save); + await waitFor(() => expect(save).toHaveAttribute('aria-busy', 'true')); + expect(save).toBeDisabled(); + expect(save).toHaveTextContent(/Bezig met opslaan|Saving/); + + release(); + await waitFor(() => expect(save).toHaveAttribute('aria-busy', 'false')); + await waitFor(() => expect(save).toBeDisabled()); + expect(screen.queryByTestId('hero-unsaved')).not.toBeInTheDocument(); + }); + + it('is greyed out again when a change is undone, and active for a focus change that is applied', async () => { + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] }); + await openPage(); + const save = screen.getByTestId('hero-save'); + + await userEvent.type(screen.getByTestId('hero-title-input'), 'X'); + await waitFor(() => expect(save).toBeEnabled()); + await userEvent.type(screen.getByTestId('hero-title-input'), '{backspace}'); + await waitFor(() => expect(save).toBeDisabled()); + + await userEvent.click(screen.getByTestId('hero-slide-0-focus')); + fireEvent.change(await screen.findByTestId('focus-x', {}, T), { target: { value: '15' } }); + await userEvent.click(screen.getByTestId('focus-save')); + + await waitFor(() => expect(save).toBeEnabled()); + }); + + it('stays active when saving failed, so it can be tried again', async () => { + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] }); + server.use(http.put(`${API_BASE}/api/v1/hero`, () => HttpResponse.json({ error: 'x', message: 'Het lukt niet.' }, { status: 500 }))); + await openPage(); + await userEvent.type(screen.getByTestId('hero-title-input'), 'X'); + + await userEvent.click(screen.getByTestId('hero-save')); + + await waitFor(() => expect(screen.getByTestId('hero-save')).toHaveAttribute('aria-busy', 'false')); + expect(screen.getByTestId('hero-save')).toBeEnabled(); + }); +}); diff --git a/frontend/src/features/hero/pages/HeroPage.tsx b/frontend/src/features/hero/pages/HeroPage.tsx index 8add405..10a7011 100644 --- a/frontend/src/features/hero/pages/HeroPage.tsx +++ b/frontend/src/features/hero/pages/HeroPage.tsx @@ -4,7 +4,7 @@ import { useFieldArray, useForm, useWatch } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { useTranslation } from 'react-i18next'; import { toast } from 'sonner'; -import { ArrowDown, ArrowUp, Crosshair, ImagePlus, Trash2 } from 'lucide-react'; +import { ArrowDown, ArrowUp, ImagePlus, Trash2 } from 'lucide-react'; import { DndContext, KeyboardSensor, PointerSensor, closestCenter, useSensor, useSensors, type DragEndEvent } from '@dnd-kit/core'; import { SortableContext, sortableKeyboardCoordinates, verticalListSortingStrategy } from '@dnd-kit/sortable'; import { Badge } from '@/components/ui/badge'; @@ -15,7 +15,8 @@ import { FieldError } from '@/components/ui/FieldError'; import { FormErrorBanner } from '@/components/ui/FormErrorBanner'; import { NetworkError, ProblemDetailsError } from '@/lib/api-client'; import { FocusDialog } from '../components/FocusDialog'; -import { DoneStatus, PhotoPlaceholder, ProcessingStatus, RecutThumbnail } from '../components/ProcessingStatus'; +import { CropThumbnail } from '../components/CropThumbnail'; +import { DoneStatus, PhotoPlaceholder, ProcessingStatus, RecutThumbnail, Spinner } from '../components/ProcessingStatus'; import { SlideSwitch } from '../components/SlideSwitch'; import { SortableSlide } from '../components/SortableSlide'; import { UnsavedChangesGuard } from '../components/UnsavedChangesGuard'; @@ -472,7 +473,13 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { ) : image?.processing ? ( ) : image ? ( - + setFocusImageId(field.imageId)} + testId={`hero-slide-${index}-focus`} + /> ) : (
?
)} @@ -486,9 +493,6 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {

{t('hero.slides.altHint')}

- {focusEdits[field.imageId] && ( -

{t('hero.slides.focusPending')}

- )}