Skip to content

Commit

Permalink
some changes to support redirecting when loading an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jasquat committed Dec 11, 2023
1 parent 07f2cbc commit d74aa09
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
8 changes: 8 additions & 0 deletions spiffworkflow-frontend/src/extension_ui_schema_interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface UiSchemaPageDefinition {
markdown_instruction_filename?: string;
navigate_instead_of_post_to_api?: boolean;
navigate_to_on_form_submit?: string;
navigate_to_on_load?: string;
on_form_submit?: UiSchemaAction;
on_load?: UiSchemaAction;
open_links_in_new_tab?: boolean;
Expand All @@ -70,3 +71,10 @@ export interface ExtensionPostBody {
extension_input: any;
ui_schema_action?: UiSchemaAction;
}

export interface ExtensionApiResponse {
task_data: any;

rendered_results_markdown?: string;
redirect_to?: string;
}
83 changes: 49 additions & 34 deletions spiffworkflow-frontend/src/routes/Extension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,55 @@ export default function Extension({ displayErrors = true }: OwnProps) {
MarkdownRenderer,
};

const interpolateNavigationString = useCallback(
(navigationString: string, baseData: any) => {
let isValid = true;
const data = { backend_base_url: BACKEND_BASE_URL, ...baseData };
const optionString = navigationString.replace(/{(\w+)}/g, (_, k) => {
const value = data[k];
if (value === undefined) {
isValid = false;
addError({
message: `Could not find a value for ${k} in form data.`,
});
}
return value;
});
if (!isValid) {
return null;
}
return optionString;
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);
const processLoadResult = useCallback(
(result: any, pageDefinition: UiSchemaPageDefinition) => {
setFormData(result.task_data);
console.log('pageDefinition', pageDefinition);
if (pageDefinition.navigate_to_on_load) {
const optionString = interpolateNavigationString(
pageDefinition.navigate_to_on_load,
result.task_data
);
if (optionString !== null) {
window.location.href = optionString;
}
}
if (result.rendered_results_markdown) {
const newMarkdown = FormattingService.checkForSpiffFormats(
result.rendered_results_markdown
);
setMarkdownToRenderOnLoad(newMarkdown);
}
setReadyForComponentsToDisplay(true);
},
[interpolateNavigationString]
);

const setConfigsIfDesiredSchemaFile = useCallback(
// eslint-disable-next-line sonarjs/cognitive-complexity
(extensionUiSchemaFile: ProcessFile | null, pm: ProcessModel) => {
const processLoadResult = (result: any) => {
setFormData(result.task_data);
if (result.rendered_results_markdown) {
const newMarkdown = FormattingService.checkForSpiffFormats(
result.rendered_results_markdown
);
setMarkdownToRenderOnLoad(newMarkdown);
}
setReadyForComponentsToDisplay(true);
};

if (
extensionUiSchemaFile &&
(extensionUiSchemaFile as ProcessFile).file_contents
Expand Down Expand Up @@ -109,7 +144,8 @@ export default function Extension({ displayErrors = true }: OwnProps) {
postBody.ui_schema_action = pageDefinition.on_load;
HttpService.makeCallToBackend({
path: `${targetUris.extensionListPath}/${pageDefinition.on_load.api_path}`,
successCallback: processLoadResult,
successCallback: (result: any) =>
processLoadResult(result, pageDefinition),
httpMethod: 'POST',
postBody,
});
Expand All @@ -124,6 +160,7 @@ export default function Extension({ displayErrors = true }: OwnProps) {
params.page_identifier,
searchParams,
filesByName,
processLoadResult,
]
);

Expand All @@ -150,28 +187,6 @@ export default function Extension({ displayErrors = true }: OwnProps) {
targetUris.extensionPath,
]);

const interpolateNavigationString = (
navigationString: string,
baseData: any
) => {
let isValid = true;
const data = { backend_base_url: BACKEND_BASE_URL, ...baseData };
const optionString = navigationString.replace(/{(\w+)}/g, (_, k) => {
const value = data[k];
if (value === undefined) {
isValid = false;
addError({
message: `Could not find a value for ${k} in form data.`,
});
}
return value;
});
if (!isValid) {
return null;
}
return optionString;
};

const processSubmitResult = (result: any) => {
if (
uiSchemaPageDefinition &&
Expand Down

0 comments on commit d74aa09

Please sign in to comment.