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

(refactor) O3-4199: Bed management should use the modal system #1381

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

amosmachora
Copy link
Contributor

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 refactors the current modal functionality in the bed management app to use the openmrs modal system as described here. When I was working on this PR I was careful only to refactor the modals, separate edit-add functionalities (beds, bed tags bed types), and not change any underlying implementation. Any change made can be considered absolutely necessary.

I also improved the UX when submitting the modals by adding loading states on the button and showing a spinner. Nothing major.

Screenshots

Short video verifying that nothing breaks after this change

bed-modals-refactor.mp4

Related Issue

https://openmrs.atlassian.net/browse/O3-4199

Other

@denniskigen denniskigen requested a review from ibacher December 4, 2024 12:13
@ibacher ibacher changed the title (refactor) : bed management modal system (refactor) bed management modal system Dec 4, 2024
@ibacher ibacher changed the title (refactor) bed management modal system (refactor) O3-4199: Bed management should use the modal system Dec 4, 2024
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.

A couple of quick thoughts. While its a good idea to use the new modal system, I don't like the idea of having to maintain 6 forms instead of the previous 3.

message: string;
}

const EditBedTagForm: React.FC<EditBedTagFormProps> = ({ editData, mutate, closeModal }) => {
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite understand why we've broken things out this way instead of keeping a single, shared form used for both adding and editing a bed tag. This is way more work to maintain.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, we should maintain a single for adding/editing a tag.

Comment on lines 104 to 105
const [showErrorNotification, setShowErrorNotification] = useState(false);
const [formStateError, setFormStateError] = useState('');
Copy link
Member

Choose a reason for hiding this comment

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

It's weird to have these consts declared what is now mid-way down the file.

},
});

const onSubmit = (formData: BedTagData) => {
Copy link
Member

Choose a reason for hiding this comment

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

Why is handleUpdateBedTag wrapped in useCallback() but this isn't?


const onSubmit = (formData: BedTagData) => {
const result = BedTagAdministrationSchema.safeParse(formData);
if (result.success) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't something happen if the result is not a success?

});

const onSubmit = (formData: BedTagData) => {
const result = BedTagAdministrationSchema.safeParse(formData);
Copy link
Member

Choose a reason for hiding this comment

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

Also, why are we parsing the form data here? What's the goal of this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original code was written by @brandones in my understanding so I feel he is at a better position to answer this.

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 think I wrote this; if I appear in the blame I probably moved the code in from somewhere else. Anyway it looks like this is outdated, so I will assume the question is resolved.

Copy link
Contributor

Choose a reason for hiding this comment

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

@amosmachora please take note

};

