PO-8902-Route-guard-does-not-trigger - #2852
Conversation
make sure route guard shows when cancelling and changes have been made to the form
There was a problem hiding this comment.
The change needs to be made where the unsaved state is lost, rather than in the route guard.
- Revert the guard change
Infines-mac-offence-details.component.ts, restorecanDeactivate()so it only uses the existing sharedunsavedChangesstate:
canDeactivate(): CanDeactivateTypes {
return !this.finesMacStore.unsavedChanges();
}
Please remove this check:
const hasDraftOffenceChanges = this.finesMacOffenceDetailsStore.offenceDetailsDraft().length > 0;
offenceDetailsDraft is temporary navigation state, not a dirty-state indicator. It remains populated after the offence is copied into the main MAC store, so using it here can show a warning after a successful save.
- Restore the unsaved state after saving the minor creditor
Infines-mac-offence-details-minor-creditor.component.ts, updatehandleMinorCreditorFormSubmit()immediately after the creditor is written to the draft:
this.finesMacOffenceDetailsStore.setOffenceDetailsDraft(offenceDetailsDraft);
this.finesMacOffenceDetailsStore.setMinorCreditorAdded(true);
this.finesMacStore.setUnsavedChanges(true);
this.routerNavigate(FINES_MAC_OFFENCE_DETAILS_ROUTING_PATHS.children.addOffence);
The important new line is:
this.finesMacStore.setUnsavedChanges(true);
Please set the store directly here. Do not call:
this.handleUnsavedChanges(true);
That would also set stateUnsavedChanges on the minor-creditor component itself. Because that child route has its own canDeactivate guard, it could cause the warning to appear while navigating away after pressing Save.
The expected state lifecycle should be:
Minor-creditor form submitted
→ creditor stored in offence draft
→ finesMacStore.unsavedChanges = true
User clicks Cancel on Add an offence
→ outer route guard sees true
→ warning displayed
User saves/reviews the complete offence
→ the offence form’s normal submission emits false
→ finesMacStore.unsavedChanges = false
→ leaving Offence details does not warn
- Move the regression test
Remove the new test fromfines-mac-offence-details.component.spec.ts:
it('should block deactivation when offence draft data exists', ...)
Also remove its now-unused FINES_MAC_OFFENCE_DETAILS_DRAFT_STATE_MOCK import.
Instead, update fines-mac-offence-details-minor-creditor.component.spec.ts. In the existing submission test, after:
component.handleMinorCreditorFormSubmit(formSubmit);
add:
expect(finesMacStore.unsavedChanges()).toBe(true);
We should also have journey coverage for both outcomes:
Save minor-creditor details → return to Add an offence → Cancel → warning appears.
Save minor-creditor details → save/review the complete offence → leave Offence details → no warning appears.
This fixes the point where the state becomes incorrect and keeps the route guard concerned only with actual unsaved state.
make sure route guard shows when cancelling and changes have been made to the form
updating cypress tests
…gger' into PO-8902-Route-guard-does-not-trigger
removing file from diff
iamfrankiemoran
left a comment
There was a problem hiding this comment.
LGTM ✅
@marcmollins just run:
yarn install
then yarn audit:save
feedback comments
fix linting
fix linting
louisbriggs
left a comment
There was a problem hiding this comment.
P2 – Rehydrated dirty drafts do not sync the route guard state
fines-mac-offence-details-add-an-offence-form.component.ts (line 233) marks the form as dirty when offenceDetailsDraftDirty() is true, but it does not also update finesMacStore.unsavedChanges(true).
The top-level route guard reads this value from the store (fines-mac-offence-details.component.ts, line 22), so a restored dirty form can still leave the guard state as false if navigation doesn't go through the form's cancel handler.
Suggested fix:
Synchronise the global unsavedChanges state when hasSavedDraftChanges is applied.
Add a unit test to verify the store value is updated correctly.
P2 – Removal flows update the draft without marking it dirty
fines-mac-offence-details-remove-minor-creditor.component.ts (line 70) and fines-mac-offence-details-remove-imposition.component.ts (line 215) both update offenceDetailsDraft before navigating back, but neither sets offenceDetailsDraftDirty nor updates finesMacStore.unsavedChanges.
If the offence form was pristine before entering the removal confirmation flow, the removal creates an unsaved draft change that the route guard will not detect.
Suggested fix:
Set both offenceDetailsDraftDirty and finesMacStore.unsavedChanges(true) after a successful removal.
Add unit tests covering both the minor creditor and imposition removal flows to verify the route guard state is updated correctly.
resolved comment
|
QA Review - LGTM |
Jira link
https://tools.hmcts.net/jira/browse/PO-8902
Change description
make sure route guard shows when cancelling and changes have been made to the form in question
Testing done
Unit tested, tested locally.
Security Vulnerability Assessment
CVE Suppression: Are there any CVEs present in the codebase (either newly introduced or pre-existing) that are being intentionally suppressed or ignored by this commit?
Checklist