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

(feat) Automatically revalidate order data after form submission #796

Merged
merged 4 commits into from
Sep 6, 2022

Conversation

denniskigen
Copy link
Member

@denniskigen denniskigen commented Sep 2, 2022

Requirements

  • This PR has a title that briefly describes the work done, including the ticket number if there is a ticket.
  • My work conforms to the OpenMRS 3.0 Styleguide.
  • My work includes tests or is validated by existing tests.

Summary

This PR is a follow-up to #774. It adds a call to SWR's mutate function to the orders form submission handler so that when the form gets submitted successfully, a revalidation request gets broadcasted to SWR hooks using the same key. This means the user does not need to reload the patient chart to see newly created orders (as @samuelmale showed in the video demo in #774).

This PR also wraps an empty state Tile in the OrderBasketItemList component with the Layer component. This harmonizes its look and feel with that of the other empty states in the app.

This PR is a follow-up to #774. It adds a call to SWR's `mutate` function to the orders form submission handler so that when the form gets submitted successfully, a revalidation request gets broadcasted to SWR hooks using the same key. This means that the user does not need to reload the patient chart to see newly created orders.

Additionally, this PR also wraps an empty state `Tile` in the OrderBasketItemList component with the `Layer` component. This brings its look and feel in line with the rest of the app.
Copy link
Contributor

@ZacButko ZacButko left a comment

Choose a reason for hiding this comment

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

This looks it will be useful. I'm curious if you can think of a way to add a test to cover this functionality?

@denniskigen
Copy link
Member Author

This looks like it will be useful. I'm curious if you can think of a way to add a test to cover this functionality.

We use this pattern in multiple places, and those usages are backed up with tests that assert that a success notification gets rendered when the form gets submitted successfully. Adding a test for this shouldn't be difficult, though I'd prefer to do it in a separate PR.

@ZacButko
Copy link
Contributor

ZacButko commented Sep 2, 2022

It's good that tests check for the notification popup on endpoint success. I was specifically wondering about the mutate affecting a separate piece of the UI which are using the same endpoint

@denniskigen
Copy link
Member Author

denniskigen commented Sep 2, 2022

That's a valid concern. There's a pattern we could leverage to guard against the wrong keys being inadvertently revalidated. This logic would go through the SWR cache and make sure to only revalidate keys that match the provided RegExp.

We could use the following RegExp to match the exact key that updates the medications widgets:

const apiUrlPattern = new RegExp(
  '\\/ws\\/rest\\/v1\\/order\\?patient' + patientUuid + '\\&careSetting=' + config.careSettingUuid,
)

// Find matching keys from SWR's cache and broadcast a revalidation message to their pre-bound SWR hooks
Array.from(cache.keys())
  .filter((url: string) => apiUrlPattern.test(url))
  .forEach((url: string) => mutate(url));

Amending the PR to adopt this would be straightforward.

@samuelmale
Copy link
Member

@denniskigen I think adding the regex based filter is our based option here.

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 @denniskigen

@denniskigen denniskigen merged commit e1371a5 into main Sep 6, 2022
@denniskigen denniskigen deleted the feat-revalidate-post-submit branch September 6, 2022 07:33
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