Hero: Slide toevoegen via de bibliotheek of een upload, geen foto wijzigen per slide
Continuous Integration / config-sls (pull_request) Successful in 9s
Continuous Integration / config-asf (pull_request) Successful in 8s
Continuous Integration / changes (pull_request) Successful in 15s
Continuous Integration / compile-sites (pull_request) Successful in 1m19s
Continuous Integration / backend-build (pull_request) Successful in 1m53s
Continuous Integration / vulnerability-scan (pull_request) Successful in 2m0s
Continuous Integration / frontend-prepare (pull_request) Successful in 2m25s
Continuous Integration / frontend-build (pull_request) Canceled after 0s
Continuous Integration / frontend-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s
Continuous Integration / frontend-lint (pull_request) Canceled after 32s
Continuous Integration / backend-test (pull_request) Canceled after 3m42s
Continuous Integration / config-sls (pull_request) Successful in 9s
Continuous Integration / config-asf (pull_request) Successful in 8s
Continuous Integration / changes (pull_request) Successful in 15s
Continuous Integration / compile-sites (pull_request) Successful in 1m19s
Continuous Integration / backend-build (pull_request) Successful in 1m53s
Continuous Integration / vulnerability-scan (pull_request) Successful in 2m0s
Continuous Integration / frontend-prepare (pull_request) Successful in 2m25s
Continuous Integration / frontend-build (pull_request) Canceled after 0s
Continuous Integration / frontend-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s
Continuous Integration / frontend-lint (pull_request) Canceled after 32s
Continuous Integration / backend-test (pull_request) Canceled after 3m42s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { useRef } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
||||
import { ImagePicker } from '@/features/media/components/ImagePicker';
|
||||
@@ -10,32 +11,31 @@ import type { HeroImageDto } from '../services/types';
|
||||
interface LibraryPickerDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
/** The hero image the slide has now (a library id is `hero:` plus this). */
|
||||
currentImageId: string | null;
|
||||
/** Which photo was chosen (the hero image id) and, for a photo uploaded here, the image the server answered with. */
|
||||
onChoose: (imageId: string, uploaded?: HeroImageDto) => void;
|
||||
maxUploadBytes: number;
|
||||
limitsText?: string;
|
||||
slideNumber: number;
|
||||
}
|
||||
|
||||
const MODULE = 'hero';
|
||||
|
||||
/**
|
||||
* "Choose from the library" for a slide: the photos of the media library that a hero slide can use (now the hero's own
|
||||
* "Add slide": the photos of the media library that a hero slide can use (now the hero's own
|
||||
* photos, also those that are in no slide; never the trash), or a new photo uploaded on the spot with the hero's own upload
|
||||
* (same checks and limits, variants cut in the background). A photo that is already in another slide can be chosen again:
|
||||
* it is the same photo, so its crop is shared (assumption 36).
|
||||
* (same checks and limits, variants cut in the background; it also lands in the library). One photo makes one new slide. A photo
|
||||
* that is already in another slide can be chosen again (assumption 36).
|
||||
*/
|
||||
export function LibraryPickerDialog({ open, onOpenChange, currentImageId, onChoose, maxUploadBytes, limitsText, slideNumber }: LibraryPickerDialogProps) {
|
||||
export function LibraryPickerDialog({ open, onOpenChange, onChoose, maxUploadBytes, limitsText }: LibraryPickerDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const library = useMediaPhotos(open);
|
||||
const upload = useUploadHeroImage();
|
||||
// The picker chooses a freshly uploaded photo by itself (onChange); the dialog hands the server's answer along with it.
|
||||
const uploaded = useRef(new Map<string, HeroImageDto>());
|
||||
const items = (library.data?.photos ?? []).filter((p) => p.module === MODULE).map((p) => toPhotoItem(p, t));
|
||||
|
||||
const uploadHere = async (file: File): Promise<PhotoItem> => {
|
||||
const image = await upload.mutateAsync(file);
|
||||
onChoose(image.id, image);
|
||||
uploaded.current.set(image.id, image);
|
||||
return { id: `${MODULE}:${image.id}`, name: file.name, bytes: file.size, createdAt: new Date().toISOString(), uses: [], thumbnailUrl: null };
|
||||
};
|
||||
|
||||
@@ -43,7 +43,7 @@ export function LibraryPickerDialog({ open, onOpenChange, currentImageId, onChoo
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-h-[90vh] max-w-3xl overflow-y-auto" data-testid="library-picker">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t('hero.library.title', { n: slideNumber })}</DialogTitle>
|
||||
<DialogTitle>{t('hero.library.title')}</DialogTitle>
|
||||
<DialogDescription>{t('hero.library.description')}</DialogDescription>
|
||||
</DialogHeader>
|
||||
{library.isError && (
|
||||
@@ -56,12 +56,15 @@ export function LibraryPickerDialog({ open, onOpenChange, currentImageId, onChoo
|
||||
) : (
|
||||
<ImagePicker
|
||||
items={items}
|
||||
value={currentImageId ? `${MODULE}:${currentImageId}` : null}
|
||||
onChange={(id) => onChoose(id.slice(MODULE.length + 1))}
|
||||
value={null}
|
||||
onChange={(id) => {
|
||||
const imageId = id.slice(MODULE.length + 1);
|
||||
onChoose(imageId, uploaded.current.get(imageId));
|
||||
}}
|
||||
onUpload={uploadHere}
|
||||
maxUploadBytes={maxUploadBytes}
|
||||
limitsText={limitsText}
|
||||
label={t('hero.library.group', { n: slideNumber })}
|
||||
label={t('hero.library.group')}
|
||||
/>
|
||||
)}
|
||||
</DialogContent>
|
||||
|
||||
@@ -1230,7 +1230,7 @@ describe('Tabs Content and Images', () => {
|
||||
});
|
||||
|
||||
|
||||
describe('Choose a photo from the library for a slide', () => {
|
||||
describe('Add a slide with a photo from the library or an upload', () => {
|
||||
beforeEach(() => {
|
||||
resetMockHero();
|
||||
});
|
||||
@@ -1250,7 +1250,7 @@ describe('Choose a photo from the library for a slide', () => {
|
||||
...over,
|
||||
});
|
||||
|
||||
it('lists the library photos (also those in no slide, never the trash) and puts the chosen one in the slide, ready to save', async () => {
|
||||
it('lists the library photos (also those in no slide, never the trash) and adds the chosen one as a new slide, ready to save', async () => {
|
||||
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
||||
server.use(
|
||||
http.get('*/media-library/photos', () =>
|
||||
@@ -1259,38 +1259,41 @@ describe('Choose a photo from the library for a slide', () => {
|
||||
http.get(`${API_BASE}/api/v1/hero/images/10000000-0000-0000-0000-000000000002`, () => HttpResponse.json(slide(2).image)),
|
||||
);
|
||||
await openPage();
|
||||
await userEvent.click(screen.getByTestId('hero-slide-0-library'));
|
||||
await userEvent.click(screen.getByTestId('hero-upload-button'));
|
||||
|
||||
const dialog = await screen.findByTestId('library-picker', {}, T);
|
||||
const radios = await within(dialog).findAllByRole('radio');
|
||||
expect(radios.map((r) => r.getAttribute('aria-label'))).toEqual(['foto-3.jpg', 'foto-2.jpg', 'foto-1.jpg']);
|
||||
expect(within(dialog).getByRole('radio', { name: 'foto-1.jpg' })).toBeChecked();
|
||||
expect(within(dialog).queryAllByRole('radio', { checked: true })).toHaveLength(0);
|
||||
expect(within(dialog).getByTestId('picker-upload-button')).toBeInTheDocument();
|
||||
|
||||
await userEvent.click(within(dialog).getByRole('radio', { name: 'foto-2.jpg' }));
|
||||
|
||||
await waitFor(() => expect(screen.queryByTestId('library-picker')).not.toBeInTheDocument());
|
||||
await waitFor(() => expect(screen.getByTestId('hero-save')).toBeEnabled());
|
||||
await userEvent.type(screen.getByTestId('hero-slide-1-alt'), 'Beschrijving');
|
||||
await userEvent.click(screen.getByTestId('hero-save'));
|
||||
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
|
||||
expect(getSavedHeroRequests()[0].slides[0].imageId).toBe('10000000-0000-0000-0000-000000000002');
|
||||
expect(getSavedHeroRequests()[0].slides.map((x) => x.imageId)).toEqual(['10000000-0000-0000-0000-000000000001', '10000000-0000-0000-0000-000000000002']);
|
||||
expect(screen.queryByTestId('hero-slide-0-library')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('lets a photo that is already in another slide be chosen again (same photo, separate slide)', async () => {
|
||||
it('lets a photo that is already in another slide be chosen again as a new slide (same photo, separate slide)', async () => {
|
||||
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] });
|
||||
server.use(
|
||||
http.get('*/media-library/photos', () => HttpResponse.json({ photos: [libraryPhoto(2, { uses: [{ module: 'hero', kind: 'slide', position: 2, active: true }] }), libraryPhoto(1)], modules: ['hero'] })),
|
||||
);
|
||||
await openPage();
|
||||
await userEvent.click(screen.getByTestId('hero-slide-0-library'));
|
||||
await userEvent.click(screen.getByTestId('hero-upload-button'));
|
||||
const dialog = await screen.findByTestId('library-picker', {}, T);
|
||||
|
||||
await userEvent.click(await within(dialog).findByRole('radio', { name: 'foto-2.jpg' }));
|
||||
|
||||
await waitFor(() => expect(screen.queryByTestId('library-picker')).not.toBeInTheDocument());
|
||||
await userEvent.type(screen.getByTestId('hero-slide-2-alt'), 'Beschrijving');
|
||||
await userEvent.click(screen.getByTestId('hero-save'));
|
||||
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
|
||||
expect(getSavedHeroRequests()[0].slides.map((x) => x.imageId)).toEqual(['10000000-0000-0000-0000-000000000002', '10000000-0000-0000-0000-000000000002']);
|
||||
expect(getSavedHeroRequests()[0].slides.map((x) => x.imageId)).toEqual(['10000000-0000-0000-0000-000000000001', '10000000-0000-0000-0000-000000000002', '10000000-0000-0000-0000-000000000002']);
|
||||
});
|
||||
|
||||
it('tells that the crop is shared when the same photo is in more than one slide', async () => {
|
||||
@@ -1308,7 +1311,7 @@ describe('Choose a photo from the library for a slide', () => {
|
||||
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
||||
server.use(http.get('*/media-library/photos', () => HttpResponse.json({ photos: [], modules: ['hero'] })));
|
||||
await openPage();
|
||||
await userEvent.click(screen.getByTestId('hero-slide-0-library'));
|
||||
await userEvent.click(screen.getByTestId('hero-upload-button'));
|
||||
const dialog = await screen.findByTestId('library-picker', {}, T);
|
||||
expect(await within(dialog).findByTestId('picker-empty')).toBeInTheDocument();
|
||||
|
||||
@@ -1316,8 +1319,10 @@ describe('Choose a photo from the library for a slide', () => {
|
||||
|
||||
await waitFor(() => expect(screen.queryByTestId('library-picker')).not.toBeInTheDocument());
|
||||
await waitFor(() => expect(screen.getByTestId('hero-save')).toBeEnabled());
|
||||
await userEvent.type(screen.getByTestId('hero-slide-1-alt'), 'Beschrijving');
|
||||
await userEvent.click(screen.getByTestId('hero-save'));
|
||||
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
|
||||
expect(getSavedHeroRequests()[0].slides[0].imageId).not.toBe('10000000-0000-0000-0000-000000000001');
|
||||
expect(getSavedHeroRequests()[0].slides).toHaveLength(2);
|
||||
expect(getSavedHeroRequests()[0].slides[1].imageId).not.toBe('10000000-0000-0000-0000-000000000001');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -155,7 +155,7 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
const [batch, setBatch] = useState<string[]>([]);
|
||||
const [recutIds, setRecutIds] = useState<string[]>([]);
|
||||
// The slide (by position) whose photo is being chosen from the library.
|
||||
const [libraryFor, setLibraryFor] = useState<number | null>(null);
|
||||
const [adding, setAdding] = useState(false);
|
||||
const fileInput = useRef<HTMLInputElement>(null);
|
||||
|
||||
const {
|
||||
@@ -271,18 +271,16 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
return () => window.removeEventListener('beforeunload', warn);
|
||||
}, [hasUnsaved]);
|
||||
|
||||
// A photo chosen from the library (or uploaded in the picker) becomes the photo of that slide: only the image id of the
|
||||
// slide changes (no new data model). The photo is the same photo wherever it is used, so its crop is shared.
|
||||
// "Add slide": a photo chosen from the library, or uploaded in the picker (it then also lands in the library), becomes a NEW
|
||||
// slide with exactly that one photo. A slide's photo is never changed afterwards: remove the slide and add another.
|
||||
const queryClient = useQueryClient();
|
||||
const chooseForSlide = async (index: number, imageId: string, uploaded?: HeroImageDto) => {
|
||||
const current = watchedSlides[index];
|
||||
if (!current) return;
|
||||
const addSlideWithPhoto = async (imageId: string, uploaded?: HeroImageDto) => {
|
||||
try {
|
||||
const image: HeroImageDto = uploaded ?? ((await queryClient.fetchQuery(heroImageQuery(imageId))) as HeroImageDto);
|
||||
setLocalImages((cur) => ({ ...cur, [image.id]: image }));
|
||||
if (uploaded && image.processing) setBatch((cur) => (cur.some((id) => images[id]?.processing) ? [...cur, image.id] : [image.id]));
|
||||
if (image.id !== current.imageId) slides.update(index, { ...current, imageId: image.id });
|
||||
setLibraryFor(null);
|
||||
slides.append({ id: null, imageId: image.id, alt: '', decorative: false, isActive: true }, { shouldFocus: false });
|
||||
setAdding(false);
|
||||
} catch (error) {
|
||||
setServerError(serverMessage(error, t('errors.generic'), t('errors.network')));
|
||||
}
|
||||
@@ -530,7 +528,7 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
data-testid="hero-upload"
|
||||
onChange={(event) => void onFiles(event.target.files)}
|
||||
/>
|
||||
<Button type="button" variant="outline" disabled={uploading} onClick={() => fileInput.current?.click()} data-testid="hero-upload-button">
|
||||
<Button type="button" variant="outline" disabled={uploading} onClick={() => setAdding(true)} data-testid="hero-upload-button">
|
||||
<ImagePlus className="mr-2 size-4" aria-hidden="true" />
|
||||
{uploading ? t('hero.slides.uploading') : t('hero.slides.add')}
|
||||
</Button>
|
||||
@@ -574,9 +572,6 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
<div className="flex h-20 w-28 items-center justify-center rounded bg-muted text-xs">?</div>
|
||||
)}
|
||||
<DoneStatus done={Boolean(image && justDone[image.id] && !image.processing)} slideNumber={index + 1} testId={`hero-slide-${index}-done`} />
|
||||
<Button type="button" variant="outline" size="sm" onClick={() => setLibraryFor(index)} data-testid={`hero-slide-${index}-library`}>
|
||||
{t('hero.library.choose')}
|
||||
</Button>
|
||||
{watchedSlides.some((other, otherIndex) => otherIndex !== index && other?.imageId === field.imageId) && (
|
||||
<p className="max-w-28 text-xs text-muted-foreground" data-testid={`hero-slide-${index}-shared-crop`}>
|
||||
{t('hero.library.sharedCrop')}
|
||||
@@ -664,13 +659,11 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
|
||||
</div>
|
||||
|
||||
<LibraryPickerDialog
|
||||
open={libraryFor !== null}
|
||||
onOpenChange={(open) => !open && setLibraryFor(null)}
|
||||
currentImageId={libraryFor !== null ? (watchedSlides[libraryFor]?.imageId ?? null) : null}
|
||||
onChoose={(imageId, uploaded) => libraryFor !== null && void chooseForSlide(libraryFor, imageId, uploaded)}
|
||||
open={adding}
|
||||
onOpenChange={setAdding}
|
||||
onChoose={(imageId, uploaded) => void addSlideWithPhoto(imageId, uploaded)}
|
||||
maxUploadBytes={limits?.maxUploadBytes ?? MAX_UPLOAD_BYTES}
|
||||
limitsText={limits ? t('hero.slides.limits', { formats: limits.formats.join(', '), size: Math.round(limits.maxUploadBytes / (1024 * 1024)), megapixels: limits.maxMegapixels }) : undefined}
|
||||
slideNumber={(libraryFor ?? 0) + 1}
|
||||
/>
|
||||
<FocusDialog
|
||||
image={focusImageId ? images[focusImageId] ?? null : null}
|
||||
|
||||
Reference in New Issue
Block a user