feat: warning when navigating away from page with unsaved changes#18065
feat: warning when navigating away from page with unsaved changes#18065
Conversation
📝 WalkthroughWalkthroughA new unsaved changes warning system has been implemented to alert users before navigating away from or closing a form with unsaved changes. This includes a new custom React hook, integration into the AppConfigForm component, Norwegian translation, test coverage, and test infrastructure updates. Changes
Sequence DiagramsequenceDiagram
participant User
participant Component as AppConfigForm Component
participant Hook as useUnsavedChangesWarning Hook
participant Router as React Router
participant Dialog as Confirmation Dialogue
User->>Component: Make form changes
Component->>Component: Update appConfig state
Component->>Component: Compute hasUnsavedChanges
Component->>Hook: Pass hasUnsavedChanges & message
Hook->>Router: Register blocker with hasUnsavedChanges=true
User->>Router: Attempt navigation
Router->>Hook: Trigger blocker
Hook->>Dialog: Show confirm(message)
Dialog->>User: Display confirmation prompt
alt User confirms
User->>Dialog: Click OK
Hook->>Router: Call blocker.proceed()
Router->>Component: Navigate away
else User cancels
User->>Dialog: Click Cancel
Hook->>Router: Call blocker.reset()
Router->>Component: Remain on page
end
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
95f5f32 to
6214861
Compare
811a7b3 to
2d60ce2
Compare
Ildest
left a comment
There was a problem hiding this comment.
Se bra ut Jonas! Forslag til en liten omformulering, men ikke et must. Prøver å unngå adverb skapt med u, unntatt de som er veldig kjent, som ugjort.
Co-authored-by: Gørild Døhl <gorild.dohl@digdir.no>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #18065 +/- ##
==========================================
+ Coverage 95.16% 95.20% +0.03%
==========================================
Files 2501 2506 +5
Lines 32446 32605 +159
Branches 3844 3872 +28
==========================================
+ Hits 30877 31041 +164
+ Misses 1221 1219 -2
+ Partials 348 345 -3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…igating-away-from-about-app-page
…igating-away-from-about-app-page
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@src/Designer/frontend/libs/studio-hooks/src/hooks/useUnsavedChangesWarning.ts`:
- Around line 14-24: useBlocker in useUnsavedChangesWarning relies on a data
router but both studio-root/index.tsx and dashboard/index.tsx currently use
BrowserRouter (legacy) so in-app navigation blocking fails; migrate those roots
to use a data router (e.g., createBrowserRouter) and render via RouterProvider
so useBlocker works: replace the BrowserRouter instantiation/import with
createBrowserRouter(routeDefinitions) and wrap the app in <RouterProvider
router={router}> (updating route definitions to the data-router shape), update
any navigation usages if needed, and keep useUnsavedChangesWarning (and its
useBlocker call) unchanged so it receives the correct router context.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 793c51c8-5cce-4a8f-8197-9df06ba8431a
📒 Files selected for processing (6)
src/Designer/frontend/app-development/features/appSettings/components/TabsContent/Tabs/AboutTab/AppConfigForm/AppConfigForm.tsxsrc/Designer/frontend/language/src/nb.jsonsrc/Designer/frontend/libs/studio-hooks/src/hooks/index.tssrc/Designer/frontend/libs/studio-hooks/src/hooks/useUnsavedChangesWarning.test.tssrc/Designer/frontend/libs/studio-hooks/src/hooks/useUnsavedChangesWarning.tssrc/Designer/frontend/testing/setupTests.ts
JamalAlabdullah
left a comment
There was a problem hiding this comment.
Very good enhancement! I tested and most of them works fine, I just noticed two things you can see them in the video i added. I am not sure if these planned to be resolved in this issue ?
- The warning message does not work with optional choices
- changing in switch in the delegering card keep the global save button active.
Screen.Recording.2026-03-06.at.11.40.44.mov
|
@JamalAlabdullah |
Description
Adds a warning when navigating away from 'om appen' with unsaved changes.
Should probably not be merged before #18063
PR adds a new hook in
studio-hooks:useUnsavedChangesWarningsince both traditional navigation and SPA/react-router navigation need different handlinguseBlocker/useBeforeUnloadText
Added a text for the warning dialog:
altinn-studio/src/Designer/frontend/language/src/nb.json
Line 328 in 6214861
Screen.Recording.2026-03-06.at.09.18.34.mov
Verification
Summary by CodeRabbit
New Features
Tests