Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for preventing submit button when form content is not changed #8918

Open
kihan2518B opened this issue Oct 24, 2024 · 7 comments · May be fixed by #8956
Open

Support for preventing submit button when form content is not changed #8918

kihan2518B opened this issue Oct 24, 2024 · 7 comments · May be fixed by #8956

Comments

@kihan2518B
Copy link

Describe the bug
When attempting to update bed or staff details in the facilities section, clicking the "Update" button sends a request to the backend even if no changes have been made to the form. This results in unnecessary API calls, potentially leading to performance issues or confusion in tracking actual updates.

To Reproduce
Steps to reproduce the behavior:

  1. Go to the facilities section.
  2. Select a bed or staff detail to edit.
  3. Without making any changes, click on the "Update" button.
  4. See a network request to update the details

Expected behavior
No request should be sent if no fields are updated, and the "Update" button should remain inactive.

Screenshots
Image

Desktop (please complete the following information):

  • OS: Windows
  • Browser Microsoft Edge
@kihan2518B
Copy link
Author

I have got a solution for that
solution:
Initial State Tracking:

Capture the initial values of the form fields when the form is first loaded or reset.
On-Change Comparison:

Each time a field is changed, compare the current value with the corresponding initial value for that specific field.
Field-by-Field Tracking:

Maintain a flag (like hasFormChanged) that tracks if any field has been modified compared to its initial value.
Reset Logic:

If a field is changed back to its initial value, reset its modified state, ensuring hasFormChanged becomes false if all fields return to their original values.
Efficient Comparison:

Compare individual fields rather than the entire form object (avoiding JSON.stringify), resulting in better performance and accuracy.
Edge Case Handling:

Ensure that if a field is changed but reverted back to the original value, it doesn't falsely indicate that the form has changed.

Can anyone assign me this issue?

@rithviknishad
Copy link
Member

rithviknishad commented Oct 24, 2024

Let's have this feature in the Form component itself instead.

This way all usages of the Form component would get this feature enabled in one go and not just the form specified in the issue.

We could upgrade the Form component to have a isDirty state. And in the onChange we could set isDirty to true. And the submit button managed by the Form component could be disabled based on this isDirty state.

@kihan2518B
Copy link
Author

yaa sure that would also work.
@rithviknishad can you assign this issue to me

@kihan2518B
Copy link
Author

I had made necessary changes in the form component.
but this is applying to only patient update form as we have use Form only in PatientRegister component.

Image

For other components like BedCapacity,StaffCapacity, etc we are not using Form component.

So for that I had a solution by manually adding isDirty state to all component.

solution:
Image

@rithviknishad
Copy link
Member

let's switch those to use the Form component instead

@kihan2518B kihan2518B linked a pull request Oct 28, 2024 that will close this issue
6 tasks
@rithviknishad rithviknishad changed the title Unnecessary Update Request Triggered Without Changes in Facilities Support for preventing submit button when form content is not changed Nov 2, 2024
@rithviknishad
Copy link
Member

@kihan2518B can we also do the same for what's mentioned in #8982?

@kihan2518B
Copy link
Author

Yaa sure I will make those component use the form component so that the issues are solved correspondingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Review required
Development

Successfully merging a pull request may close this issue.

3 participants