Skip to content

Commit

Permalink
fix(orchestrator): multiple bug fixes related to execution form
Browse files Browse the repository at this point in the history
  • Loading branch information
batzionb committed Nov 27, 2024
1 parent 8ac72a1 commit 7e9db47
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
33 changes: 20 additions & 13 deletions plugins/orchestrator-form-react/src/components/OrchestratorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ const getNumSteps = (schema: JSONSchema7): number | undefined => {

const SingleStepForm = ({
schema,
formData,
onChange,
initialFormData,
onSubmit,
uiSchema,
}: {
schema: JSONSchema7;
formData: JsonObject;
onChange: (formData: JsonObject) => void;
initialFormData?: JsonObject;
onSubmit: (formData: JsonObject) => void;
uiSchema: UiSchema<JsonObject>;
}) => {
const [_initialFormData, setInitialFormData] = React.useState<JsonObject | undefined>(initialFormData)
const _onSubmit = (formData: JsonObject) => {
// Since the review step is outside of the MuiForm component in SingleStepForm, we need to load the current values when navigating back.
setInitialFormData(formData);
onSubmit(formData);
}

const steps = React.useMemo<OrchestratorFormStep[]>(() => {
return [
{
Expand All @@ -41,16 +48,16 @@ const SingleStepForm = ({
content: (
<OrchestratorFormWrapper
schema={{ ...schema, title: '' }}
formData={formData}
onChange={onChange}
initialFormData={_initialFormData}
onSubmit={_onSubmit}
uiSchema={uiSchema}
>
<OrchestratorFormToolbar />
</OrchestratorFormWrapper>
),
},
];
}, [schema, formData, onChange, uiSchema]);
}, [schema, _initialFormData, onSubmit, uiSchema]);
return <OrchestratorFormStepper steps={steps} />;
};

Expand All @@ -59,7 +66,7 @@ type OrchestratorFormProps = {
isExecuting: boolean;
handleExecute: (parameters: JsonObject) => Promise<void>;
data?: JsonObject;
isDataReadonly?: boolean;
isDataReadonly?: boolean;
};

const OrchestratorForm = ({
Expand All @@ -80,7 +87,7 @@ const OrchestratorForm = ({
handleExecute(formData || {});
}, [formData, handleExecute]);

const onChange = React.useCallback(
const onSubmit = React.useCallback(
(_formData: JsonObject) => {
setFormData(_formData);
},
Expand Down Expand Up @@ -113,17 +120,17 @@ const OrchestratorForm = ({
<OrchestratorFormWrapper
schema={schema}
numStepsInMultiStepSchema={numStepsInMultiStepSchema}
formData={formData}
onChange={onChange}
onSubmit={onSubmit}
uiSchema={uiSchema}
initialFormData={data}
>
<Fragment />
</OrchestratorFormWrapper> // it is required to pass the fragment so rjsf won't generate a Submit button
) : (
<SingleStepForm
schema={schema}
onChange={onChange}
formData={formData}
onSubmit={onSubmit}
initialFormData={data}
uiSchema={uiSchema}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const MuiForm = withTheme<JsonObject, JSONSchema7>(MuiTheme);
type OrchestratorFormWrapperProps = {
schema: JSONSchema7;
numStepsInMultiStepSchema?: number;
children: React.ReactNode;
formData: JsonObject;
onChange: (formData: JsonObject) => void;
children: React.ReactNode;
onSubmit: (formData: JsonObject) => void;
uiSchema: UiSchema<JsonObject, JSONSchema7>;
initialFormData?: JsonObject;
};

const WrapperFormPropsContext =
Expand All @@ -49,13 +49,15 @@ const FormComponent = (decoratorProps: FormDecoratorProps) => {
numStepsInMultiStepSchema,
uiSchema,
schema,
onChange,
formData,
onSubmit: _onSubmit,
initialFormData,
children,
} = props;
const [extraErrors, setExtraErrors] = React.useState<
ErrorSchema<JsonObject> | undefined
>();
// make this form a controlled component so state will remain when moving between steps. see https://rjsf-team.github.io/react-jsonschema-form/docs/quickstart#controlled-component
const [formData, setFormData] = React.useState<JsonObject>(initialFormData || {});
const isMultiStep = numStepsInMultiStepSchema !== undefined;
const { handleNext, activeStep, handleValidateStarted, handleValidateEnded } =
useStepperContext();
Expand Down Expand Up @@ -96,8 +98,10 @@ const FormComponent = (decoratorProps: FormDecoratorProps) => {
!_validationError &&
activeStep < (numStepsInMultiStepSchema || 1)
) {
_onSubmit(_formData);
handleNext();
}

}
};

return (
Expand All @@ -109,7 +113,7 @@ const FormComponent = (decoratorProps: FormDecoratorProps) => {
)}
<Grid item>
<MuiForm
{...omit(decoratorProps, 'extra')}
{...omit(decoratorProps, 'getExtraErrors')}
fields={isMultiStep ? { ObjectField: StepperObjectField } : {}}
uiSchema={uiSchema}
validator={validator}
Expand All @@ -119,7 +123,7 @@ const FormComponent = (decoratorProps: FormDecoratorProps) => {
extraErrors={extraErrors}
onSubmit={e => onSubmit(e.formData || {})}
onChange={e => {
onChange(e.formData || {});
setFormData(e.formData || {});
if (decoratorProps.onChange) {
decoratorProps.onChange(e);
}
Expand All @@ -135,17 +139,18 @@ const FormComponent = (decoratorProps: FormDecoratorProps) => {
const OrchestratorFormWrapper = ({
schema,
uiSchema,
formData,
initialFormData,
...props
}: OrchestratorFormWrapperProps) => {
const formApi =
useApiHolder().get(orchestratorFormApiRef) || defaultFormExtensionsApi;
const NewComponent = React.useMemo(() => {
const formDecorator = formApi.getFormDecorator(schema, uiSchema, formData);
const formDecorator = formApi.getFormDecorator(schema, uiSchema, initialFormData);
return formDecorator(FormComponent);
}, [schema, formApi, uiSchema, formData]);
}, [schema, uiSchema, formApi, initialFormData]);
console.log({initialFormData})
return (
<WrapperFormPropsContext.Provider value={{ schema, uiSchema, formData, ...props }}>
<WrapperFormPropsContext.Provider value={{ schema, uiSchema, initialFormData, ...props }}>
<NewComponent />
</WrapperFormPropsContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const ExecuteWorkflowPage = () => {
const response = await orchestratorApi.executeWorkflow({
workflowId,
parameters,
businessKey: effectiveInstanceId,
businessKey: assessmentInstanceId,
});
navigate(instanceLink({ instanceId: response.data.id }));
} catch (err) {
Expand All @@ -84,7 +84,7 @@ export const ExecuteWorkflowPage = () => {
setIsExecuting(false);
}
},
[orchestratorApi, workflowId, navigate, instanceLink, effectiveInstanceId],
[orchestratorApi, workflowId, navigate, instanceLink, assessmentInstanceId],
);

const error = responseError || workflowNameError;
Expand Down

0 comments on commit 7e9db47

Please sign in to comment.