mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
fix(places): an edit left open does not follow onto the next place
In All places the edit state outlived the place it was opened on, so on the phone a draft started on one place and left open was saved onto the next place the sheet showed. The form now closes when another place opens.
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -99,6 +99,15 @@ export function usePlaceEdit({
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [draft, setDraft] = useState<Draft | null>(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;
|
||||
|
||||
|
||||
@@ -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 },
|
||||
|
||||
Reference in New Issue
Block a user