Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,6 @@ pages:
id: 88d7b067-68c2-4fe1-9a9a-ea98f0752e03
- title: Select all the eligible land parcels for the location of your woodland
controller: CommonSelectLandParcelPageController
components:
- name: landParcels
type: CheckboxesField
title: Select land parcels
config:
enableMultipleParcelSelect: true
topSection: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,6 @@ pages:

- title: Select land parcel for actions
controller: CommonSelectLandParcelPageController
components:
- name: landParcels
type: CheckboxesField
title: Select land parcels
config:
enableMultipleParcelSelect: false
bottomSection: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import LandGrantsQuestionWithAuthCheckController from '../../controllers/auth/la
import { fetchParcels } from '../../services/land-grants.service.js'
import { mapParcelsToViewModel } from '../../view-models/parcel.view-model.js'
import { getParcelIdFromQuery, getParcelIdsFromPayload } from '../../utils/parcel-request.utils.js'
import { ComponentType } from '@defra/forms-model'

export default class CommonSelectLandParcelPageController extends LandGrantsQuestionWithAuthCheckController {
viewName = 'common-select-land-parcel'
Expand All @@ -14,12 +15,37 @@ export default class CommonSelectLandParcelPageController extends LandGrantsQues
* config:
* enableMultipleParcelSelect: true
*
* Ensures a `landParcels` CheckboxesField component exists on the page.
* If not defined in YAML, it is injected here so the form schema and state
* are consistent across all journeys.
*
* @param {FormModel} model
* @param {import('@defra/forms-model').Page} pageDef
* @param {PageQuestion} pageDef
*/
constructor(model, pageDef) {
super(model, pageDef)
const config = model.def.metadata?.pageConfig?.[pageDef.path] ?? {}

const existing = pageDef.components?.find((c) => c.name === 'landParcels')
/** @type {import('@defra/forms-model').PageQuestion} */
const patchedPageDef = {
...pageDef,
components: existing
? pageDef.components
: [
...(pageDef.components ?? []),
{
type: ComponentType.CheckboxesField,
name: 'landParcels',
title: 'Select land parcels',
list: 'landParcels',
options: {
required: true
}
}
]
}
super(model, patchedPageDef)

this.enableMultipleParcelSelect = config.enableMultipleParcelSelect === true
this.topSection = config.topSection || ''
this.bottomSection = config.bottomSection || ''
Expand Down Expand Up @@ -141,6 +167,7 @@ export default class CommonSelectLandParcelPageController extends LandGrantsQues

/**
* @import { FormContext, AnyFormRequest } from '@defra/forms-engine-plugin/engine/types.js'
* @import { PageQuestion } from '@defra/forms-model'
* @import { FormModel } from '@defra/forms-engine-plugin/engine/models/index.js'
* @import { ResponseObject, ResponseToolkit } from '@hapi/hapi'
* @import { ResponseToolkit } from '@hapi/hapi'
*/
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ describe('CommonSelectLandParcelPageController', () => {

afterEach(vi.clearAllMocks)

describe('constructor', () => {
it('does NOT inject CheckboxesField if landParcels already exists', () => {
const model = {
def: { metadata: { pageConfig: {} } }
}

const existingComponent = {
type: 'CheckboxesField',
name: 'landParcels',
title: 'Existing'
}

const pageDef = {
path: '/test',
components: [existingComponent]
}

const controller = new CommonSelectLandParcelPageController(model, pageDef)

const components =
controller.form?.definition?.pages?.[0]?.components || controller.pageDef?.components || pageDef.components

expect(components).toEqual([existingComponent])
})
})

describe('resolveParcelIds', () => {
it('returns payload values for POST', () => {
const controller = createController()
Expand Down
Loading