diff --git a/frontend/src/hooks/useLocationMapViewControls.js b/frontend/src/hooks/useLocationMapViewControls.js index a55280f..8e079fa 100644 --- a/frontend/src/hooks/useLocationMapViewControls.js +++ b/frontend/src/hooks/useLocationMapViewControls.js @@ -76,7 +76,6 @@ export default function useLocationMapViewControls({ setLayersOpen(false); if ((mode === "edit" || previewDraft) && (hasUnsavedChanges || mapState?.draft_map)) { setPreviewDraft(true); - setSelectedObjectKey(null); return; } setPreviewDraft(false); diff --git a/frontend/tests/location-map-manager.spec.ts b/frontend/tests/location-map-manager.spec.ts index dfef9b0..71e6905 100644 --- a/frontend/tests/location-map-manager.spec.ts +++ b/frontend/tests/location-map-manager.spec.ts @@ -1238,6 +1238,62 @@ test("admin view mode previews the edited draft when a saved draft exists", asyn await expect(page.locator(".location-map-object", { hasText: "Draft Bakery" })).toBeVisible(); }); +test("admin keeps the selected draft area when switching to view mode", async ({ page }) => { + await mockMapShell(page); + + const publishedObjects = [ + { + id: 1211, + location_map_id: 901, + zone_id: 501, + zone_name: "Bakery", + type: "zone", + label: "Live Bakery", + x: 40, + y: 40, + width: 260, + height: 160, + rotation: 0, + locked: false, + visible: true, + sort_order: 1, + }, + ]; + const draftObjects = [ + { + ...publishedObjects[0], + id: 1311, + location_map_id: 900, + label: "Draft Bakery", + x: 80, + y: 80, + }, + ]; + + await page.route("**/households/1/locations/10/map", async (route) => { + await route.fulfill({ + status: 200, + contentType: "application/json", + body: JSON.stringify(draftAndPublishedMapState(draftObjects, publishedObjects, true)), + }); + }); + + await page.goto("/stores/100/locations/10/map"); + + await page.getByRole("button", { name: "Edit Draft" }).click(); + const draftBakeryArea = page.getByRole("button", { name: "Map area Draft Bakery" }); + await draftBakeryArea.click(); + await expect(page.locator(".location-map-sheet-header strong")).toHaveText("Draft Bakery"); + await expect(page.locator(".location-map-object.is-selected")).toHaveAttribute("data-object-key", "1311"); + + await page.getByRole("button", { name: "View", exact: true }).click(); + + await expect(page.locator(".location-map-status")).toHaveText("Viewing Draft"); + await expect(page.locator(".location-map-object.is-selected")).toHaveAttribute("data-object-key", "1311"); + await expect(page.locator(".location-map-sheet-header strong")).toHaveText("Draft Bakery"); + await expect(page.getByRole("button", { name: "Close selected map area" })).toBeVisible(); +}); + test("admin edit mode hides unavailable draft actions until changes exist", async ({ page }) => { await mockMapShell(page);