diff --git a/client/src/components/Collections/CollectionPicker.test.tsx b/client/src/components/Collections/CollectionPicker.test.tsx index 1d17eab30..335346790 100644 --- a/client/src/components/Collections/CollectionPicker.test.tsx +++ b/client/src/components/Collections/CollectionPicker.test.tsx @@ -1,4 +1,4 @@ -// FE-COMP-COLPICKER-001 to FE-COMP-COLPICKER-023 +// FE-COMP-COLPICKER-001 to FE-COMP-COLPICKER-024 import React from 'react' import { render, screen, fireEvent, waitFor, within } from '../../../tests/helpers/render' import type { Collection, CollectionDetailResponse, CollectionPlace } from '@trek/shared' @@ -306,10 +306,10 @@ describe('CollectionPicker in the place dialog', () => { * places beside the lists', read only once it is chosen. */ describe('CollectionPicker with All places', () => { - // On an earlier trip, on the trip being planned, and in a list where it was visited. + // On an earlier trip, on another one, and in a list where it was visited. const pastTrip = libraryEntry({ key: 'trip:70', name: 'Old Harbour', address: 'Quay 3', lat: 54.3, lng: 10.1, category: { id: 3, name: 'Bar', color: null, icon: null }, origins: [tripOrigin({ trip_id: 5, place_id: 70 })] }) - const thisTrip = libraryEntry({ key: 'trip:71', name: 'Here Already', origins: [tripOrigin({ trip_id: 8, place_id: 71 })] }) + const otherTrip = libraryEntry({ key: 'trip:71', name: 'Night Market', origins: [tripOrigin({ trip_id: 8, place_id: 71 })] }) const visited = libraryEntry({ key: 'list:72', name: 'Seen Tower', origins: [listOrigin({ collection_place_id: 72, status: 'visited' })] }) beforeEach(() => { @@ -319,15 +319,15 @@ describe('CollectionPicker with All places', () => { vi.spyOn(collectionsApi, 'get').mockImplementation(async (id: number) => id === 1 ? detail(listA, [zebra]) : detail(listB, [alpha, noCoords]), ) - vi.spyOn(placesLibraryRepo, 'list').mockResolvedValue(libraryPage([pastTrip, thisTrip, visited])) + vi.spyOn(placesLibraryRepo, 'list').mockResolvedValue(libraryPage([pastTrip, otherTrip, visited])) }) afterEach(() => { vi.restoreAllMocks() }) - it('FE-COMP-COLPICKER-022: All places shows the places of other trips and hands their facts to the form', async () => { - const { props } = setup({ tripId: 8 }) + it('FE-COMP-COLPICKER-022: All places shows the places of the trips beside the lists and hands their facts to the form', async () => { + const { props } = setup() await screen.findByText('Zebra Cafe') expect(placesLibraryRepo.list).not.toHaveBeenCalled() fireEvent.click(screen.getByRole('button', { name: /All lists/ })) @@ -335,9 +335,12 @@ describe('CollectionPicker with All places', () => { await screen.findByText('Old Harbour') expect(screen.getByText('Seen Tower')).toBeInTheDocument() - // The trip being planned has it already. - expect(screen.queryByText('Here Already')).not.toBeInTheDocument() + expect(screen.getByText('Night Market')).toBeInTheDocument() expect(screen.queryByText('Zebra Cafe')).not.toBeInTheDocument() + // Its count is the places it holds, each once. + fireEvent.click(screen.getByRole('button', { name: /All places/ })) + expect(within(screen.getByRole('listbox')).getByRole('option', { name: /All places/ })).toHaveTextContent('3') + fireEvent.click(within(screen.getByRole('listbox')).getByRole('option', { name: /All places/ })) fireEvent.click(screen.getByRole('button', { name: /Old Harbour/ })) expect(props.onSelect).toHaveBeenCalledWith({ name: 'Old Harbour', address: 'Quay 3', lat: 54.3, lng: 10.1, category_id: 3 }) @@ -355,4 +358,20 @@ describe('CollectionPicker with All places', () => { expect(screen.getByText('Seen Tower')).toBeInTheDocument() expect(screen.queryByText('Old Harbour')).not.toBeInTheDocument() }) + + it('FE-COMP-COLPICKER-024: All lists shows a place kept in two lists once, each list still shows its own', async () => { + const first = place({ id: 20, collection_id: 1, name: 'Belem Tower', source_trip_id: 1, source_place_id: 1 }) + const second = place({ id: 21, collection_id: 2, name: 'Belem Tower', source_trip_id: 1, source_place_id: 1 }) + vi.mocked(collectionsApi.get).mockImplementation(async (id: number) => + id === 1 ? detail(listA, [first]) : detail(listB, [second]), + ) + setup() + expect(await screen.findAllByText('Belem Tower')).toHaveLength(1) + fireEvent.click(screen.getByRole('button', { name: /All lists/ })) + const menu = screen.getByRole('listbox') + expect(within(menu).getByRole('option', { name: /All lists/ })).toHaveTextContent('1') + expect(within(menu).getByRole('option', { name: /Rome/ })).toHaveTextContent('1') + fireEvent.click(within(menu).getByRole('option', { name: /Rome/ })) + expect(screen.getByText('Belem Tower')).toBeInTheDocument() + }) }) diff --git a/client/src/components/Collections/CollectionPicker.tsx b/client/src/components/Collections/CollectionPicker.tsx index 60613e1c3..16b7e16d2 100644 --- a/client/src/components/Collections/CollectionPicker.tsx +++ b/client/src/components/Collections/CollectionPicker.tsx @@ -2,7 +2,7 @@ import React, { useEffect, useId, useMemo, useRef, useState } from 'react' import { Search, Bookmark, Loader2, ChevronDown, Check, Layers, Plus, SearchX, Globe2 } from 'lucide-react' import PlaceAvatar from '../shared/PlaceAvatar' import { collectionsApi } from '../../api/collections' -import { loadUnionPlaces } from '../../store/collectionUnion' +import { distinctPlaces, loadUnionPlaces } from '../../store/collectionUnion' import { pickedFromLibrary, usePickerLibrary, type PickerLibraryPlace } from './usePickerLibrary' import { STATUS_META, STATUS_ORDER } from '../../pages/collections/collectionsModel' import type { CollectionPlace, CollectionStatus } from '@trek/shared' @@ -23,8 +23,6 @@ interface CollectionPickerProps { /** Trip bounding box used for autocomplete — sorts the saved places by * proximity to the trip so the relevant ones surface first. */ bias?: LocationBias - /** The trip the place is added to: its own places stay out of All places, they are in it already. */ - tripId?: number /** Fills the place form from the chosen place (handleSelectMapsResult): a saved place whole, a place of All places by its facts. */ onSelect: (place: Record) => void t: TranslationFn @@ -147,7 +145,7 @@ function FilterDropdown({ label, current, options, onSelect, lead }: { * place saved on an earlier trip can be dropped straight into the form. * Desktop only — gated by the caller. */ -export default function CollectionPicker({ bias, tripId, onSelect, t }: CollectionPickerProps): React.ReactElement { +export default function CollectionPicker({ bias, onSelect, t }: CollectionPickerProps): React.ReactElement { const searchId = useId() const [places, setPlaces] = useState([]) const [lists, setLists] = useState([]) @@ -183,19 +181,19 @@ export default function CollectionPicker({ bias, tripId, onSelect, t }: Collecti const PAGE = 10 const [shown, setShown] = useState(PAGE) + // All lists shows a place kept in several lists once, as All saved does; one list shows its own. + const distinct = useMemo(() => distinctPlaces(places), [places]) const visible = useMemo(() => { const rows = listFilter === 'places' - ? library.places - .filter(p => (tripId == null || !p.tripIds.includes(tripId)) && (statusFilter === 'all' || p.statuses.includes(statusFilter))) - .map(libraryRow) - : places + ? library.places.filter(p => statusFilter === 'all' || p.statuses.includes(statusFilter)).map(libraryRow) + : (listFilter === 'all' ? distinct : places) .filter(p => (listFilter === 'all' || p.collection_id === listFilter) && (statusFilter === 'all' || p.status === statusFilter)) .map(savedRow) const list = rows.filter(r => textMatchesSearch([r.name, r.address], search)) if (center) list.sort((a, b) => distanceTo(a, center) - distanceTo(b, center)) else list.sort((a, b) => a.name.localeCompare(b.name)) return list - }, [places, library.places, tripId, search, center, listFilter, statusFilter]) + }, [places, distinct, library.places, search, center, listFilter, statusFilter]) // Searching or filtering starts a new list; keeping the old offset would drop // the user somewhere in the middle of it. @@ -206,7 +204,7 @@ export default function CollectionPicker({ bias, tripId, onSelect, t }: Collecti const listOpts: Opt[] = [ { key: 'places', label: t('collections.library.title'), icon: , count: library.loaded ? library.places.length : undefined }, - { key: 'all', label: t('collections.picker.allLists'), icon: , count: places.length }, + { key: 'all', label: t('collections.picker.allLists'), icon: , count: distinct.length }, ...lists.map(l => ({ key: l.id, label: l.name, diff --git a/client/src/components/Collections/usePickerLibrary.test.ts b/client/src/components/Collections/usePickerLibrary.test.ts index 2a4225e2f..917d05dde 100644 --- a/client/src/components/Collections/usePickerLibrary.test.ts +++ b/client/src/components/Collections/usePickerLibrary.test.ts @@ -11,7 +11,7 @@ afterEach(() => { }); describe('pickerPlaceOf', () => { - it('FE-COMP-PICKERLIB-001: names the picture by the first use and keeps the trips and the list statuses', () => { + it('FE-COMP-PICKERLIB-001: names the picture by the first use and keeps the list statuses', () => { const entry = libraryEntry({ key: 'trip:70', category: { id: 3, name: 'Bar', color: null, icon: null }, @@ -22,7 +22,7 @@ describe('pickerPlaceOf', () => { ], }); const place = pickerPlaceOf(entry); - expect(place).toMatchObject({ key: 'trip:70', id: 70, name: 'Colosseum', tripIds: [5, 6], statuses: ['visited'] }); + expect(place).toMatchObject({ key: 'trip:70', id: 70, name: 'Colosseum', statuses: ['visited'] }); expect(pickerPlaceOf(libraryEntry({ origins: [listOrigin({ collection_place_id: 80 })] })).id).toBe(80); expect(pickedFromLibrary(place)).toEqual({ name: 'Colosseum', diff --git a/client/src/components/Collections/usePickerLibrary.ts b/client/src/components/Collections/usePickerLibrary.ts index 32baa87a1..3345a6229 100644 --- a/client/src/components/Collections/usePickerLibrary.ts +++ b/client/src/components/Collections/usePickerLibrary.ts @@ -19,8 +19,6 @@ export interface PickerLibraryPlace { category: PlacesLibraryEntry['category']; /** The statuses its lists give it, for the picker's status filter. */ statuses: CollectionStatus[]; - /** The trips it is in already. */ - tripIds: number[]; } /** An entry of All places as a row of the picker. */ @@ -36,7 +34,6 @@ export function pickerPlaceOf(entry: PlacesLibraryEntry): PickerLibraryPlace { image_url: entry.image_url, category: entry.category, statuses: entry.origins.flatMap((o) => (o.kind === 'list' ? [o.status] : [])), - tripIds: entry.origins.flatMap((o) => (o.kind === 'trip' ? [o.trip_id] : [])), }; } diff --git a/client/src/components/Planner/PlaceFormModal.tsx b/client/src/components/Planner/PlaceFormModal.tsx index 69bd1f0cc..83987e5d0 100644 --- a/client/src/components/Planner/PlaceFormModal.tsx +++ b/client/src/components/Planner/PlaceFormModal.tsx @@ -896,7 +896,7 @@ export default function PlaceFormModal(props: PlaceFormModalProps) { )} {twoColumn && ( - + )}