Skip to content

(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
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
8 changes: 5 additions & 3 deletions src/form-engine.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface FormEngineProps {
onCancel?: () => void;
handleClose?: () => void;
handleConfirmQuestionDeletion?: (question: Readonly<FormField>) => Promise<void>;
handleEmptyFormSubmission?: () => Promise<void>;
markFormAsDirty?: (isDirty: boolean) => void;
}

Expand All @@ -47,6 +48,7 @@ const FormEngine = ({
onCancel,
handleClose,
handleConfirmQuestionDeletion,
handleEmptyFormSubmission,
markFormAsDirty,
}: FormEngineProps) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -127,13 +129,13 @@ const FormEngine = ({
provider={session?.currentProvider}
visit={visit}
handleConfirmQuestionDeletion={handleConfirmQuestionDeletion}
handleEmptyFormSubmission={handleEmptyFormSubmission}
isFormExpanded={isFormExpanded}
formSubmissionProps={{
isSubmitting,
setIsSubmitting,
onSubmit,
onError: () => {},
handleClose: () => {},
onSubmit,
handleClose: handleClose,
}}
hideFormCollapseToggle={hideFormCollapseToggle}
setIsFormDirty={setIsFormDirty}>
Expand Down
13 changes: 13 additions & 0 deletions src/provider/form-factory-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ export function validateForm(context: FormContextProps) {
return errors.length === 0;
}

export function validateEmptyForm(context: FormContextProps) {
const { formFields, deletedFields } = context;
const allFormFields = [...formFields, ...deletedFields];
const transientFields = allFormFields
.filter((field) => field.questionOptions.isTransient)
.map((field) => field.id);
const { methods: { formState: { dirtyFields } } } = context;
const nonTransientDirtyFields = Object.keys(dirtyFields).filter(
(fieldName) => !transientFields.includes(fieldName)
);
return nonTransientDirtyFields.length === 0;
}

export async function processPostSubmissionActions(
postSubmissionHandlers: PostSubmissionActionMeta[],
submissionResults: OpenmrsResource[],
Expand Down
66 changes: 48 additions & 18 deletions src/provider/form-factory-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}

Expand All @@ -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();
Expand All @@ -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));

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;
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 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({
Expand All @@ -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({
Expand All @@ -142,11 +170,13 @@ export const FormFactoryProvider: React.FC<FormFactoryProviderProps> = ({
} else {
showToast(errorObject);
}
});
} else {
setIsSubmitting(false);
}
} else {
setIsSubmitting(false);
}
}
}
};
handleFormSubmission();
return () => {
abortController.abort();
};
Expand Down Expand Up @@ -181,4 +211,4 @@ export const useFormFactory = () => {
throw new Error('useFormFactoryContext must be used within a FormFactoryProvider');
}
return context;
};
};
Loading