Skip to content

fix: reuse Cancel confirmation for browser-back guard (OPS-1081) - #6565

Open
rajohnson90 wants to merge 4 commits into
mainfrom
OPS-1081/reuse-cancel-modal
Open

rajohnson90 wants to merge 4 commits into
mainfrom
OPS-1081/reuse-cancel-modal

Conversation

@rajohnson90

Copy link
Copy Markdown
Contributor

What changed

This PR changes how the browser's Back button is guarded on step 1 (project selection) of the Create Agreement wizard. A prior merged PR (#6545) added a distinct "Leave without finishing?" modal for this case. This PR replaces that with reuse of the page's existing Cancel confirmation — same heading/buttons/handler — so confirming cancels the agreement (deletes a new draft, redirects to /agreements) exactly like the page's own Cancel button does, instead of resuming navigation to wherever Back was headed.

It also fixes three issues an internal AI-assisted review caught in that change before asking for human review.

frontend/src/pages/agreements/StepSelectProject.jsx

  • Dropped the separate useUnsavedChangesBlocker / SaveChangesAndExitModal pairing (still used elsewhere — unaffected) in favor of calling useBlocker directly.
  • When browser Back is blocked, an effect opens the same Cancel-confirmation ConfirmationModal the Cancel button uses, with the same handleCancel handler.
  • Added handleCloseModal to reset a pending blocked navigation when the modal closes (either button), and kept isBypassingRef so the Cancel confirm's own redirect isn't re-blocked.
  • Fixed a bug the review caught: the predicate's !showModal guard (meant only to avoid stacking a second modal) was letting any navigation attempt slip through unguarded once the modal was open, abandoning the pending delete/redirect. Removed that guard — the shared modal/state no longer needs it.

frontend/src/pages/agreements/StepSelectProject.test.jsx

  • Replaced the old hook-mocking approach with a react-router-dom useBlocker mock that captures the real predicate, so tests exercise actual behavior instead of asserting on mock call arguments.
  • Added tests for the predicate still blocking while the modal is open, the modal matching Cancel's exact wording/buttons, and the "Continue Editing" / "Cancel Agreement" button outcomes.
  • Added tests proving deleteAgreement actually fires via the new browser-Back trigger (and is skipped in edit mode) — a gap the review flagged, since this PR is what first routes Back into a data-write path.
  • Fixed a test that looked like it verified isBypassingRef but didn't — hasProjectChanged was pinned false from before mount in its setup, so the assertion passed independent of the ref. Now the project is selected after mount so the assertion is meaningful.

Decisions reviewers may ask about

  • Why does confirming Cancel via browser Back redirect to /agreements instead of resuming navigation to wherever Back was headed? That's the deliberate point of this PR — reusing the exact same Cancel flow means the same outcome (delete the draft, redirect to /agreements), not a different "continue navigating" behavior.
  • Why was !showModal removed from the blocker predicate entirely, rather than gating the duplicate-modal concern a different way? With the Cancel-button and Back-triggered opens now sharing one modal/state, re-running the open-modal effect while already open is just a harmless no-op (same props, no remount) — so there's no longer a reason to suppress the guard itself while the modal is open.
  • Why two commits? The first made the behavior change; the second closes three S1 findings (one implementation bug, two test gaps) found by this session's own /ops-pr-review run before asking for human review.

Issue

OPS-1081

How to test

Automated:

cd frontend && bun run test --watch=false -- StepSelectProject

Manual (must navigate in-SPA — a hard page load to /agreements/create won't let the browser-Back guard fire, since that's a cross-document history entry, not a same-document popstate):

  1. docker compose up --build (or against the already-running dev stack), log in.
  2. From the home page, use the Create → Agreement nav link to reach step 1 ("Select a Project").
  3. Select any research project.
  4. Press the browser's Back button → the same "Are you sure you want to cancel creating a new agreement?" modal appears (not a separate "Leave without finishing?" wording) — see screenshots below.
  5. Click "Continue Editing" → modal closes, selection is still intact, no navigation happened.
  6. Press Back again, then "Cancel Agreement" → redirected to /agreements, draft agreement deleted, success alert shown.
  7. Repeat steps 2-3, then click the page's own "Cancel" button instead of Back — same modal, same outcome, confirming the two triggers share one flow.
  8. With the Cancel modal open (from either trigger), press browser Back once more before clicking a button — confirm it does not silently bypass the confirmation (this was the S1 bug fixed in the second commit).

A11y impact

  • No accessibility-impacting changes in this PR
  • Accessibility changes included and validated against WCAG 2.1 AA intent
  • Any temporary suppression includes A11Y-SUPPRESSION metadata (owner, expires, rationale)

Storybook

  • No UI component changes in this PR
  • Story added for new component in src/components/UI/
  • Story updated to reflect changed props/states
  • N/A — change is page-specific or non-visual

Screenshots

Step 1 with a project selected, after pressing browser Back — the Cancel-confirmation modal appears in place of the old distinct "Leave without finishing?" wording:

Full page with Cancel confirmation modal open after browser Back

Close-up of the modal itself — same heading/buttons as the page's own Cancel button:

Close-up of the Cancel confirmation modal

Definition of Done Checklist

  • OESA: Code refactored for clarity
  • OESA: Dependency rules followed
  • Automated unit tests updated and passed
  • Automated integration tests updated and passed
  • Automated quality tests updated and passed
  • Automated load tests updated and passed
  • Automated a11y tests updated and passed
  • Automated security tests updated and passed
  • 90%+ Code coverage achieved
  • Form validations updated

