From b43746b1fc73f1add16c0595e56eb6e4ceafb7d1 Mon Sep 17 00:00:00 2001 From: jen Date: Thu, 25 Jan 2024 14:54:59 +0000 Subject: [PATCH] feat: allow next links to be configured with a redirect --- model/src/schema/schema.ts | 1 + .../pageControllers/PageControllerBase.ts | 42 +++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/model/src/schema/schema.ts b/model/src/schema/schema.ts index 59de15acf7..fafbe169db 100644 --- a/model/src/schema/schema.ts +++ b/model/src/schema/schema.ts @@ -98,6 +98,7 @@ export const componentSchema = joi const nextSchema = joi.object().keys({ path: joi.string().required(), condition: joi.string().allow("").optional(), + redirect: joi.string().uri().optional(), }); /** diff --git a/runner/src/server/plugins/engine/pageControllers/PageControllerBase.ts b/runner/src/server/plugins/engine/pageControllers/PageControllerBase.ts index 50d3c21c7a..d45eb7201e 100644 --- a/runner/src/server/plugins/engine/pageControllers/PageControllerBase.ts +++ b/runner/src/server/plugins/engine/pageControllers/PageControllerBase.ts @@ -181,9 +181,16 @@ export class PageControllerBase { } get next() { - return (this.pageDef.next || []) - .map((next: { path: string }) => { + const pageDefNext = this.pageDef.next ?? []; + + return pageDefNext + .map((next: { path: string; redirect?: string }) => { const { path } = next; + + if (next?.redirect) { + return next; + } + const page = this.model.pages.find((page: PageControllerBase) => { return path === page.path; }); @@ -231,12 +238,20 @@ export class PageControllerBase { let defaultLink; const nextLink = this.next.find((link) => { const { condition } = link; - if (condition) { - return this.model.conditions[condition]?.fn?.(state); + if (!condition) { + defaultLink = link; + } + const conditionPassed = this.model.conditions[condition]?.fn?.(state); + if (conditionPassed) { + return link; } - defaultLink = link; return false; }); + + if (nextLink?.redirect) { + return nextLink; + } + return nextLink?.page ?? defaultLink?.page; } @@ -246,6 +261,9 @@ export class PageControllerBase { */ getNext(state: any) { const nextPage = this.getNextPage(state); + if (nextPage?.redirect) { + return nextPage.redirect; + } const query = { num: 0 }; let queryString = ""; if (nextPage?.repeatField) { @@ -403,7 +421,13 @@ export class PageControllerBase { relevantState = merge(relevantState, newValue); //By passing our current relevantState to getNextPage, we will check if we can navigate to this next page (including doing any condition checks if applicable) - nextPage = nextPage.getNextPage(relevantState); + const possibleNextPage = nextPage.getNextPage(relevantState); + if (possibleNextPage?.redirect) { + nextPage = null; + } else { + nextPage = possibleNextPage; + } + //If a nextPage is returned, we must have taken that route through the form so continue our iteration with the new page } @@ -732,7 +756,11 @@ export class PageControllerBase { * TODO:- proceed is interfering with subclasses */ proceed(request: HapiRequest, h: HapiResponseToolkit, state) { - return proceed(request, h, this.getNext(state)); + const nextPage = this.getNext(state); + if (nextPage?.redirect) { + return proceed(request, h, nextPage?.redirect); + } + return proceed(request, h, nextPage); } getPartialMergeState(value) {