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: refactor modals #727

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft

feat: refactor modals #727

wants to merge 15 commits into from

Conversation

calisio
Copy link
Contributor

@calisio calisio commented Feb 5, 2025

Description of the change

Refactors modal into a new modal component. This will standardize the way that we implement form modals by passing an array of inputs, which will then create the form automatically. This will eliminate the need to create a new file included in the /forms folder.

NOTE: will be moving the interfaces/enums to common.ts, but just for simplicity now i am keeping them in the new modal. I also will be renaming at the very end once all is transferred to new modal.

Related issues: closes #652

Additional context

Still a work in progress. Leaving as a draft for people to look at it first before implementing across most pages that include modals.

@jtucholski jtucholski self-requested a review February 6, 2025 01:49
Comment on lines 53 to 56
const onSubmitHandler: SubmitHandler<T> = async (data) => {
await onSubmit(data);
reset();
};
Copy link
Contributor

Choose a reason for hiding this comment

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

This is good, having it automatically clear the form on submit.

Comment on lines 49 to 51
useEffect(() => {
reset(defaultValues);
}, [defaultValues, reset]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this handle the case where the user presses Esc and the form resets back to it's default values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does

Comment on lines 64 to 69
const openDeleteFacility = (facility: Facility) => {
setTargetFacility({
action: CRUDActions.Delete,
facility: facility
});
};
Copy link
Contributor

Choose a reason for hiding this comment

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

It feels like these should have the word handle in the name. My initial reaction was that it's doing the same as what is in the useEffect

Comment on lines 84 to 145
const facilityInputs: Input[] = [
{
type: FormInputTypes.Text,
label: 'Name',
interfaceRef: 'name',
required: true
},
{
type: FormInputTypes.Dropdown,
label: 'Timezone',
interfaceRef: 'timezone',
required: true,
enumType: Timezones
}
revalidate();
editFacilityModal.current?.close();
addFacilityModal.current?.close();
resetModal();
}
];

const handleDeleteFacilityCancel = () => {
deleteFacilityModal.current?.close();
resetModal();
const addFacility: SubmitHandler<FieldValues> = async (data) => {
const response = await API.post('facilities', data);
if (!response.success) {
toaster('Failed to add facility', ToastState.error);
return;
}
toaster('Facility created successfully', ToastState.success);
void mutate();
addFacilityModal.current?.close();
setTargetFacility(null);
};
const openDeleteFacility = (facility: Facility) => {
deleteFacilityModal.current?.showModal();
setTargetFacility(facility);

const updateFacility: SubmitHandler<FieldValues> = async (data) => {
const response = await API.patch(
`facilities/${targetFacility?.facility.id}`,
data
);
if (!response.success) {
toaster('Failed to update facility', ToastState.error);
return;
}
toaster('Facility updated successfully', ToastState.success);
void mutate();
editFacilityModal.current?.close();
setTargetFacility(null);
};
const handleDeleteFacility = async () => {
if (targetFacility?.id == 1) {

const deleteFacility = async () => {
if (targetFacility?.facility.id == 1) {
toaster('Cannot delete default facility', ToastState.error);
deleteFacilityModal.current?.close();
return;
}

await API.delete('facilities/' + targetFacility?.id)
.then((response) => {
if (response.success) {
toaster(
'Facility successfully deleted',
ToastState.success
);
revalidate();
} else {
toaster('Error deleting facility', ToastState.success);
}
})
.catch(() => {
toaster('Error deleting facility', ToastState.error);
});

const response = await API.delete(
'facilities/' + targetFacility?.facility.id
);
if (response.success) {
toaster('Facility successfully deleted', ToastState.success);
void mutate();
} else {
toaster('Error deleting facility', ToastState.error);
}
deleteFacilityModal.current?.close();
resetModal();
void mutate();
setTargetFacility(null);
return;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about something like putting the form inputs and the handlers into their own file so that we have a reusable form module for managing facilities?

It cleans up the FacilityManagement page a bit more and adds an element of reusability (not that we have a use case for it yet).

I could be overthinking it and I am open to you pushing back on this.

Copy link
Contributor

@jtucholski jtucholski left a comment

Choose a reason for hiding this comment

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

Looks good. I like the direction this is going and added a few comments for consideration.

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

Successfully merging this pull request may close these issues.

Refactor Modal Component and Form Handling
2 participants