-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: main
Are you sure you want to change the base?
Conversation
const onSubmitHandler: SubmitHandler<T> = async (data) => { | ||
await onSubmit(data); | ||
reset(); | ||
}; |
There was a problem hiding this comment.
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.
useEffect(() => { | ||
reset(defaultValues); | ||
}, [defaultValues, reset]); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does
const openDeleteFacility = (facility: Facility) => { | ||
setTargetFacility({ | ||
action: CRUDActions.Delete, | ||
facility: facility | ||
}); | ||
}; |
There was a problem hiding this comment.
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
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; | ||
}; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
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.