Skip to content

Commit

Permalink
wip.. almost..!
Browse files Browse the repository at this point in the history
  • Loading branch information
jenbutongit committed Jul 1, 2020
1 parent 0298514 commit fceaae9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 47 deletions.
4 changes: 2 additions & 2 deletions runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"postinstall": "npm run build",
"a11y": "node test/audit/components && node lighthouse",
"symlink-env": "./bin/symlink-config",
"babel-build": "babel src -d dist --copy-files --minified",
"babel-build:dev": "babel src -d dist --copy-files --watch"
"babel-build": "babel src -d dist --copy-files --minified -s inline",
"babel-build:dev": "babel src -d dist --copy-files --watch -s inline"
},
"repository": {
"type": "git",
Expand Down
16 changes: 0 additions & 16 deletions runner/src/server/forms/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,6 @@
{
"name": "applicant",
"title": "Applicant"
},
{
"name": "applicantOneDetails",
"title": "Applicant 1"
},
{
"name": "applicantTwoDetails",
"title": "Applicant 2"
},
{
"name": "applicantThreeDetails",
"title": "Applicant 3"
},
{
"name": "applicantFourDetails",
"title": "Applicant 4"
}
],
"lists": [
Expand Down
98 changes: 70 additions & 28 deletions runner/src/server/plugins/builder/pages/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const shortid = require('shortid')
const { formSchema } = require('../../../lib/formSchema')
const { serviceName } = require('../../../config')
const { flatten } = require('flat')
import * as querystring from 'querystring'
const { merge } = require('hoek')

class SummaryViewModel {
constructor (pageTitle, model, state) {
Expand All @@ -19,56 +21,65 @@ class SummaryViewModel {
nextPage = nextPage.getNextPage(state)
}

;[undefined].concat(model.sections).forEach((section) => {
[undefined, ...model.sections].forEach((section) => {
const items = []
const sectionState = section
? (state[section.name] || {})
: state

relevantPages.forEach(page => {
if (page.section === section) {
if (page.hasFormComponents) {
if (page.section === section) { //should this be a filter THEN for each..?
if (page.hasFormComponents) { //hm.. this is already handled in while(nextPage?.hasFormComponents)..?
const isRepeatable = page.repeatField
let repeatItems = []
page.components.formItems.forEach(component => {
items.push({
name: component.name,
path: component.path,
label: component.localisedString(component.title),
value: component.getDisplayStringFromState(sectionState),
rawValue: sectionState[component.name],
url: `/${model.basePath}${page.path}?returnUrl=/${model.basePath}/summary`,
pageId: `/${model.basePath}${page.path}`,
type: component.type
})

let item = this.Item(component, sectionState, page, model)
// items.push(item)
isRepeatable ? repeatItems.push(item) : items.push(item)
if (component.items) {
const selectedValue = sectionState[component.name]
const selectedItem = component.items.filter(i => i.value === selectedValue)[0]
if (selectedItem && selectedItem.conditional) {
selectedItem.conditional.componentCollection.formItems.forEach(cc => {
items.push({
name: cc.name,
path: cc.path,
label: cc.localisedString(cc.title),
value: cc.getDisplayStringFromState(sectionState),
rawValue: sectionState[cc.name],
url: `/${model.basePath}${page.path}?returnUrl=/${model.basePath}/summary`,
pageId: `/${model.basePath}${page.path}`,
type: cc.type
})
let cItem = this.Item(cc, sectionState, page, model)
isRepeatable ? repeatItems.push(cItem) : items.push(cItem)
})
}
}
})
if(repeatItems.length) {
// const collatePages = repeatItems.map((item, i) => {
// return item.map((field, j) => {
// return field[j]
// })
// })
items.push(repeatItems)
}
} else if (!(page instanceof SummaryPage) && !page.hasNext) {
endPage = page
}
}
})
if (items.length > 0) {
details.push({
name: section && section.name ? section.name : null,
title: section && section.title ? section.title : null,
items
})
if(Array.isArray(sectionState)) {
details.push({
name: section?.name,
title: section?.title,
items: items.map((item, i) => {
return item.map((field, j) => {
return field[j]
})
})
})
} else {
details.push({
name: section?.name,
title: section?.title,
items
})
}

}
})

Expand Down Expand Up @@ -307,6 +318,37 @@ class SummaryViewModel {
]
})
}

Item (component, sectionState, page, model, queryString = '') {
const isRepeatable = page.repeatField
let returnUrl = `/${model.basePath}/summary`

let query = {
returnUrl
}
if(isRepeatable && Array.isArray(sectionState)) {
return sectionState.map((state, i) => {
const query = {
returnUrl:`/${model.basePath}/summary`,
num: i + 1
}
let collated = Object.values(state).reduce((acc, p) => ({...acc, p}))
const qs = `?${querystring.encode(query)}`
return this.Item(component, collated, page, model, qs)
})
}

return {
name: component.name,
path: component.path,
label: component.localisedString(component.title),
value: component.getDisplayStringFromState(sectionState),
rawValue: sectionState[component.name],
url: `/${model.basePath}${page.path}${queryString}`,
pageId: `/${model.basePath}${page.path}`,
type: component.type
}
}
}

class SummaryPage extends Page {
Expand Down
4 changes: 3 additions & 1 deletion runner/src/server/plugins/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ module.exports = {
}
},
prepare: (options, next) => {
options.compileOptions.environment = nunjucks.configure(options.path, {
let environment = nunjucks.configure(options.path, {
autoescape: true,
watch: false
})
environment.addFilter('isArray', x => Array.isArray(x))
options.compileOptions.environment = environment

return next()
}
Expand Down

0 comments on commit fceaae9

Please sign in to comment.