Skip to content

Commit

Permalink
revert test.json, dynamic pages looping + reading values. return
Browse files Browse the repository at this point in the history
  • Loading branch information
jenbutongit committed Jul 1, 2020
1 parent fceaae9 commit 5b55fa7
Show file tree
Hide file tree
Showing 7 changed files with 509 additions and 79 deletions.
12 changes: 5 additions & 7 deletions engine/src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Model {
constructor (def, options) {
const result = schema.validate(def, { abortEarly: false })

//TODO:- throw/catch this properly 🤦🏻‍
if (result.error) {
throw result.error
}
Expand Down Expand Up @@ -80,21 +81,18 @@ class Model {
if (sectionPages.length > 0) {
if (section) {
const isRepeatable = sectionPages.find(page => page.pageDef.repeatField)
let sectionSchema

sectionSchema = joi.object().required()
let sectionSchema = joi.object().required()
sectionPages.forEach(sectionPage => {
sectionSchema = sectionSchema.concat(sectionPage.stateSchema)
})
if(isRepeatable) {
sectionSchema = joi.array().items(sectionSchema)
}
schema = schema.append({
[section.name]: sectionSchema
})

if(!isRepeatable ||( isRepeatable && !schema[section.name])) {
schema = schema.append({
[section.name]: sectionSchema
})
}
} else {
sectionPages.forEach(sectionPage => {
schema = schema.concat(sectionPage.stateSchema)
Expand Down
7 changes: 3 additions & 4 deletions engine/src/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class Page {
}).filter(v => !!v)
}

getNextPage (state) {
let defaultLink
const nextLink = this.next.find(link => {
const { page, condition } = link
Expand Down Expand Up @@ -148,7 +147,7 @@ class Page {
getFormDataFromState (state, atIndex) {
const pageState = this.section ? state[this.section.name] : state
if(this.repeatField) {
let repeatedPageState = pageState?.[atIndex ?? (pageState.length || 1) -1] ?? {}
let repeatedPageState = pageState?.[atIndex ?? (pageState.length - 1 || 0)] ?? {}
let values = Object.values(repeatedPageState)
return this.components.getFormDataFromState(values.length ? values.reduce((acc, page) => ({...acc, ...page})) : {})
}
Expand Down Expand Up @@ -215,7 +214,7 @@ class Page {
const currentPath = `/${this.model.basePath}${this.path}`
const startPage = this.model.def.startPage
const { num } = request.query
const formData = this.getFormDataFromState(state, num)
const formData = this.getFormDataFromState(state, num - 1)

if (!this.model.options.previewMode && progress.length === 0 && this.path !== `${startPage}`) {
return startPage.startsWith('http') ? h.redirect(startPage) : h.redirect(`/${this.model.basePath}${startPage}`)
Expand Down Expand Up @@ -338,7 +337,7 @@ class Page {
let updateValue = {[this.path]: update[this.section.name]}
let sectionState = state[this.section.name]
if (!sectionState) {
update = { [this.section.name]: [updateValue]}
update = { [this.section.name]: [updateValue] }
} else if(!sectionState[num-1]) {
sectionState.push(updateValue)
update = { [this.section.name]: sectionState }
Expand Down
251 changes: 251 additions & 0 deletions runner/src/server/forms/dynamic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
{
"conditions": [
{
"name": "hasUKPassport",
"value": "checkBeforeYouStart.ukPassport==true"
},
{
"name": "doesntHaveUKPassport",
"value": "checkBeforeYouStart.ukPassport==false"
},
{
"name": "moreThanOneApplicant",
"value": "applicantDetails.numberOfApplicants > 1"
},
{
"name": "moreThanTwoApplicants",
"value": "applicantDetails.numberOfApplicants > 2"
},
{
"name": "moreThanThreeApplicants",
"value": "applicantDetails.numberOfApplicants > 3"
}
],
"startPage": "/uk-passport",
"pages": [
{
"path": "/uk-passport",
"components": [
{
"type": "YesNoField",
"name": "ukPassport",
"title": "Do you have a UK passport?"
}
],
"section": "checkBeforeYouStart",
"next": [
{
"path": "/how-many-people"
},
{
"path": "/no-uk-passport",
"condition": "doesntHaveUKPassport"
}
],
"title": "Do you have a UK passport?"
},
{
"path": "/no-uk-passport",
"title": "You're not eligible for this service",
"components": [
{
"type": "Para",
"content": "If you still think you're eligible please contact the Foreign and Commonwealth Office."
}
],
"next": []
},
{
"path": "/how-many-people",
"section": "applicantDetails",
"components": [
{
"options": {
"list": "numberOfApplicants",
"classes": "govuk-input--width-10"
},
"type": "SelectField",
"name": "numberOfApplicants",
"title": "How many applicants are there?"
}
],
"next": [
{
"path": "/applicant-repeatable"
}
],
"title": "How many applicants are there?"
},
{
"path": "/applicant-repeatable",
"title": "Applicant",
"section": "applicant",
"repeatField": "applicantDetails.numberOfApplicants",
"components": [
{
"type": "Para",
"content": "Provide the details as they appear on your passport."
},
{
"type": "TextField",
"name": "firstName",
"title": "First name"
},
{
"options": {
"required": false,
"optionalText": false
},
"type": "TextField",
"name": "middleName",
"title": "Middle name",
"hint": "If you have a middle name on your passport you must include it here"
},
{
"type": "TextField",
"name": "lastName",
"title": "Surname"
}
],
"next": [
{
"path": "/applicant-address"
}
]
},
{
"path": "/applicant-address",
"section": "applicant",
"repeatField": "applicantDetails.numberOfApplicants",
"components": [
{
"type": "UkAddressField",
"name": "address",
"title": "Address"
}
],
"next": [
{
"path": "/contact-details"
}
],
"title": "Address"
},
{
"path": "/contact-details",
"section": "applicant",
"repeatField": "applicantDetails.numberOfApplicants",
"components": [
{
"type": "TelephoneNumberField",
"name": "phoneNumber",
"title": "Phone number",
"hint": "If you haven't got a UK phone number, include country code"
},
{
"type": "EmailAddressField",
"name": "emailAddress",
"title": "Your email address"
}
],
"next": [
{
"path": "/summary"
}
],
"title": "Applicant contact details"
},
{
"path": "/summary",
"controller": "./pages/summary.js",
"title": "Summary",
"components": [],
"next": []
}
],
"sections": [
{
"name": "checkBeforeYouStart",
"title": "Check before you start"
},
{
"name": "applicantDetails",
"title": "Applicant details"
},
{
"name": "applicant",
"title": "Applicant"
}
],
"lists": [
{
"name": "numberOfApplicants",
"title": "Number of people",
"type": "number",
"items": [
{
"text": "1",
"value": "1",
"description": "",
"condition": ""
},
{
"text": "2",
"value": "2",
"description": "",
"condition": ""
},
{
"text": "3",
"value": "3",
"description": "",
"condition": ""
},
{
"text": "4",
"value": "4",
"description": "",
"condition": ""
}
]
}
],
"fees": [
{
"description": "Application fee",
"amount": 5000,
"condition": "hasUKPassport",
"multiplier": "applicantDetails.numberOfApplicants"
},
{
"description": "Postage",
"amount": 1000,
"condition": "hasUKPassport"
}
],
"payApiKey": "<GOV.UK Pay API key>",
"outputs": [
{
"name": "formsapi",
"type": "webhook",
"outputConfiguration": {
"url": "http://forms-api:9000/v1/forms"
}
},
{
"name": "confirmationemail",
"type": "notify",
"outputConfiguration": {
"templateId": "<GOV.UK Notify template ID>",
"personalisation": [
"applicantOneDetails.firstName",
"applicantOneDetails.lastName"
],
"emailField": "applicantDetails.emailAddress",
"apiKey": "<GOV.UK Notify API key>"
}
}
],
"skipSummary": false,
"declaration": "<p class=\"govuk-body\">All the answers you have provided are true to the best of your knowledge.</p>"
}

0 comments on commit 5b55fa7

Please sign in to comment.