feat(collections): import a file into a list that already exists

The import dialog always made a new list. It now asks where the places go:
a new list, as before and still the default, or a list that is already there.
The picker offers the lists the person may edit, which is their own plus a
shared one where they are an editor or an admin, and starts on the list they
have open.

Adding to a list only ever adds. A place the list already holds is left
exactly as it is, with its status, notes, rating and labels, and counted as
already there in what the import reports; it is recognised the way a single
save recognises one, by provider id, then name, then position. The list keeps
its own name, colour, icon and description whatever the file says, a label
the file brings is created only when the list has none of that name, and new
places are appended after the ones already there so a manual order survives.

The route is POST /api/addons/collections/:id/import, 200 because it adds to
a list rather than making one, and both imports now write their places through
the same helper.
This commit is contained in:
Maurice
2026-09-19 23:12:25 +02:00
parent 7cfec6452d
commit d803efd39d
36 changed files with 978 additions and 81 deletions
+4 -1
View File
@@ -345,10 +345,13 @@ function CollectionsPageDesktop(): React.ReactElement {
/>
)}
{/* A list file as a new list — no active list needed, it makes one (#2198) */}
{/* A list file as a new list, or added to one that is already there (#2198, #2301) */}
{c.showImportFile && (
<ImportCollectionModal
onImport={c.handleImportFile}
onImportInto={c.handleImportFileInto}
lists={c.writableLists}
defaultListId={typeof c.activeId === 'number' ? c.activeId : null}
onReadGpx={c.handleReadGpx}
onClose={() => c.setShowImportFile(false)}
t={t}
@@ -1,4 +1,4 @@
// FE-PAGE-COLL-001 to FE-PAGE-COLL-081
// FE-PAGE-COLL-001 to FE-PAGE-COLL-085
// The Collections page hook. The collection store is replaced by a fixture so
// every handler/branch can be driven directly; the categories request goes
// through MSW, and the websocket module is already mocked in tests/setup.ts.
@@ -1063,6 +1063,65 @@ describe('useCollections — export and import as a file', () => {
expect(result.current.showImportFile).toBe(true)
})
it('FE-PAGE-COLL-082: a file added to another list lands on it and says what it did', async () => {
const seen: unknown[] = []
server.use(http.post('/api/addons/collections/:id/import', async ({ request, params }) => {
seen.push({ id: params.id, body: await request.json() })
return HttpResponse.json({ collection: { id: 11, name: 'Lisbon 2027' }, imported: 2, skipped: 0, duplicates: 1 })
}))
const { result } = await setup()
await act(async () => { await result.current.handleImportFileInto(listFile as never, 11) })
expect(seen[0]).toEqual({ id: '11', body: { file: listFile } })
expect(store.loadAll).toHaveBeenCalled()
expect(navigate).toHaveBeenCalledWith('/collections/11')
expect(addToast).toHaveBeenCalledWith('2 added to Lisbon 2027, 1 were already there', 'success', undefined)
expect(result.current.showImportFile).toBe(false)
})
it('FE-PAGE-COLL-083: adding to the list that is open refreshes it instead of navigating', async () => {
server.use(http.post('/api/addons/collections/:id/import', () =>
HttpResponse.json({ collection: { id: 11, name: 'Lisbon 2027' }, imported: 2, skipped: 0, duplicates: 0 })))
store.activeId = 11
const { result } = await setup()
navigate.mockClear()
await act(async () => { await result.current.handleImportFileInto(listFile as never, 11) })
expect(store.refreshActive).toHaveBeenCalled()
expect(navigate).not.toHaveBeenCalledWith('/collections/11')
expect(addToast).toHaveBeenCalledWith('2 places added to Lisbon 2027', 'success', undefined)
})
it('FE-PAGE-COLL-084: a file whose places were all there already says so instead of claiming a success', async () => {
server.use(http.post('/api/addons/collections/:id/import', () =>
HttpResponse.json({ collection: { id: 11, name: 'Lisbon 2027' }, imported: 0, skipped: 0, duplicates: 2 })))
const { result } = await setup()
await act(async () => { await result.current.handleImportFileInto(listFile as never, 11) })
expect(addToast).toHaveBeenCalledWith('Every place in the file is already in Lisbon 2027', 'info', undefined)
})
it('FE-PAGE-COLL-085: only lists this person may write to are offered as a target', async () => {
const shared = (id: number, role: string | null) => ({
...collection({ id, is_owner: false }),
members: role === null ? [] : [{ user_id: 7, username: 'me', status: 'accepted', role }],
}) as unknown as Collection
store.collections = [
collection({ id: 1 }),
shared(2, 'editor'),
shared(3, 'admin'),
shared(4, 'viewer'),
shared(5, null),
{ ...shared(6, 'editor'), members: [{ user_id: 7, username: 'me', status: 'pending', role: 'editor' }] } as unknown as Collection,
]
const { result } = await setup()
expect(result.current.writableLists.map(l => l.id)).toEqual([1, 2, 3])
})
it('FE-PAGE-COLL-076: a renamed import carries the new name to the server', async () => {
const seen: unknown[] = []
server.use(http.post('/api/addons/collections/import', async ({ request }) => {
+40 -1
View File
@@ -170,6 +170,17 @@ export function useCollections() {
useEffect(() => { setShowShare(false) }, [activeId])
const ownedLists = useMemo(() => collections.filter(c => c.is_owner !== false), [collections])
/**
* Lists a file may be added to: the person's own, plus a shared one where
* they are an editor or an admin. Same rule the server applies on the way in,
* so the import dialog never offers a list the import would refuse.
*/
const writableLists = useMemo(
() => collections.filter(c => c.is_owner !== false || (c.members ?? []).some(
m => m.user_id === currentUserId && m.status === 'accepted' && (m.role === 'editor' || m.role === 'admin'),
)),
[collections, currentUserId],
)
const sharedLists = useMemo(() => collections.filter(c => c.is_owner === false), [collections])
// Labels are per-collection, so never apply them on the "All saved" union.
@@ -260,6 +271,33 @@ export function useCollections() {
setShowImportFile(false)
}, [loadAll, navigate, toast, t])
/**
* Read a chosen file into a list that is already there.
*
* Lands on that list, for the same reason a new one does. Nothing in it is
* overwritten: the server counts the places it already had and leaves them,
* and the toast says so rather than letting a file quietly do less than it
* looked like it would.
*/
const handleImportFileInto = useCallback(async (file: CollectionFile, collectionId: number) => {
const result = await collectionsApi.importFileInto(collectionId, { file })
const name = (result.collection as Collection).name
const duplicates = result.duplicates ?? 0
await loadAll()
if (activeId === collectionId) refreshActive()
else navigate(`/collections/${collectionId}`)
if (result.imported === 0 && duplicates > 0) {
toast.info(t('collections.file.doneIntoNothing', { name }))
} else if (duplicates > 0) {
toast.success(t('collections.file.doneIntoDuplicates', { count: result.imported, duplicates, name }))
} else if (result.skipped > 0) {
toast.info(t('collections.file.doneSkipped', { count: result.imported, skipped: result.skipped }))
} else {
toast.success(t('collections.file.doneInto', { count: result.imported, name }))
}
setShowImportFile(false)
}, [activeId, loadAll, refreshActive, navigate, toast, t])
const handleDeleteList = useCallback(async () => {
if (confirmDeleteList == null) return
const id = confirmDeleteList
@@ -467,7 +505,8 @@ export function useCollections() {
showAddPlace, setShowAddPlace, handlePlaceAdded,
showImport, setShowImport,
exporting, handleExportList,
showImportFile, setShowImportFile, handleImportFile, handleReadGpx,
showImportFile, setShowImportFile, handleImportFile, handleImportFileInto, handleReadGpx,
writableLists,
confirmDeleteList, setConfirmDeleteList,
mobileRailOpen, setMobileRailOpen,
showShare, setShowShare, handleAfterLeave,