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

fix: prevent posting invalid values #1069

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

HendrikThePendric
Copy link
Contributor

@HendrikThePendric HendrikThePendric commented Oct 5, 2022

Before this PR this app would unconditionally POST the new field value. Now this won't happen, which is obviously a very good thing. It seems that this improvement does come at a very small cost, which I attempt to explain in the code.

@dhis2-bot
Copy link
Contributor

🚀 Deployed on https://pr-1069--dhis2-settings.netlify.app

Comment on lines +170 to +176
} else {
// Update client-side state to invalid value
// to prevent the error UI from persisting when
// changing an invalid value back to the original value
settingsStore.state[key] = value
settingsStore.setState(settingsStore.state)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found a bug after adding the first if clause.

  1. When the user changes the initial form value to an invalid value
  2. And then blurs the field
  3. The field will get and error state and show an error message (CORRECT)
  4. When the user focusses the field again
  5. And then changes the value back to the original value
  6. And then blurs the field
  7. The error message would persist

This was because the form state was not updated to the invalid value, so the state value was still the initial value. And as a result, changing the input's value back to initial state value was not triggering the onUpdateField callback.

Setting the state to the invalid value in the else clause makes sense, because the state should reflect the form-values whether valid or not. However, this does come at a price. The following steps will now produce a POST request, while strictly speaking they shouldn't:

  1. User changes initial value to an invalid value and blurs
  2. User changes invalid value to initial value and blurs
  3. The value is now different, so onUpdateField is triggered. It is also valid, so the value is POSTed too.

Given the constraints of the tools being used here, I think this is probably as good as it will get.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably good to point out that strictly speaking this is a "known issue", not a "regression". Since previously invalid values were being posted, resetting to an initial value would also trigger a post.

Copy link
Contributor

Choose a reason for hiding this comment

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

Given the constraints of the tools being used here, I think this is probably as good as it will get.

agreed

@HendrikThePendric HendrikThePendric requested a review from a team October 5, 2022 09:08
Comment on lines +170 to +176
} else {
// Update client-side state to invalid value
// to prevent the error UI from persisting when
// changing an invalid value back to the original value
settingsStore.state[key] = value
settingsStore.setState(settingsStore.state)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Given the constraints of the tools being used here, I think this is probably as good as it will get.

agreed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants