some changes to support redirecting when loading an extension (#806)

Co-authored-by: jasquat <jasquat@users.noreply.github.com>
This commit is contained in:
jasquat 2023-12-12 16:16:01 -05:00 committed by GitHub
parent 9c38ca851b
commit 8e91ef58e6
2 changed files with 57 additions and 34 deletions

View File

@ -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;
@ -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;
}

View File

@ -60,11 +60,41 @@ export default function Extension({ displayErrors = true }: OwnProps) {
MarkdownRenderer,
};
const setConfigsIfDesiredSchemaFile = useCallback(
// eslint-disable-next-line sonarjs/cognitive-complexity
(extensionUiSchemaFile: ProcessFile | null, pm: ProcessModel) => {
const processLoadResult = (result: any) => {
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
@ -72,8 +102,13 @@ export default function Extension({ displayErrors = true }: OwnProps) {
setMarkdownToRenderOnLoad(newMarkdown);
}
setReadyForComponentsToDisplay(true);
};
},
[interpolateNavigationString]
);
const setConfigsIfDesiredSchemaFile = useCallback(
// eslint-disable-next-line sonarjs/cognitive-complexity
(extensionUiSchemaFile: ProcessFile | null, pm: ProcessModel) => {
if (
extensionUiSchemaFile &&
(extensionUiSchemaFile as ProcessFile).file_contents
@ -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,
});
@ -124,6 +160,7 @@ export default function Extension({ displayErrors = true }: OwnProps) {
params.page_identifier,
searchParams,
filesByName,
processLoadResult,
]
);
@ -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 &&