-
Notifications
You must be signed in to change notification settings - Fork 91
(feat) O3-2971: Show a confirmation modal when the user submits an empty form #464
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
Open
UNCANNY69
wants to merge
12
commits into
openmrs:main
Choose a base branch
from
UNCANNY69:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f2255ea
Adding conformation modal for empty form
UNCANNY69 cf4b9e5
Adding Empty Form conformation modal
UNCANNY69 0290341
Refactored the modal
UNCANNY69 8e6a800
Making requested changes
UNCANNY69 5305c73
Merge branch 'openmrs:main' into main
UNCANNY69 bbb4d15
Changes according to the comments
UNCANNY69 1947009
Modification to the vvalidate function
UNCANNY69 823ff91
Merge branch 'openmrs:main' into main
UNCANNY69 676861e
Adding checks for trasient fields
UNCANNY69 a1752c2
Adding checks for trasient fields
UNCANNY69 0c421d7
Merge branch 'main' into main
UNCANNY69 781a8b7
Merge branch 'main' into main
denniskigen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import { | |
} from '@openmrs/esm-framework'; | ||
import { type FormProcessorConstructor } from '../processors/form-processor'; | ||
import { type FormContextProps } from './form-provider'; | ||
import { processPostSubmissionActions, validateForm } from './form-factory-helper'; | ||
import { processPostSubmissionActions, validateForm, validateEmptyForm } from './form-factory-helper'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { usePostSubmissionActions } from '../hooks/usePostSubmissionActions'; | ||
|
||
|
@@ -47,11 +47,11 @@ interface FormFactoryProviderProps { | |
isSubmitting: boolean; | ||
setIsSubmitting: (isSubmitting: boolean) => void; | ||
onSubmit: (data: any) => void; | ||
onError: (error: any) => void; | ||
handleClose: () => void; | ||
}; | ||
hideFormCollapseToggle: () => void; | ||
handleConfirmQuestionDeletion?: (question: Readonly<FormField>) => Promise<void>; | ||
handleEmptyFormSubmission?: () => Promise<void>; | ||
setIsFormDirty: (isFormDirty: boolean) => void; | ||
} | ||
|
||
|
@@ -70,14 +70,15 @@ export const FormFactoryProvider: React.FC<FormFactoryProviderProps> = ({ | |
children, | ||
formSubmissionProps, | ||
hideFormCollapseToggle, | ||
handleEmptyFormSubmission, | ||
handleConfirmQuestionDeletion, | ||
setIsFormDirty, | ||
}) => { | ||
const { t } = useTranslation(); | ||
const rootForm = useRef<FormContextProps>(); | ||
const subForms = useRef<Record<string, FormContextProps>>({}); | ||
const layoutType = useLayoutType(); | ||
const { isSubmitting, setIsSubmitting, onSubmit, onError, handleClose } = formSubmissionProps; | ||
const { isSubmitting, setIsSubmitting, onSubmit, handleClose } = formSubmissionProps; | ||
const postSubmissionHandlers = usePostSubmissionActions(formJson.postSubmissionActions); | ||
|
||
const abortController = new AbortController(); | ||
|
@@ -96,14 +97,42 @@ export const FormFactoryProvider: React.FC<FormFactoryProviderProps> = ({ | |
}); | ||
|
||
useEffect(() => { | ||
if (isSubmitting) { | ||
// TODO: find a dynamic way of managing the form processing order | ||
const forms = [rootForm.current, ...Object.values(subForms.current)]; | ||
// validate all forms | ||
const isValid = forms.every((formContext) => validateForm(formContext)); | ||
if (isValid) { | ||
Promise.all(forms.map((formContext) => formContext.processor.processSubmission(formContext, abortController))) | ||
.then(async (results) => { | ||
const handleFormSubmission = async () => { | ||
if (isSubmitting) { | ||
const forms = [rootForm.current, ...Object.values(subForms.current)]; | ||
// Validate all forms | ||
const isValid = forms.every((formContext) => validateForm(formContext)); | ||
UNCANNY69 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (isValid) { | ||
// Check if the form is empty | ||
const isEmpty = forms.every((formContext) => validateEmptyForm(formContext)); | ||
if (isEmpty) { | ||
if (handleEmptyFormSubmission && typeof handleEmptyFormSubmission === 'function') { | ||
const result = handleEmptyFormSubmission(); | ||
if (result instanceof Promise) { | ||
try { | ||
await result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we try inferring the resolved promise to a boolean? |
||
handleClose(); | ||
} catch { | ||
// Rejected (cancelled) -> Do nothing | ||
} | ||
} | ||
else if (typeof result === 'boolean') { | ||
if (result) { // If `true`, close the form | ||
handleClose(); | ||
} | ||
} | ||
else { | ||
handleClose(); | ||
} | ||
return setIsSubmitting(false); | ||
} | ||
} | ||
|
||
try { | ||
const results = await Promise.all( | ||
forms.map((formContext) => formContext.processor.processSubmission(formContext, abortController)) | ||
); | ||
formSubmissionProps.setIsSubmitting(false); | ||
if (sessionMode === 'edit') { | ||
showSnackbar({ | ||
|
@@ -129,8 +158,7 @@ export const FormFactoryProvider: React.FC<FormFactoryProviderProps> = ({ | |
} else { | ||
handleClose(); | ||
} | ||
}) | ||
.catch((errorObject: Error | ToastDescriptor) => { | ||
} catch (errorObject) { | ||
setIsSubmitting(false); | ||
if (errorObject instanceof Error) { | ||
showToast({ | ||
|
@@ -142,11 +170,13 @@ export const FormFactoryProvider: React.FC<FormFactoryProviderProps> = ({ | |
} else { | ||
showToast(errorObject); | ||
} | ||
}); | ||
} else { | ||
setIsSubmitting(false); | ||
} | ||
} else { | ||
setIsSubmitting(false); | ||
} | ||
} | ||
} | ||
}; | ||
handleFormSubmission(); | ||
return () => { | ||
abortController.abort(); | ||
}; | ||
|
@@ -181,4 +211,4 @@ export const useFormFactory = () => { | |
throw new Error('useFormFactoryContext must be used within a FormFactoryProvider'); | ||
} | ||
return context; | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.