Uitsnede-dialoog: reset altijd zichtbaar (uitgeschakeld zonder eigen uitsnede), past direct toe en laat de dialoog open
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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. */
|
/** 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;
|
onApply: (imageId: string, request: HeroFocusRequest) => void;
|
||||||
onClose: () => 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;
|
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();
|
const { t } = useTranslation();
|
||||||
return (
|
return (
|
||||||
<Dialog open={image !== null} onOpenChange={(next) => { if (!next) onClose(); }}>
|
<Dialog open={image !== null} onOpenChange={(next) => { if (!next) onClose(); }}>
|
||||||
@@ -31,7 +33,8 @@ export function FocusDialog({ image, onApply, onClose, onReset }: FocusDialogPro
|
|||||||
<DialogTitle>{t('hero.focus.title')}</DialogTitle>
|
<DialogTitle>{t('hero.focus.title')}</DialogTitle>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
{/* Mounted only while open, so every opening starts from the stored values. */}
|
{/* Mounted only while open, so every opening starts from the stored values. */}
|
||||||
{image && <FocusEditor image={image} onApply={onApply} onClose={onClose} onReset={onReset} />}
|
{/* Keyed by the crop it shows, so after a reset it shows the photo's crop. */}
|
||||||
|
{image && <FocusEditor key={JSON.stringify([image.focus, image.wideFocus, image.tallFocus])} image={image} onApply={onApply} onClose={onClose} onReset={onReset} canReset={canReset} />}
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
@@ -39,7 +42,7 @@ export function FocusDialog({ image, onApply, onClose, onReset }: FocusDialogPro
|
|||||||
|
|
||||||
const percent = (fraction: number) => Math.round(fraction * 1000) / 10;
|
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 { t } = useTranslation();
|
||||||
const area = useRef<HTMLDivElement>(null);
|
const area = useRef<HTMLDivElement>(null);
|
||||||
// The whole picture: image.src is already a crop of the wide shape and would be shown stretched and out of place.
|
// 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
|
|||||||
|
|
||||||
<div className="flex flex-wrap justify-end gap-2">
|
<div className="flex flex-wrap justify-end gap-2">
|
||||||
{onReset && (
|
{onReset && (
|
||||||
<Button
|
<Button type="button" variant="outline" className="mr-auto" disabled={!canReset} onClick={onReset} data-testid="focus-reset">
|
||||||
type="button"
|
|
||||||
variant="ghost"
|
|
||||||
className="mr-auto"
|
|
||||||
onClick={() => {
|
|
||||||
onReset();
|
|
||||||
onClose();
|
|
||||||
}}
|
|
||||||
data-testid="focus-reset"
|
|
||||||
>
|
|
||||||
{t('hero.focus.reset')}
|
{t('hero.focus.reset')}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -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();
|
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);
|
const one = slide(1);
|
||||||
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [{ ...one, ownCrop: true }, slide(2)] });
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [{ ...one, ownCrop: true }, slide(2)] });
|
||||||
await openPage();
|
await openPage();
|
||||||
|
|
||||||
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
|
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
|
||||||
await screen.findByTestId('focus-dialog');
|
const dialog = await screen.findByTestId('focus-dialog');
|
||||||
await userEvent.click(screen.getByTestId('focus-reset'));
|
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]));
|
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)] });
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
||||||
await openPage();
|
await openPage();
|
||||||
|
|
||||||
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
|
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 () => {
|
it('asks to save a new slide first before its crop can be changed', async () => {
|
||||||
|
|||||||
@@ -690,14 +690,11 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
|||||||
image={focusSlideIndex !== null ? imageOfSlide(focusSlideIndex) ?? null : null}
|
image={focusSlideIndex !== null ? imageOfSlide(focusSlideIndex) ?? null : null}
|
||||||
onApply={(_imageId, request) => focusSlideIndex !== null && applyFocus(focusSlideIndex, request)}
|
onApply={(_imageId, request) => focusSlideIndex !== null && applyFocus(focusSlideIndex, request)}
|
||||||
onClose={() => setFocusSlideIndex(null)}
|
onClose={() => setFocusSlideIndex(null)}
|
||||||
onReset={
|
canReset={focusSlideIndex !== null && Boolean(savedSlideOf(focusSlideIndex)?.ownCrop)}
|
||||||
focusSlideIndex !== null && savedSlideOf(focusSlideIndex)?.ownCrop
|
onReset={() => {
|
||||||
? () => {
|
const slideId = focusSlideIndex !== null ? watchedSlides[focusSlideIndex]?.id : undefined;
|
||||||
const slideId = watchedSlides[focusSlideIndex]?.id;
|
|
||||||
if (slideId) resetSlideFocus.mutate(slideId, { onError: (error) => setServerError(serverMessage(error, t('errors.generic'), t('errors.network'))) });
|
if (slideId) resetSlideFocus.mutate(slideId, { onError: (error) => setServerError(serverMessage(error, t('errors.generic'), t('errors.network'))) });
|
||||||
}
|
}}
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
</form>
|
</form>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user