Skip to content

Commit

Permalink
Ensure that updates to the form context state are propagated to the f…
Browse files Browse the repository at this point in the history
…orm-factory provider.
  • Loading branch information
samuelmale committed Sep 25, 2024
1 parent 8e9ae10 commit 1b7a217
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const FormProcessorFactory = ({
<FormRenderer
processorContext={processorContext}
initialValues={initialValues}
isSubForm={isSubForm}
setIsLoadingFormDependencies={setIsLoadingFormDependencies}
/>
)}
Expand Down
19 changes: 10 additions & 9 deletions src/components/renderer/form/form-renderer.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ import { useFormStateHelpers } from '../../../hooks/useFormStateHelpers';
export type FormRendererProps = {
processorContext: FormProcessorContextProps;
initialValues: Record<string, any>;
isSubForm: boolean;
setIsLoadingFormDependencies: (isLoading: boolean) => void;
};

export const FormRenderer = ({ processorContext, initialValues, setIsLoadingFormDependencies }: FormRendererProps) => {
export const FormRenderer = ({
processorContext,
initialValues,
isSubForm,
setIsLoadingFormDependencies,
}: FormRendererProps) => {
const { evaluatedFields, evaluatedFormJson } = useEvaluateFormFieldExpressions(initialValues, processorContext);
const { registerForm, setIsFormDirty, workspaceLayout } = useFormFactory();
const methods = useForm({
Expand Down Expand Up @@ -64,8 +70,8 @@ export const FormRenderer = ({ processorContext, initialValues, setIsLoadingForm
}, [processorContext, workspaceLayout, methods, formFields, formJson, invalidFields]);

useEffect(() => {
registerForm(formJson.name, context);
}, [formJson.name, context]);
registerForm(formJson.name, isSubForm, context);
}, [formJson.name, isSubForm, context]);

useEffect(() => {
setIsFormDirty(isDirty);
Expand All @@ -91,12 +97,7 @@ export const FormRenderer = ({ processorContext, initialValues, setIsLoadingForm
/>
);
}
return (
<PageRenderer
key={page.label}
page={page}
/>
);
return <PageRenderer key={page.label} page={page} />;
})}
</FormProvider>
);
Expand Down
1 change: 0 additions & 1 deletion src/form-engine.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ interface FormEngineProps {
// TODOs:
// - Implement sidebar
// - Conditionally render the button set
// - Patient banner
const FormEngine = ({
formJson,
patientUUID,
Expand Down
10 changes: 5 additions & 5 deletions src/provider/form-factory-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface FormFactoryProviderContextProps {
visit: OpenmrsResource;
location: OpenmrsResource;
provider: OpenmrsResource;
registerForm: (formId: string, context: FormContextProps) => void;
registerForm: (formId: string, isSubForm: boolean, context: FormContextProps) => void;
setCurrentPage: (page: string) => void;
handleConfirmQuestionDeletion?: (question: Readonly<FormField>) => Promise<void>;
setIsFormDirty: (isFormDirty: boolean) => void;
Expand Down Expand Up @@ -80,11 +80,11 @@ export const FormFactoryProvider: React.FC<FormFactoryProviderProps> = ({

const abortController = new AbortController();

const registerForm = useCallback((formId: string, context: FormContextProps) => {
if (!rootForm.current) {
rootForm.current = context;
} else if (rootForm.current.formJson.name !== formId) {
const registerForm = useCallback((formId: string, isSubForm: boolean, context: FormContextProps) => {
if (isSubForm) {
subForms.current[formId] = context;
} else {
rootForm.current = context;
}
}, []);

Expand Down

0 comments on commit 1b7a217

Please sign in to comment.