Links

N/A

@rajohnson90 rajohnson90 self-assigned this Nov 19, 2026
@rajohnson90 rajohnson90 added ready-for-human-review Shared AI review is clean on the latest commit; ready for a human reviewer preview labels Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Reviewer: @Santi-3rd (picked by rotation)
What to check: intent — does this meet the linked issue's acceptance criteria? — and risk. Not a re-review of CI or AI findings. See docs/REVIEW.md.

Not the right person? The PR author can comment /reroll to pick the next available reviewer.

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Preview environment

URL https://pr-6565.ops.opre.acf.gov
Commit d4dd6b8
Status Ready
Last seeded 2026-11-19T22:51:58Z

Log in: choose Sign in with FakeAuth and pick a role: System Owner, Budget Team Member, Procurement Team Member, Division Director, User Demo, Administrative Power User or Read Only User.

Data: the first deploy seeds test data. Later pushes only run migrations, so data you enter survives fix commits. To reset it, open Actions > Preview Create > Run workflow and enter 6565.

Lifetime: removed when the PR closes, the preview label is removed, or after 5 days with no pushes or comments (with a warning a day before).

Limits: FakeAuth only; no Storybook, /docs or document upload. If you edit a migration that already ran here, reseed.

Docs · Latest run

@github-actions github-actions Bot mentioned this pull request Nov 19, 2026
3 tasks

@josbell josbell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shared AI Review — PR #6565

Head SHA reviewed: 5db3ec0d1 · Scope: Fix PR (S0/S1 block) · Tier: 3 (routes browser-Back into a deleteAgreement data-write path) · Mode: Full review, reviewer mode · N: 3 skeptic turns

Four specialist reviewers (pragmatic, frontend, accessibility, software-architect) reviewed this PR independently, and three independently found the same bug below. Three independent skeptics re-derived severity from the code; 2 of 3 (including the accessibility domain specialist) land on S1, which is why this is REQUEST_CHANGES. See the inline comment for the blocking finding.

Non-blocking

[S3] frontend/src/pages/agreements/StepSelectProject.jsx:49-76
What breaks: Nothing currently — the isBypassingRef/blockerRef/bypass-effect machinery this PR adds duplicates frontend/src/hooks/useUnsavedChangesBlocker.hooks.js:38-48 almost line-for-line instead of sharing it.
Scenario: A future fix to this ref/effect pattern in one copy could be forgotten in the other.
Fix: Extract a shared useBlockerWithBypassRef(...) hook for both call sites. Discretionary.

Pre-existing

[Pre-existing S2] frontend/src/components/UI/Modals/ConfirmationModal.jsx (~lines 85, 101-119)
aria-describedby="ops-modal-description" points at a <p> that only renders when a description prop is passed; StepSelectProject never passes one, in base or head. Unrelated to this PR.

No author shared-review comment (<!-- shared-review sha=... -->) exists on this PR to compare against — the PR body describes an internal AI-assisted pass that fixed three other issues in the second commit, but that's not a tracked, comparable result. The finding below is new to this run.

Verdict: BLOCKING (1)

// Closes the Cancel confirmation (whichever trigger opened it — the Cancel button or
// the browser-Back guard below) and discards any pending blocked back-navigation so the
// user stays on the page instead of leaving a blocked transition dangling.
const handleCloseModal = (value) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[S1] frontend/src/pages/agreements/StepSelectProject.jsx:81-86
What breaks: handleCloseModal is passed as ConfirmationModal's setShowModal prop on every render (replacing the previously-stable useState setter), without useCallback. ConfirmationModal's handleKeyDown (useCallback, deps include setShowModal) and its focus-trap useEffect (deps include handleKeyDown) therefore tear down and rebuild on every re-render of StepSelectProject while the dialog is open. Teardown unconditionally refocuses whatever was focused when that effect instance mounted — the page's background trigger button, which isn't inert/aria-hidden while the dialog is open — and setup unconditionally refocuses the dialog's first button, with no check for where focus currently is.
Scenario: User opens the Cancel-confirmation dialog (via Cancel, or a blocked browser Back), tabs to "Continue Editing" to read it, then presses browser Back again before clicking anything — the exact sequence this PR's own new test ("still blocks further navigation while the Cancel confirmation dialog is already open") and manual-test step 8 both exercise. The resulting re-render recreates handleCloseModal, rebuilding the focus-trap effect and snapping focus away from "Continue Editing" — briefly through the background trigger button — back to "Cancel Agreement," with no visual cue, while the user is still deciding. ConfirmationModal is fully mocked in StepSelectProject.test.jsx, so no test (old or new) exercises the real focus-trap behavior this PR's change affects.
Fix: Wrap handleCloseModal in React.useCallback with an empty dependency array (setShowModal and blockerRef are both already stable): const handleCloseModal = React.useCallback((value) => { setShowModal(value); if (!value && blockerRef.current.state === "blocked") blockerRef.current.reset(); }, []). Add a test that exercises the real ConfirmationModal (not the current stub) across a repeated blocked-navigation attempt.

@weimiao67 weimiao67 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me as long as findings from Josbell are resolved.

This branch was successfully deployed

1 active deployment
preview — d4dd6b8f Deployed Nov 23, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview ready-for-human-review Shared AI review is clean on the latest commit; ready for a human reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants