Skip to content

QuestionnaireForm prematurely clears dirty state, disabling unsaved changes guard on failed submission #15855

@anusha19murthy

Description

@anusha19murthy

Describe the bug

QuestionnaireForm disables the unsaved changes navigation guard at the beginning of handleSubmit by calling setIsDirty(false) before validation and before async submission completes.

If client-side validation fails or server-side submission fails, the navigation guard remains disabled. Users can navigate away without any warning, resulting in silent loss of entered clinical data.


To Reproduce

  1. Open any encounter and start filling a questionnaire (e.g., vitals, daily round, assessment).
  2. Enter data in multiple fields.
  3. Leave at least one required field empty.
  4. Click "Submit".
  5. A validation error appears.
  6. Without correcting the error, click browser back or navigate to another page.
  7. Observe: Navigation proceeds with no "unsaved changes" prompt and all entered data is lost.

Alternative scenario:

  1. Fill the form completely.
  2. Click "Submit".
  3. Server returns a validation error (e.g., constraint violation).
  4. Navigate away.
  5. Observe: No navigation guard warning appears; data is lost.

Expected behavior

The navigation guard (useNavigationPrompt) should remain active until the form is successfully submitted.

Users should receive an "unsaved changes" warning if:

  • Client-side validation fails, or
  • Server-side submission fails.

setIsDirty(false) should only execute after confirmed successful submission.


Screenshots

Not applicable — behavior occurs during normal navigation after submit attempt.


Desktop (please complete the following information):

  • OS: Windows 11
  • Browser: Chrome
  • Version: Latest stable

Smartphone (please complete the following information):

  • Device: Not tested
  • OS: Not tested
  • Browser: Not tested
  • Version: Not tested

Additional context

setIsDirty(false) is currently called at the top of handleSubmit before validation logic and before submitBatch resolves.

The navigation guard is defined as:
Because isDirty is cleared prematurely, the guard is disabled even if submission does not succeed.

A minimal fix would be moving setIsDirty(false) into the onSuccess callback of the submitBatch mutation so that the dirty flag is cleared only after confirmed successful submission.


🚨 DO NOT EDIT BELOW THIS LINE 🚨

Instructions for Requesting Assignment:

To request assignment, please clearly outline your solution and timeline by commenting on the issue using the format below:

Describe your solution clearly:
Provide a detailed explanation of your proposed solution, including your approach, key implementation steps, and relevant examples or references. Mention any dependencies, assumptions, or risks you foresee that might affect your timeline or implementation.

Expected Timeline:

  • End date: [Expected submission date of a completed Pull Request]

Additional Context:
Include any other relevant context, links, screenshots, or resources that support your proposed solution.

🚨 Your assignment may be unassigned if there is no activity or progress within the stated timeline unless communicated clearly and agreed upon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions