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) O3-2252: Calculated values shouldn't be overwritten by the Encounter's values #318

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

CynthiaKamau
Copy link
Contributor

@CynthiaKamau CynthiaKamau commented Jun 11, 2024

… values

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work conforms to the OpenMRS 3.0 Styleguide and design documentation.
  • My work includes tests or is validated by existing tests.

Summary

This PR resolves the issue of null or undefined initial values for calculated fields when entering edit mode. Previously, the assumption was that all calculated fields would be retrieved through asynchronous functions from the expression runner. This assumption led to problems when synchronous functions were used, resulting in null or undefined initial values.

Screenshots

https://www.loom.com/share/866edea9d4c34b91ab4ef3ef93eba49c?sid=7ffa7c0d-58cd-42e2-a30f-64d4448462d7

Related Issue

Other

Copy link

github-actions bot commented Jun 11, 2024

Size Change: -245 kB (-19.5%) 🎉

Total Size: 1.01 MB

Filename Size Change
dist/800.js 0 B -245 kB (removed) 🏆
ℹ️ View Unchanged
Filename Size Change
dist/184.js 11.2 kB 0 B
dist/195.js 76.6 kB 0 B
dist/225.js 2.57 kB 0 B
dist/29.js 163 kB 0 B
dist/300.js 709 B 0 B
dist/327.js 1.84 kB 0 B
dist/335.js 967 B 0 B
dist/353.js 3.02 kB 0 B
dist/41.js 3.36 kB 0 B
dist/422.js 6.79 kB 0 B
dist/474.js 116 kB -2 B (0%)
dist/491.js 9.18 kB 0 B
dist/540.js 2.63 kB 0 B
dist/55.js 756 B 0 B
dist/635.js 14.3 kB 0 B
dist/70.js 482 B 0 B
dist/708.js 245 kB 0 B
dist/776.js 3.2 kB 0 B
dist/99.js 690 B 0 B
dist/993.js 3.09 kB 0 B
dist/main.js 343 kB -1 B (0%)
dist/openmrs-form-engine-lib.js 3.72 kB 0 B

compressed-size-action

Copy link
Member

@samuelmale samuelmale left a comment

Choose a reason for hiding this comment

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

Incase of a null calculated value, can we fallback using the encounter value?

src/hooks/useInitialValues.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.ts Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Show resolved Hide resolved
@brandones
Copy link
Contributor

This PR addresses the issue of null/undefined values on calculated fields in edit mode .
Calculated value is persisted when :
-There is a default value
-The encounter value is undefined
-The encounter value and calculated value are the same, if different

@CynthiaKamau Please strive for greater clarity in PR summaries. "This PR addresses the issue of null/undefined values on calculated fields in edit mode" what is the issue in question? It is not obvious what "null/undefined values on calculated fields in edit mode" means or why it would be a problem. And the phrase "The encounter value and calculated value are the same, if different" doesn't make sense.

Thanks for your work on this; I hope this is useful feedback.

@brandones
Copy link
Contributor

@CynthiaKamau Are the next steps clear to you? Any blockers?

@CynthiaKamau
Copy link
Contributor Author

@CynthiaKamau Are the next steps clear to you? Any blockers?

@CynthiaKamau Are the next steps clear to you? Any blockers?

I will update the description.Thanks for the feedback

@CynthiaKamau
Copy link
Contributor Author

@samuelmale do you still have any reservations on this ?

Copy link
Member

@samuelmale samuelmale left a comment

Choose a reason for hiding this comment

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

Nice work @CynthiaKamau, just a couple of requests:

  1. For cases where we have an encounter value but the value from evaluating the calculate expression is null/undefined, can we fallback to the encounter value?
  2. Can we have a test case scenario asserting the above?
  3. Can we have a test case scenario with an async calculate expressions?

src/hooks/useInitialValues.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Show resolved Hide resolved
Comment on lines +45 to +51
const encounterValue = formFieldHandlers[field.type]?.getInitialValue(
encounter,
field,
formFields,
encounterContext,
);
formFieldHandlers[field.type].handleFieldSubmission(field, encounterValue, encounterContext);
Copy link
Member

Choose a reason for hiding this comment

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

So we are only falling back to the encounter's value when we run into an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

},
id: 'latest_mother_hiv_status',
};
allFormFields.push(fieldWithCalculateExpression);
Copy link
Contributor

Choose a reason for hiding this comment

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

This mutates the object at the top level of this test. This will lead to behavior that varies depending on which tests are run, or the order in which they are run. Objects at the top level of a test file should be considered immutable. Use the spread syntax to define new form fields within specific tests.


it('should fall back to encounter value if the calculated expression result is null or undefined', async () => {
let hook = null;
let formFields: Array<FormField> = [
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks very similar to the other formFields variable. It would be easier to understand what the difference is if you defined this at the top level of the test file and then used it in each test file where it is needed, building new modified copies using the object spread syntax as necessary.

src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
src/hooks/useInitialValues.test.ts Outdated Show resolved Hide resolved
@brandones brandones changed the title O3-2252 Calculated values shouldn't be overwritten by the Encounter's… (fix) O3-2252: Calculated values shouldn't be overwritten by the Encounter's values Jul 3, 2024
@brandones brandones merged commit 109314c into main Jul 3, 2024
4 checks passed
@brandones brandones deleted the O3-2252 branch July 3, 2024 20:31
Art-Ndiema pushed a commit to Art-Ndiema/openmrs-form-engine-lib that referenced this pull request Aug 8, 2024
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