const onError = (error: { [key: string]: ErrorType }) => {
setFormStateError(Object.entries(error)[0][1].message);
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 only support a single error? That seems wrong...

In principle, each field should display an error message related to that field and we only need a bigger message if there's an error... bigger than a field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ibacher the aim of this PR was to just separate out the forms. As I said in the PR description I consciously made the decision to leave everything as is to make it easier for review. If more changes like this are required I am more than happy to work on them but that might bloat the PR and make it more challenging to review.

Also as I was working on this I made the assumption that since I was working on top of already merged changes there is not much need to concentrate on the details.

Copy link
Member

Choose a reason for hiding this comment

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

Because of the way these changes are done, it's very hard for me to tell what's "new" in this PR and what's just "moved" from somewhere else.

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 see. Think of it this way. the new tag form and the edit tag form both used an abstracted tag component. what I did was duplicate the tag component in both forms. So the new tag form know owns its own functionality that was previously in the tag component same as the edit one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so nothing is new in short

render={({ field, fieldState }) => (
<>
<TextInput
id="bedTag"
Copy link
Member

Choose a reason for hiding this comment

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

Is id required here?

@amosmachora
Copy link
Contributor Author

A couple of quick thoughts. While its a good idea to use the new modal system, I don't like the idea of having to maintain 6 forms instead of the previous 3.

Hello @denniskigen is at a better position to answer this question. Also the new 'architecture' is a personal preference of mine too as it allows for separation of concerns. I think it looks like more work from the onset but it simplifies work later IMO.

@ibacher
Copy link
Member

ibacher commented Dec 5, 2024

I think it looks like more work from the onset but it simplifies work later IMO.

See, I see this in exactly the opposite way. Previously, we had one form to add or edit each of the three things (bed tags, etc.). Now, we have a separate form for each. That means if we decide that we need to, I don't know, collect a date the change should be effective on, we now need to add that field to both the add form and the edit form. With the previous setup, we just needed to modify the code in a single place.

While, in general, I'm in favour of breaking things up a bit, breaking things up in this way makes on-going maintenance harder, precisely because you need to know that there are two files that need to be updated for each change.

The objection here isn't necessarily to having a separate add-tag-form modal and edit-tag-form modal, but these should share a single component that actually renders the form.

@amosmachora
Copy link
Contributor Author

I think it looks like more work from the onset but it simplifies work later IMO.

See, I see this in exactly the opposite way. Previously, we had one form to add or edit each of the three things (bed tags, etc.). Now, we have a separate form for each. That means if we decide that we need to, I don't know, collect a date the change should be effective on, we now need to add that field to both the add form and the edit form. With the previous setup, we just needed to modify the code in a single place.

While, in general, I'm in favour of breaking things up a bit, breaking things up in this way makes on-going maintenance harder, precisely because you need to know that there are two files that need to be updated for each change.

The objection here isn't necessarily to having a separate add-tag-form modal and edit-tag-form modal, but these should share a single component that actually renders the form.

Great points @ibacher this absolutely makes sense if you explain it that way. I guess its just a personal preference of mine and the ticket did leave room for some creativity. What do you suggest we do in that case then? should we just refactor the modals to use the modal system and leave everything as is?

@amosmachora
Copy link
Contributor Author

I think it looks like more work from the onset but it simplifies work later IMO.

See, I see this in exactly the opposite way. Previously, we had one form to add or edit each of the three things (bed tags, etc.). Now, we have a separate form for each. That means if we decide that we need to, I don't know, collect a date the change should be effective on, we now need to add that field to both the add form and the edit form. With the previous setup, we just needed to modify the code in a single place.

While, in general, I'm in favour of breaking things up a bit, breaking things up in this way makes on-going maintenance harder, precisely because you need to know that there are two files that need to be updated for each change.

The objection here isn't necessarily to having a separate add-tag-form modal and edit-tag-form modal, but these should share a single component that actually renders the form.

While we are on the topic, just for fun, how about when you only need to refactor one functionality, say, the edit functionality? Isn`t there a chance that you might end up breaking two features instead of one or making an unnecessary change in the other while you only aimed at changing one thing?

@ojwanganto
Copy link
Contributor

I think it looks like more work from the onset but it simplifies work later IMO.

See, I see this in exactly the opposite way. Previously, we had one form to add or edit each of the three things (bed tags, etc.). Now, we have a separate form for each. That means if we decide that we need to, I don't know, collect a date the change should be effective on, we now need to add that field to both the add form and the edit form. With the previous setup, we just needed to modify the code in a single place.
While, in general, I'm in favour of breaking things up a bit, breaking things up in this way makes on-going maintenance harder, precisely because you need to know that there are two files that need to be updated for each change.
The objection here isn't necessarily to having a separate add-tag-form modal and edit-tag-form modal, but these should share a single component that actually renders the form.

Great points @ibacher this absolutely makes sense if you explain it that way. I guess its just a personal preference of mine and the ticket did leave room for some creativity. What do you suggest we do in that case then? should we just refactor the modals to use the modal system and leave everything as is?

In addition to @ibacher's response, it is also a case of convention and standards which also guide code reviews. If you look at many forms in the system, the convention is to use a single form for add/edit and unit tests ensure the forms work as expected. I suggest that you only refactor the original code to use the modals. That will also align with the PR title for ease of reviews

@amosmachora
Copy link
Contributor Author

I think it looks like more work from the onset but it simplifies work later IMO.

See, I see this in exactly the opposite way. Previously, we had one form to add or edit each of the three things (bed tags, etc.). Now, we have a separate form for each. That means if we decide that we need to, I don't know, collect a date the change should be effective on, we now need to add that field to both the add form and the edit form. With the previous setup, we just needed to modify the code in a single place.
While, in general, I'm in favour of breaking things up a bit, breaking things up in this way makes on-going maintenance harder, precisely because you need to know that there are two files that need to be updated for each change.
The objection here isn't necessarily to having a separate add-tag-form modal and edit-tag-form modal, but these should share a single component that actually renders the form.

Great points @ibacher this absolutely makes sense if you explain it that way. I guess its just a personal preference of mine and the ticket did leave room for some creativity. What do you suggest we do in that case then? should we just refactor the modals to use the modal system and leave everything as is?

In addition to @ibacher's response, it is also a case of convention and standards which also guide code reviews. If you look at many forms in the system, the convention is to use a single form for add/edit and unit tests ensure the forms work as expected. I suggest that you only refactor the original code to use the modals. That will also align with the PR title for ease of reviews

Perfect. Makes sense if that is the ongoing standard then there is probably no reason for this one to be different.

@amosmachora amosmachora force-pushed the refactor/bed-management-modal-system branch from 09545c7 to eea4e30 Compare December 9, 2024 06:46
@amosmachora amosmachora requested a review from ibacher December 9, 2024 06:51
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.

4 participants