Skip to content

Commit

Permalink
private method/props. reading state for repeatable pages
Browse files Browse the repository at this point in the history
  • Loading branch information
jenbutongit committed Jul 1, 2020
1 parent 40d7174 commit af7bf4d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
6 changes: 5 additions & 1 deletion engine/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
}]
],
"exclude": ["node_modules/**"],
"plugins": [ "@babel/plugin-proposal-export-default-from" ]
"plugins": [
"@babel/plugin-proposal-export-default-from",
"@babel/plugin-proposal-private-property-in-object",
"@babel/plugin-proposal-private-methods"
]
}

2 changes: 2 additions & 0 deletions engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"@babel/cli": "^7.10.1",
"@babel/core": "^7.10.2",
"@babel/plugin-proposal-export-default-from": "^7.10.1",
"@babel/plugin-proposal-private-methods": "^7.10.1",
"@babel/plugin-proposal-private-property-in-object": "^7.10.1",
"@babel/preset-env": "^7.10.2",
"@hapi/code": "^8.0.1",
"@hapi/lab": "^22.0.4",
Expand Down
30 changes: 19 additions & 11 deletions engine/src/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const joi = require('joi')
const { proceed } = require('./helpers')
const { ComponentCollection } = require('../components')
import { merge, reach } from '@hapi/hoek'
import * as querystring from 'querystring'

const FORM_SCHEMA = Symbol('FORM_SCHEMA')
const STATE_SCHEMA = Symbol('STATE_SCHEMA')
Expand Down Expand Up @@ -123,32 +124,40 @@ class Page {

getNext (state) {
const nextPage = this.getNextPage(state)
let query = ''
let query = {}
let queryString = ''
if(nextPage.repeatField) {
query.num = 0
const requiredCount = reach(state, nextPage.repeatField)
const otherRepeatPagesInSection = this.model.pages.filter(page => page.section === this.section && page.repeatField)
const sectionState = state[this.section.name]
const lastInSection = sectionState[sectionState.length -1] ?? {}
const sectionState = state[nextPage.section.name]
const lastInSection = sectionState?.[sectionState.length -1] ?? {}
const isLastComplete = Object.keys(lastInSection).length === otherRepeatPagesInSection.length
const currentIteration = sectionState ? isLastComplete ? sectionState.length + 1 : sectionState.length : 1
query.num = sectionState ? isLastComplete ? this.#objLength(sectionState) + 1 : this.#objLength(sectionState): 1

if(currentIteration < requiredCount) {
query = `?num=${currentIteration}`
if(query.num < requiredCount) {
queryString = `?${querystring.encode(query)}`
}
}

if (nextPage) {
return `/${this.model.basePath || ''}${nextPage.path}${query}`
return `/${this.model.basePath || ''}${nextPage.path}${queryString}`
}
return this.defaultNextPath
}

getFormDataFromState (state, atIndex) {
const pageState = this.section ? state[this.section.name] : state
if(this.repeatField) {
let repeatedPageState = pageState[atIndex ?? this.#objLength(pageState)]
return this.components.getFormDataFromState({...Object.values(repeatedPageState)})

let repeatedPageState = pageState?.[atIndex ?? (pageState.length || 1) -1] ?? {}
let values = Object.values(repeatedPageState)

return this.components.getFormDataFromState(values.length ? values.reduce((acc, page) => ({...acc, ...page})) : {})
}
//{
// "ukPassport": true
// }
return this.components.getFormDataFromState(pageState || {})
}

Expand Down Expand Up @@ -211,8 +220,7 @@ class Page {
const currentPath = `/${this.model.basePath}${this.path}`
const startPage = this.model.def.startPage
const { num } = request.query
const fromState = this.getFormDataFromState(state, num)
const formData = this.repeatField ? fromState[num ?? this.#objLength(fromState) - 1] : fromState
const formData = this.getFormDataFromState(state, num)

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
8 changes: 8 additions & 0 deletions engine/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@
"@babel/helper-create-class-features-plugin" "^7.10.1"
"@babel/helper-plugin-utils" "^7.10.1"

"@babel/plugin-proposal-private-property-in-object@^7.10.1":
version "7.10.1"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.10.1.tgz#1ae6b19dc971fc38cf5adb76330becc5bfe6f729"
integrity sha512-c4SfqvkXWB9Aw0+S/V6qxO0RF85/UTAiwVC6LYM5an+3GJDGyMqvGKQyzpdXGmjyImizRqkQs3vg6Fe0gtXcNg==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.10.1"
"@babel/helper-plugin-utils" "^7.10.1"

"@babel/plugin-proposal-unicode-property-regex@^7.10.1", "@babel/plugin-proposal-unicode-property-regex@^7.4.4":
version "7.10.1"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-unicode-property-regex/-/plugin-proposal-unicode-property-regex-7.10.1.tgz#dc04feb25e2dd70c12b05d680190e138fa2c0c6f"
Expand Down

0 comments on commit af7bf4d

Please sign in to comment.