Skip to content

Commit

Permalink
state wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jenbutongit committed Jul 1, 2020
1 parent f79ab6b commit 40d7174
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions engine/src/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ class Page {
return this.defaultNextPath
}

getFormDataFromState (state) {
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)})
}
return this.components.getFormDataFromState(pageState || {})
}

Expand Down Expand Up @@ -194,15 +198,22 @@ class Page {
return request.yar.get('lang')
}

#objLength (object) {
return Object.keys(object).length ?? 0
}

makeGetRouteHandler () {
return async (request, h) => {
const { cacheService } = request.services([])
const lang = this.langFromRequest(request)
const state = await cacheService.getState(request)
const formData = this.getFormDataFromState(state)
const progress = state.progress || []
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

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

0 comments on commit 40d7174

Please sign in to comment.