diff --git a/client/src/components/Collections/usePlaceEdit.test.ts b/client/src/components/Collections/usePlaceEdit.test.ts index afdb10a61..750cb7778 100644 --- a/client/src/components/Collections/usePlaceEdit.test.ts +++ b/client/src/components/Collections/usePlaceEdit.test.ts @@ -1,4 +1,4 @@ -// FE-COMP-PLACEEDIT-001 to FE-COMP-PLACEEDIT-009: editing a place of All places in one of +// FE-COMP-PLACEEDIT-001 to FE-COMP-PLACEEDIT-010: editing a place of All places in one of // its copies, for the desktop dialog and the phone sheet. import { act, renderHook } from '@testing-library/react'; import type { PlacesLibraryDetailResponse } from '@trek/shared'; @@ -172,4 +172,26 @@ describe('usePlaceEdit', () => { await act(() => locked.current.save()); expect(save).not.toHaveBeenCalled(); }); + + it('FE-COMP-PLACEEDIT-010: a form left open closes when another place opens, so its draft is not saved there', async () => { + const save = vi.spyOn(placesLibraryRepo, 'saveDetails').mockResolvedValue(undefined); + const first = copies(); + const other = copies(); + other.entry = { ...other.entry, key: 'trip:99' }; + const { result, rerender } = renderHook(({ data }) => usePlaceEdit({ data, onSaved: vi.fn(), t }), { + initialProps: { data: first }, + }); + act(() => result.current.start()); + act(() => result.current.set('name', 'Typed for the first')); + rerender({ data: other }); + expect(result.current.editing).toBe(false); + expect(result.current.draft).toBeNull(); + await act(() => result.current.save()); + expect(save).not.toHaveBeenCalled(); + + // The same place read again (after a save elsewhere) keeps the form open. + act(() => result.current.start()); + rerender({ data: { ...other } }); + expect(result.current.editing).toBe(true); + }); }); diff --git a/client/src/components/Collections/usePlaceEdit.ts b/client/src/components/Collections/usePlaceEdit.ts index 4eef7ed57..5c6372709 100644 --- a/client/src/components/Collections/usePlaceEdit.ts +++ b/client/src/components/Collections/usePlaceEdit.ts @@ -99,6 +99,15 @@ export function usePlaceEdit({ const [editing, setEditing] = useState(false); const [draft, setDraft] = useState(null); const [saving, setSaving] = useState(false); + // A form left open belongs to the place it was opened on: another place in the same + // dialog or sheet starts closed, so the old draft can never be saved onto it. + const placeKey = data?.entry.key ?? null; + const [formKey, setFormKey] = useState(placeKey); + if (formKey !== placeKey) { + setFormKey(placeKey); + setEditing(false); + setDraft(null); + } const target: EditTarget | null = targets.length > 0 ? targets[0] : null; diff --git a/client/src/repo/placesLibraryRepo.ts b/client/src/repo/placesLibraryRepo.ts index d0536ffc1..0e4d058f5 100644 --- a/client/src/repo/placesLibraryRepo.ts +++ b/client/src/repo/placesLibraryRepo.ts @@ -75,7 +75,8 @@ export const placesLibraryRepo = { /** * A place of All places edited where one of its copies lives: a saved place's - * details in its list, a trip place's in its trip. The other copies keep theirs. + * details in its list, a trip place's in its trip. The server makes the same change + * on every other copy the reader may edit (the copies stay in step). */ async saveDetails( origin: { kind: 'trip'; trip_id: number; place_id: number } | { kind: 'list'; collection_place_id: number },