Skip to content

Commit

Permalink
feat: allow next links to be configured with a redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
jenbutongit committed Jan 25, 2024
1 parent 4899d98 commit b43746b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions model/src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b43746b

Please sign in to comment.