fix: clear map leave guard history

This commit is contained in:
Nico 2026-06-15 16:11:20 -07:00
parent 14c01d6152
commit b09edae159
2 changed files with 54 additions and 0 deletions

View File

@ -24,6 +24,9 @@ export default function useUnsavedMapLeaveGuard({
useEffect(() => {
if (!shouldGuardLeave) {
if (historyGuardActiveRef.current && guardedUrlRef.current === window.location.href) {
window.history.back();
}
setPendingLeaveTarget(null);
guardedClickTargetRef.current = null;
confirmedLeaveRef.current = false;

View File

@ -2723,6 +2723,57 @@ test("admin browser back warns when draft edits are unsaved", async ({ page }) =
await expect(page).toHaveURL(/\/$/);
});
test("admin browser back leaves after draft edits are undone clean", async ({ page }) => {
await mockMapShell(page);
const mapState = draftMapState([
{
id: 1376,
location_map_id: 900,
zone_id: 501,
zone_name: "Bakery",
type: "zone",
label: "Bakery",
x: 40,
y: 40,
width: 260,
height: 160,
rotation: 0,
locked: false,
visible: true,
sort_order: 1,
},
], true);
await page.route("**/households/1/locations/10/map", async (route) => {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify(mapState),
});
});
await page.goto("/stores/100/locations/10/map");
await page.evaluate(() => {
window.history.replaceState(window.history.state, "", "/");
window.history.pushState({}, "", "/stores/100/locations/10/map");
});
await expect(page).toHaveURL(/\/stores\/100\/locations\/10\/map$/);
await page.getByRole("button", { name: "Continue Editing" }).click();
await page.getByRole("button", { name: "Map area Bakery" }).click();
await page.getByLabel("Linked zone").selectOption("");
await expect(page.locator(".location-map-status")).toHaveText("Unsaved Draft");
await page.getByRole("button", { name: "Undo" }).click();
await expect(page.locator(".location-map-status")).toHaveText("Draft");
await expect(page.getByRole("heading", { name: "Leave with unsaved changes?" })).toHaveCount(0);
await page.evaluate(() => window.history.back());
await expect(page).toHaveURL(/\/$/);
await expect(page.getByRole("heading", { name: "Leave with unsaved changes?" })).toHaveCount(0);
});
test("admin logout warns when draft edits are unsaved", async ({ page }) => {
await mockMapShell(page);