mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
* feat(places): upload a custom place image on the server (#1136) * i18n(places): custom place image labels * feat(places): click a place thumbnail to set a custom image * feat(places): custom place image on mobile place and collection sheets * test(places): cover custom place and collection image upload * docs(places): document custom place images * fix(places): carry osm_id on the embedded place so day-plan thumbnails auto-fetch (#1136) * feat(places): custom tooltip on the place image controls * fix(places): show a custom place image on map markers, skip the redundant photo fetch (#1136)
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import { render, screen, fireEvent, waitFor } from '../../../tests/helpers/render';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { getCached, isLoading, fetchPhoto, onThumbReady } from '../../services/photoService';
|
||||
|
||||
// Mock photoService — PlaceAvatarUpload wraps PlaceAvatar, which pulls thumbnails.
|
||||
vi.mock('../../services/photoService', () => ({
|
||||
getCached: vi.fn(() => null),
|
||||
isLoading: vi.fn(() => false),
|
||||
fetchPhoto: vi.fn(),
|
||||
onThumbReady: vi.fn(() => () => {}),
|
||||
}));
|
||||
|
||||
// IntersectionObserver stub (PlaceAvatar observes visibility to lazy-load photos)
|
||||
class MockIntersectionObserver {
|
||||
observe = vi.fn();
|
||||
disconnect = vi.fn();
|
||||
unobserve = vi.fn();
|
||||
}
|
||||
|
||||
beforeAll(() => {
|
||||
(globalThis as any).IntersectionObserver = MockIntersectionObserver;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(getCached).mockReturnValue(null);
|
||||
vi.mocked(isLoading).mockReturnValue(false);
|
||||
vi.mocked(fetchPhoto).mockReset();
|
||||
vi.mocked(onThumbReady).mockReturnValue(() => {});
|
||||
});
|
||||
|
||||
import PlaceAvatarUpload from './PlaceAvatarUpload';
|
||||
|
||||
const placeNoImage = {
|
||||
id: 1,
|
||||
name: 'Eiffel Tower',
|
||||
image_url: null,
|
||||
google_place_id: null,
|
||||
osm_id: null,
|
||||
lat: 48.8584,
|
||||
lng: 2.2945,
|
||||
};
|
||||
|
||||
const placeWithImage = { ...placeNoImage, image_url: 'https://example.com/eiffel.jpg' };
|
||||
|
||||
describe('PlaceAvatarUpload', () => {
|
||||
it('FE-COMP-PLACEAVUPLOAD-001: renders the underlying avatar image when image_url is set', () => {
|
||||
render(<PlaceAvatarUpload place={placeWithImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
const img = screen.getByRole('img') as HTMLImageElement;
|
||||
expect(img.src).toContain('eiffel.jpg');
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-002: clicking the avatar opens the hidden file picker', async () => {
|
||||
const user = userEvent.setup();
|
||||
const { container } = render(<PlaceAvatarUpload place={placeNoImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
const input = container.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const clickSpy = vi.spyOn(input, 'click');
|
||||
await user.click(screen.getByRole('button', { name: 'Upload image' }));
|
||||
expect(clickSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-003: choosing a file calls onUpload with the picked file', async () => {
|
||||
const onUpload = vi.fn().mockResolvedValue(undefined);
|
||||
const { container } = render(<PlaceAvatarUpload place={placeNoImage} onUpload={onUpload} onRemove={vi.fn()} />);
|
||||
const input = container.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const file = new File(['x'], 'photo.jpg', { type: 'image/jpeg' });
|
||||
fireEvent.change(input, { target: { files: [file] } });
|
||||
await waitFor(() => expect(onUpload).toHaveBeenCalledWith(file));
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-004: the remove button only appears when a custom image is set', () => {
|
||||
const { rerender } = render(<PlaceAvatarUpload place={placeNoImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
expect(screen.queryByRole('button', { name: 'Remove image' })).toBeNull();
|
||||
rerender(<PlaceAvatarUpload place={placeWithImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
expect(screen.getByRole('button', { name: 'Remove image' })).toBeTruthy();
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-005: clicking remove calls onRemove', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onRemove = vi.fn().mockResolvedValue(undefined);
|
||||
render(<PlaceAvatarUpload place={placeWithImage} onUpload={vi.fn()} onRemove={onRemove} />);
|
||||
await user.click(screen.getByRole('button', { name: 'Remove image' }));
|
||||
expect(onRemove).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-006: the avatar aria-label reflects upload vs change from the places.* keys', () => {
|
||||
const { rerender } = render(<PlaceAvatarUpload place={placeNoImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
expect(screen.getByRole('button', { name: 'Upload image' })).toBeTruthy();
|
||||
rerender(<PlaceAvatarUpload place={placeWithImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
expect(screen.getByRole('button', { name: 'Change image' })).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,111 @@
|
||||
import { useRef, useState } from 'react'
|
||||
import { Camera, Loader2, X } from 'lucide-react'
|
||||
import PlaceAvatar from './PlaceAvatar'
|
||||
import { Tooltip } from './Tooltip'
|
||||
import { normalizeImageFile } from '../../utils/convertHeic'
|
||||
import { useToast } from './Toast'
|
||||
import { useTranslation, translateApiError } from '../../i18n'
|
||||
import type { Place } from '../../types'
|
||||
|
||||
interface Category {
|
||||
color?: string
|
||||
icon?: string
|
||||
}
|
||||
|
||||
interface PlaceAvatarUploadProps {
|
||||
place: Pick<Place, 'id' | 'name' | 'image_url' | 'google_place_id' | 'osm_id' | 'lat' | 'lng'>
|
||||
category?: Category | null
|
||||
size?: number
|
||||
onUpload: (file: File) => Promise<void>
|
||||
/** Clears the custom image; the auto-fetched default thumbnail then returns (#1136). */
|
||||
onRemove: () => Promise<void> | void
|
||||
}
|
||||
|
||||
/**
|
||||
* A PlaceAvatar the user can click to set a custom thumbnail (#1136): hover reveals
|
||||
* a camera overlay with a tooltip, clicking opens the file picker, and when a custom
|
||||
* upload is present a small corner button removes it (falling back to the default).
|
||||
*/
|
||||
export default function PlaceAvatarUpload({ place, category, size = 52, onUpload, onRemove }: PlaceAvatarUploadProps) {
|
||||
const { t } = useTranslation()
|
||||
const toast = useToast()
|
||||
const fileRef = useRef<HTMLInputElement>(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const hasCustom = Boolean(place.image_url)
|
||||
|
||||
const handleFile = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0]
|
||||
e.target.value = ''
|
||||
if (!file) return
|
||||
setBusy(true)
|
||||
try {
|
||||
await onUpload(await normalizeImageFile(file))
|
||||
} catch (err: unknown) {
|
||||
toast.error(translateApiError(t, err, 'places.imageUploadError'))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleRemove = async (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
setBusy(true)
|
||||
try {
|
||||
await onRemove()
|
||||
} catch (err: unknown) {
|
||||
toast.error(translateApiError(t, err, 'places.imageUploadError'))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ position: 'relative', width: size, height: size }}>
|
||||
<Tooltip label={hasCustom ? t('places.changeImage') : t('places.uploadImage')} placement="top">
|
||||
<div
|
||||
className="group"
|
||||
style={{ position: 'relative', width: size, height: size, borderRadius: '50%', cursor: busy ? 'default' : 'pointer' }}
|
||||
onClick={() => { if (!busy) fileRef.current?.click() }}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
onKeyDown={e => { if ((e.key === 'Enter' || e.key === ' ') && !busy) { e.preventDefault(); fileRef.current?.click() } }}
|
||||
aria-label={hasCustom ? t('places.changeImage') : t('places.uploadImage')}
|
||||
>
|
||||
<PlaceAvatar place={place} category={category} size={size} />
|
||||
|
||||
{/* Hover overlay — a camera cue that this thumbnail is editable. */}
|
||||
<div
|
||||
className="opacity-0 group-hover:opacity-100"
|
||||
style={{
|
||||
position: 'absolute', inset: 0, borderRadius: '50%',
|
||||
background: 'rgba(0,0,0,0.45)', display: 'flex', alignItems: 'center', justifyContent: 'center',
|
||||
transition: 'opacity 0.15s', pointerEvents: 'none',
|
||||
}}
|
||||
>
|
||||
{busy ? <Loader2 size={Math.round(size * 0.34)} className="animate-spin" color="#fff" /> : <Camera size={Math.round(size * 0.34)} color="#fff" />}
|
||||
</div>
|
||||
</div>
|
||||
</Tooltip>
|
||||
|
||||
{/* Remove button — only when a custom upload is set. */}
|
||||
{hasCustom && !busy && (
|
||||
<Tooltip label={t('places.removeImage')} placement="top">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleRemove}
|
||||
aria-label={t('places.removeImage')}
|
||||
style={{
|
||||
position: 'absolute', top: -3, right: -3, width: 18, height: 18, borderRadius: '50%',
|
||||
background: '#ef4444', color: '#fff', border: '2px solid var(--bg-elevated, #fff)',
|
||||
display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', padding: 0, zIndex: 1,
|
||||
}}
|
||||
>
|
||||
<X size={10} strokeWidth={3} />
|
||||
</button>
|
||||
</Tooltip>
|
||||
)}
|
||||
|
||||
<input ref={fileRef} type="file" accept="image/jpeg,image/png,image/gif,image/webp,.heic,.heif" style={{ display: 'none' }} onChange={handleFile} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user