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) O3-2760: Present workspaces should leverage the promptBeforeClosing function #1613

Merged
merged 46 commits into from
Feb 21, 2024

Conversation

vasharma05
Copy link
Member

@vasharma05 vasharma05 commented Jan 24, 2024

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 updates the existing workspaces implementation to use the promptBeforeClosing functions in the workspaces.

This PR makes the following changes in the codebase:

  1. Shifts all the workspaces registered as extensions from routes.jsonto registering them viaregisterWorkspace`.
  2. Added workspace title translations in the index.ts
  3. All the workspaces now use the promptBeforeClosing function.
    All the workspace forms (except the Lab order form) use React Hook Form, which returns a state isDirty, which tells whether the user made any change to the form or not. The promptBeforeClosing keeps an eye on the form state isDirty and decides whether to show the user a prompt when closing form without saving form changes.

CloseWorkspace

Workspace's closeWorkspace now takes an object of the following keys as an argument:

  1. ignoreChanges : Whether to just close the workspace
  2. onWorkspaceClose: Callback function to be called when the workspace is closed.

P.S. The onWorkspaceClose function can be used in the following manner:

❌ 
workspace.closeWorkspace();
clearGlobalStore()

✅ 
workspace.closeWorkspace({
	onWorkspaceClose: () => clearGlobalStore();
})

GetWhetherWorkspaceCanBeClosed

A new function getWhetherWorkspaceCanBeClosed is introduced which tells whether a workspace can be directly closed or the user will be shown a prompt before closing the function.

Screenshots

None

Related Issue

https://issues.openmrs.org/browse/O3-2760

Other

Also solves O3-2714.

@vasharma05 vasharma05 marked this pull request as draft January 24, 2024 06:16
Copy link
Member

@ibacher ibacher left a comment

Choose a reason for hiding this comment

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

Wouldn't it make sense if the closeWorkspace() function returned a Promise<void> rather than void? Then instead of needing all this logic to embed the onWorkspaceClose() callback, callers that need to do something after the workspace closes can just do:

closeWorkspace().then(/* do something */);

@usamaidrsk
Copy link
Member

@vasharma05 order is cleared upon prompting.

order-removed-even-before-user-discards.mp4

@vasharma05
Copy link
Member Author

Thank you @usamaidrsk , I'll fix it!

…rkspaces and keeping the closeWorkspace default behaviour intact
@brandones
Copy link
Contributor

@vasharma05 Please re-request review when ready. I've got my eye on this.

packages/esm-patient-chart-app/src/root.component.tsx Outdated Show resolved Hide resolved
* t('unsavedChangesInForms', 'There are unsaved changes in the following workspaces. Do you want to discard changes in the following workspaces? {{workspaceNames}}')
* t('closeWorkspaces', 'Discard changes in {{count}} workspaces', {count})

* t('unsavedChangesInOpenedWorkspace', 'You have unsaved changes in the opened workspace. Do you want to discard these changes?')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* t('unsavedChangesInOpenedWorkspace', 'You have unsaved changes in the opened workspace. Do you want to discard these changes?')
* t('unsavedChangesInOpenedWorkspace', 'You have unsaved changes in the currently opened workspace. Do you want to discard these changes?')

* t('closingAllWorkspacesPromptTitle','You have unsaved changes')
* t('closingAllWorkspacesPromptBody', 'There are unsaved changes in the following workspaces. Do you want to discard changes in the following workspaces? {{workspaceNames}}')
* t('closeAllOpenedWorkspaces', 'Discard changes in {{count}} workspaces', {count})
* t('unsavedChangesInWorkspace', 'There are unsaved changes in {{workspaceName}}. Please save them before opening another workspace.')
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I'm not sure that relying on workspace names for this particular translation is the most ideal approach because they're so arbitrary. That said, I don't have a better alternative.

Copy link
Member Author

Choose a reason for hiding this comment

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

Since we had this in the previous versions as well, I say we move ahead with this

"closeWorkspaces_other": "Discard changes in {{count}} workspaces",
"closeAllOpenedWorkspaces_one": "Discard changes in {{count}} workspaces",
"closeAllOpenedWorkspaces_other": "Discard changes in {{count}} workspaces",
"closingAllWorkspacesPromptBody": "There are unsaved changes in the following workspaces. Do you want to discard changes in the following workspaces? {{workspaceNames}}",
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't seem right to repeat the phrase 'following workspaces' twice. Also, wouldn't it be better to list out the open workspaces as list items?

Suggested change
"closingAllWorkspacesPromptBody": "There are unsaved changes in the following workspaces. Do you want to discard changes in the following workspaces? {{workspaceNames}}",
"closingAllWorkspacesPromptBody": "There are unsaved changes in several workspaces. Do you want to discard changes in the following workspaces? {{workspaceNames}}",

Co-authored-by: Dennis Kigen <[email protected]>
@vasharma05
Copy link
Member Author

I had a discussion with Ciaran today in the O3 UI/UX Design hours, and he said that the Discard button in the workspace will not directly close the workspace, but will show the prompt if there are changes in the form and the user can click the Discard Changes button in the prompt to close the form.

Hence I will be reverting on the discardChangesAndCloseWorkspace and replace it back with closeWorkspace and ignoreChanges = false.

So sorry everyone for hanging this PR so long. Thank you for bearing with me and I'll finalize the PR for final review.
Thanks a lot!

@brandones
Copy link
Contributor

Good call talking to Ciaran about it! Thanks for doing such a thorough job.

@denniskigen
Copy link
Member

Do let us know when you're ready so we can do a fresh pass of reviews, @vasharma05! This is very consequential work, thanks for taking it on!

@vasharma05
Copy link
Member Author

Hi @denniskigen!
The final changes are pushed and the PR is ready for final review.
CC: @brandones @ibacher

@@ -154,7 +166,7 @@ function AllergyForm({ closeWorkspace, patientUuid }: DefaultWorkspaceProps) {
(response: FetchResponse) => {
if (response.status === 201) {
mutate();
closeWorkspace();
closeWorkspace({ ignoreChanges: true });
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, I see. Now that ignoreChanges is false by default, we have to pass this argument on saving & exiting, which is a little awkward. Nevertheless I still think this is the right approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I guess the thing that is weird is actually that isDirty is still true after the form is submitted. Logically, it seems like it shouldn't be.

* @returns
*/

export function showWorkspacePrompts(
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really like this as a pattern; I think it's inappropriate unless the same prompts are being launched from many different places in the code. It is better to have the content of the prompt housed in the context that it is relevant to. But that's just sort of a note for posterity; let's get this PR in.

Copy link
Contributor

@brandones brandones left a comment

Choose a reason for hiding this comment

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

LGTM. I agree with @denniskigen 's suggestions about copy but this is a big PR that's been around for a while already. Requesting his forgiveness and that we fix the copy after merging this.

Thanks so much for all your work on this @vasharma05 !

@vasharma05 vasharma05 merged commit f9d9db4 into main Feb 21, 2024
5 of 6 checks passed
@vasharma05 vasharma05 deleted the feat/O3-2760 branch February 21, 2024 07:16
@jayasanka-sack
Copy link
Member

jayasanka-sack commented Feb 21, 2024

@vasharma05, the program enrollment E2E test has been failing since this PR. Can we sort it out pronto? New PRs are feeling the heat. Let's stick to the branch protection rules and avoid merging when checks are failing.

@vasharma05
Copy link
Member Author

Sure @jayasanka-sack !
I'll fix this first thing in the morning

@jayasanka-sack
Copy link
Member

Thanks, Vineet! ❤️

@denniskigen
Copy link
Member

denniskigen commented Mar 26, 2024

LGTM. I agree with @denniskigen 's suggestions about copy but this is a big PR that's been around for a while already. Requesting his forgiveness and that we fix the copy after merging this.

Thanks so much for all your work on this @vasharma05 !

Coming back to this like the Proverbial grammar Sith lord. Can we fix the copy @brandones @vasharma05?

@brandones
Copy link
Contributor

I'll do this after we move workspaces into core.

I have an idea: sometimes workspace titles are a little funny. But we can make workspace names whatever we want; there's no reason they need to be lowercased-and-hyphenated. Why don't we make workspace names exactly what should appear in notifications about that workspace? e.g. the allergy form, which has title "Record a new allergy" could have name "Allergy Form". So then when you try to close it you see "You have unsaved changes in the Allergy Form." What do you think of that, @denniskigen ?

@ibacher
Copy link
Member

ibacher commented Mar 29, 2024

I think the only issue with that is it needs to be translatable, since, presumably, we don’t want to need a separate allergy workspace per language, but I like the idea.

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.

6 participants