Skip to content
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

fix(orchestrator): multiple bug fixes related to execution form #2573

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/warm-clouds-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@janus-idp/backstage-plugin-orchestrator-form-react": patch
"@janus-idp/backstage-plugin-orchestrator": patch
---

multiple bug fixes related to execution form
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