Skip to content

Commit

Permalink
Merge pull request #63 from DEFRA/SCW-583
Browse files Browse the repository at this point in the history
[583] construct summary page dynamically
  • Loading branch information
hughfdjackson authored Dec 11, 2024
2 parents a951016 + 75eaf74 commit 38c5463
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 88 deletions.
1 change: 1 addition & 0 deletions src/server/common/model/answer/answer-model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @import {AnswerErrors, AnswerValidationResult} from './validation.js'
* @import {Page} from '../../model/page/page-model.js'
*/

import { NotImplementedError } from '../../helpers/not-implemented-error.js'
Expand Down
2 changes: 1 addition & 1 deletion src/server/common/model/page/question-page-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class QuestionPage extends Page {

/**
* @param {AnswerModel} _answer
* @returns {Page}
* @returns {Page | QuestionPage}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
nextPage(_answer) {
Expand Down
7 changes: 4 additions & 3 deletions src/server/origin/on-off-farm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import { OnOffFarm } from '~/src/server/common/model/answer/on-off-farm.js'
import { QuestionPage } from '../../common/model/page/question-page-model.js'
import { exitPage } from '../../exit-page/index.js'
import { cphNumberPage } from '../cph-number/index.js'
import { QuestionPageController } from '../../common/controller/question-page-controller.js'
import { exitPage } from '~/src/server/exit-page/index.js'
import { cphNumberPage } from '~/src/server/origin/cph-number/index.js'

/** @import { AnswerErrors } from "~/src/server/common/model/answer/validation.js" */
/** @import { AnswerModel } from "~/src/server/common/model/answer/answer-model.js" */

export class OnOffFarmPage extends QuestionPage {
urlPath = '/origin/to-or-from-own-premises'
Expand All @@ -22,7 +23,7 @@ export class OnOffFarmPage extends QuestionPage {
view = 'origin/on-off-farm/index'
Answer = OnOffFarm

/** @param {OnOffFarm} answer */
/** @param {AnswerModel} answer */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
nextPage(answer) {
if (answer.value === 'on') {
Expand Down
48 changes: 29 additions & 19 deletions src/server/origin/summary/controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Origin } from '../../common/model/section/origin.js'
import { OnOffFarmPage } from '../on-off-farm/index.js'
import { QuestionPage } from '../../common/model/page/question-page-model.js'

const indexView = 'origin/summary/index.njk'
export const pageTitle =
Expand All @@ -9,32 +11,39 @@ export const heading = pageTitle
* @satisfies {Partial<ServerRoute>}
*/
export const originSummaryGetController = {
handler(req, h) {
handler(req, res) {
const origin = Origin.fromState(req.yar.get('origin'))
const { isValid, result } = origin.validate()

if (!isValid) {
if (!result.onOffFarm.isValid) {
return h.redirect(
'/origin/to-or-from-own-premises?redirect_uri=/origin/summary'
)
}
if (!result.cphNumber.isValid) {
return h.redirect('/origin/cph-number?redirect_uri=/origin/summary')
}
if (!result.address.isValid) {
return h.redirect('/origin/address?redirect_uri=/origin/summary')
/** @type {QuestionPage[]} */
const pages = []

/** @type {Page} */
let page = new OnOffFarmPage()

while (page instanceof QuestionPage) {
const currPage = origin[page.questionKey]
pages.push(page)
if (currPage.validate().isValid) {
page = page.nextPage(currPage)
} else {
return res.redirect(`${page.urlPath}?redirect_uri=/origin/summary`)
}
}

return h.view(indexView, {
const items = pages.map((visitedPage) => ({
key: visitedPage.question,
value: origin[visitedPage.questionKey].html,
url: `${visitedPage.urlPath}?redirect_uri=/origin/summary`,
visuallyHiddenKey: visitedPage.question,
attributes: {
'data-testid': `${visitedPage.questionKey}-change-link`
}
}))

return res.view(indexView, {
pageTitle,
heading,
origin: {
cphNumber: origin?.cphNumber.html,
address: origin.address.html,
onOffFarm: origin.onOffFarm.html
}
originSummary: items
})
}
}
Expand All @@ -50,4 +59,5 @@ export const originSummaryPostController = {

/**
* @import { ServerRoute } from '@hapi/hapi'
* @import { Page } from '../../common/model/page/page-model.js'
*/
95 changes: 32 additions & 63 deletions src/server/origin/summary/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,41 @@

{% extends 'layouts/questions.njk' %}

{% block questions %}
<h1 class="govuk-heading-l">{{pageTitle}}</h1>
{% macro createSummary(rows) %}

{{ govukSummaryList({
classes: "govuk-!-margin-bottom-9",
rows: [
{
key: {
text: "Are you moving the cattle on or off your farm or premises?"
},
value: {
text: origin.onOffFarm
},
actions: {
items: [
{
href: "to-or-from-own-premises?redirect_uri=/origin/summary",
text: "Change",
visuallyHiddenText: "Are you moving the cattle on or off your farm or premises?",
attributes: {
'data-testid': "on-off-change-link"
}
}
]
}
{% set summaryRows = [] %}
{% for row in originSummary %}
{% set rows = (summaryRows, summaryRows.push({
key: {
text: row.key,
classes: "govuk-!-width-one-half govuk-!-font-weight-regular"
},
{
key: {
text: "What is the County Parish Holding (CPH) number of your farm where the animals are moving off?"
},
value: {
text: origin.cphNumber
},
actions: {
items: [
{
href: "cph-number?redirect_uri=/origin/summary",
text: "Change",
visuallyHiddenText: "What is the County Parish Holding (CPH) number of your farm where the animals are moving off?",
attributes: {
'data-testid': "cph-change-link"
}
}
]
}
value: {
html: row.value
},
{
key: {
text: "What is the address of your farm or premises where the animals are moving off?"
},
value: {
html: origin.address
},
actions: {
items: [
{
href: "address?redirect_uri=/origin/summary",
text: "Change",
visuallyHiddenText: "What is the address of your farm or premises where the animals are moving off?",
attributes: {
'data-testid': "address-change-link"
}
}
]
}
actions: {
items: [
{
href: row.url,
text: "Change",
visuallyHiddenText: row.visuallyHiddenKey,
attributes: row.attributes
}
]
}
]
})) %}
{% endfor %}



{{ govukSummaryList({
classes: "govuk-!-margin-bottom-9",
rows: summaryRows
}) }}
{% endmacro %}

{% block questions %}
<h1 class="govuk-heading-l">{{pageTitle}}</h1>

{{ createSummary(originSummary) }}
{% endblock %}
4 changes: 2 additions & 2 deletions user-journey-tests/page-objects/origin/checkAnswersPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class OriginCheckAnswersPage extends Page {

// Change links
get changeOnOrOffLink() {
return $('[data-testid="on-off-change-link"]')
return $('[data-testid="onOffFarm-change-link"]')
}

get changeParishNumberLink() {
return $('[data-testid="cph-change-link"]')
return $('[data-testid="cphNumber-change-link"]')
}

get changeAddressLink() {
Expand Down

0 comments on commit 38c5463

Please sign in to comment.