Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow next links to be configured with a redirect #1190

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/runner/redirects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Redirects

Pages in the form JSON can be configured to go to the next page in the form, or redirect to a new URL. This happens when the user
"continues" to the next page, and any field validations do not fail.

To redirect to another URL, it must be a fully qualified URL (i.e. not a partial path). This will be useful if your service can be completed by another service or site. You must manually change the JSON to enable this feature. It is currently not supported in the designer.

```json5
{
"title": "Start",
"path": "/start",
"section": "beforeYouStart",
"components": [
{
"name": "country",
"type": "AutocompleteField",
"title": "Country",
"list": "SfkWjb"
}
],
"next": [
{
"path": "/second-page" // next page in form
},
{
"redirect": "http://localhost:3009/help/cookies", // a URL you wish to redirect to
"condition": "shouldRedirectToCookiesPage"
}
]
}
```

To go to the next page in the form, in the `next` array, add:
```json5
{
"path": "/second-page", // page.path of the next page in the form
"condition": ".." // optional, set up a condition if you only want the user to go to this page if the condition succeeds
}
```

To redirect the user to another URL
```json5
{
"redirect": "http://localhost:3009/help/cookies",
"condition": "shouldRedirectToCookiesPage" // optional
}

```

It is good practice to always have a page that does not have a condition attached, making it the "default" page.
This way, if the conditions fail to evaluate, the user will not see an error.

See [redirects.json](../../e2e/cypress/fixtures/redirects.json) for a full example.
18 changes: 18 additions & 0 deletions e2e/cypress/e2e/runner/redirect.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Feature: Back link fallback
As a service team,
I want to redirect to another URL
So that part of the service may be completed by another form or url

Scenario: Redirects to another page
Given the form "redirects" exists
When I navigate to the "redirects" form
And I enter "Turkey" for "Start"
And I continue
Then I see "Cookies are files saved on your phone, tablet or computer when you visit a website."

Scenario: Continues to next page
Given the form "redirects" exists
When I navigate to the "redirects" form
And I enter "Thailand" for "Start"
And I continue
Then I see "Second page"
87 changes: 87 additions & 0 deletions e2e/cypress/fixtures/redirects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"metadata": {},
"startPage": "/start",
"pages": [
{
"title": "Start",
"path": "/start",
"section": "beforeYouStart",
"components": [
{
"name": "country",
"type": "AutocompleteField",
"title": "Country",
"nameHasError": false,
"list": "SfkWjb"
}
],
"next": [
{
"path": "/second-page"
},
{
"redirect": "http://localhost:3009/help/cookies",
"condition": "shouldRedirectToCookiesPage"
}
]
},
{
"path": "/second-page",
"title": "Second page",
"components": [
{
"name": "SFtcpL",
"options": {},
"type": "Html",
"content": "<p class=\"govuk-body\">You chose the option {{country}}</p>"
}
],
"next": [
{
"path": "/summary"
}
]
},
{
"title": "Summary",
"path": "/summary",
"controller": "./pages/summary.js",
"components": [],
"next": []
}
],
"lists": [
{
"title": "Countries",
"name": "SfkWjb",
"type": "string",
"items": [
{
"text": "Turkey",
"value": "Turkey"
},
{
"text": "Thailand",
"value": "Thailand"
}
]
}
],
"sections": [
{
"title": "Before you start",
"name": "beforeYouStart"
}
],
"conditions": [
{
"name": "shouldRedirectToCookiesPage",
"value": "beforeYouStart.country == 'Turkey'"
}
],
"fees": [],
"outputs": [],
"version": 2,
"skipSummary": false,
"feeOptions": {}
}
6 changes: 5 additions & 1 deletion model/src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export const componentSchema = joi
.unknown(true);

const nextSchema = joi.object().keys({
path: joi.string().required(),
path: joi.string().when(joi.ref("redirect"), {
is: joi.exist(),
then: joi.string().optional(),
otherwise: joi.string().required(),
}),
condition: joi.string().allow("").optional(),
redirect: joi.string().uri().optional(),
});
Expand Down
Loading