some changes to support redirecting when loading an extension (#806)
Co-authored-by: jasquat <jasquat@users.noreply.github.com>
This commit is contained in:
parent
9c38ca851b
commit
8e91ef58e6
|
@ -51,6 +51,7 @@ export interface UiSchemaPageDefinition {
|
||||||
markdown_instruction_filename?: string;
|
markdown_instruction_filename?: string;
|
||||||
navigate_instead_of_post_to_api?: boolean;
|
navigate_instead_of_post_to_api?: boolean;
|
||||||
navigate_to_on_form_submit?: string;
|
navigate_to_on_form_submit?: string;
|
||||||
|
navigate_to_on_load?: string;
|
||||||
on_form_submit?: UiSchemaAction;
|
on_form_submit?: UiSchemaAction;
|
||||||
on_load?: UiSchemaAction;
|
on_load?: UiSchemaAction;
|
||||||
open_links_in_new_tab?: boolean;
|
open_links_in_new_tab?: boolean;
|
||||||
|
@ -70,3 +71,10 @@ export interface ExtensionPostBody {
|
||||||
extension_input: any;
|
extension_input: any;
|
||||||
ui_schema_action?: UiSchemaAction;
|
ui_schema_action?: UiSchemaAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ExtensionApiResponse {
|
||||||
|
task_data: any;
|
||||||
|
|
||||||
|
rendered_results_markdown?: string;
|
||||||
|
redirect_to?: string;
|
||||||
|
}
|
||||||
|
|
|
@ -60,20 +60,55 @@ export default function Extension({ displayErrors = true }: OwnProps) {
|
||||||
MarkdownRenderer,
|
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(
|
const setConfigsIfDesiredSchemaFile = useCallback(
|
||||||
// eslint-disable-next-line sonarjs/cognitive-complexity
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
||||||
(extensionUiSchemaFile: ProcessFile | null, pm: ProcessModel) => {
|
(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 (
|
if (
|
||||||
extensionUiSchemaFile &&
|
extensionUiSchemaFile &&
|
||||||
(extensionUiSchemaFile as ProcessFile).file_contents
|
(extensionUiSchemaFile as ProcessFile).file_contents
|
||||||
|
@ -109,7 +144,8 @@ export default function Extension({ displayErrors = true }: OwnProps) {
|
||||||
postBody.ui_schema_action = pageDefinition.on_load;
|
postBody.ui_schema_action = pageDefinition.on_load;
|
||||||
HttpService.makeCallToBackend({
|
HttpService.makeCallToBackend({
|
||||||
path: `${targetUris.extensionListPath}/${pageDefinition.on_load.api_path}`,
|
path: `${targetUris.extensionListPath}/${pageDefinition.on_load.api_path}`,
|
||||||
successCallback: processLoadResult,
|
successCallback: (result: any) =>
|
||||||
|
processLoadResult(result, pageDefinition),
|
||||||
httpMethod: 'POST',
|
httpMethod: 'POST',
|
||||||
postBody,
|
postBody,
|
||||||
});
|
});
|
||||||
|
@ -124,6 +160,7 @@ export default function Extension({ displayErrors = true }: OwnProps) {
|
||||||
params.page_identifier,
|
params.page_identifier,
|
||||||
searchParams,
|
searchParams,
|
||||||
filesByName,
|
filesByName,
|
||||||
|
processLoadResult,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -150,28 +187,6 @@ export default function Extension({ displayErrors = true }: OwnProps) {
|
||||||
targetUris.extensionPath,
|
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) => {
|
const processSubmitResult = (result: any) => {
|
||||||
if (
|
if (
|
||||||
uiSchemaPageDefinition &&
|
uiSchemaPageDefinition &&
|
||||||
|
|
Loading…
Reference in New Issue