Skip to content

Commit

Permalink
Implement the dynamic radio options
Browse files Browse the repository at this point in the history
Build the options from the return versions against the licence in the session.
  • Loading branch information
Cruikshanks committed Jun 9, 2024
1 parent a500ac0 commit c9f8dd9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
20 changes: 20 additions & 0 deletions app/presenters/return-requirements/existing.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @module ExistingPresenter
*/

const { formatLongDate } = require('../base.presenter.js')
const { returnRequirementReasons } = require('../../lib/static-lookups.lib.js')

/**
* Formats data for the `/return-requirements/{sessionId}/existing` page
*
Expand All @@ -16,11 +19,28 @@ function go (session) {
const { id: sessionId, licence } = session

return {
existingOptions: _existingOptions(licence.returnVersions),
licenceRef: licence.licenceRef,
sessionId
}
}

function _existingOptions (returnVersions) {
return returnVersions.map((returnVersion) => {
const { id, reason, startDate } = returnVersion

// NOTE: because the session data is stored in a JSONB field when we get the instance from the DB the date values
// are in JS Date format (string). So, we have to convert it to a date before calling `formatLongDate()`
const dateObj = new Date(startDate)
const formattedStartDate = formatLongDate(dateObj)

return {
value: id,
text: reason ? `${formattedStartDate} - ${returnRequirementReasons[reason]}` : formattedStartDate
}
})
}

module.exports = {
go
}
33 changes: 26 additions & 7 deletions app/views/return-requirements/existing.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends 'layout.njk' %}
{% from "govuk/components/back-link/macro.njk" import govukBackLink %}
{% from "govuk/components/button/macro.njk" import govukButton %}
{% from "govuk/components/radios/macro.njk" import govukRadios %}

{% block breadcrumbs %}
{# Back link #}
Expand All @@ -14,14 +15,32 @@

{% block content %}
{# Main heading #}
{% set radioItems = [] %}
{% for existingOption in existingOptions %}
{% set radioItem = {
value: existingOption.value,
text: existingOption.text
} %}

{% set radioItems = (radioItems.push(radioItem), radioItems) %}
{% endfor %}

<div class="govuk-body">
<span class="govuk-caption-l"> Licence {{ licenceRef }} </span>
<h1 class="govuk-heading-xl govuk-!-margin-bottom-3">{{ pageTitle }}</h1>
</div>
<form method="post">
{{ govukRadios({
name: "existing",
errorMessage: error,
fieldset: {
legend: {
html: '<span class="govuk-caption-l">Licence ' + licenceRef + '</span>' + pageTitle,
isPageHeading: true,
classes: "govuk-fieldset__legend--l govuk-!-margin-bottom-6"
}
},
items: radioItems
}) }}

<form method="post">
<div class="govuk-body">
{{ govukButton({ text: "Continue", preventDoubleClick: true }) }}
</div>
</form>
</form>
</div>
{% endblock %}
30 changes: 30 additions & 0 deletions test/presenters/return-requirements/existing.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,39 @@ describe('Return Requirements - Existing presenter', () => {
const result = ExistingPresenter.go(session)

expect(result).to.equal({
existingOptions: [{ value: '60b5d10d-1372-4fb2-b222-bfac81da69ab', text: '1 January 2023' }],
licenceRef: '01/ABC',
sessionId: '61e07498-f309-4829-96a9-72084a54996d'
})
})
})

describe('the "existingOptions" property', () => {
describe('when the return versions do not contain a "reason"', () => {
it('returns the version ID as the option value and just the start date as the option text', () => {
const result = ExistingPresenter.go(session)

expect(result.existingOptions).to.equal([
{ value: '60b5d10d-1372-4fb2-b222-bfac81da69ab', text: '1 January 2023' }
])
})
})

describe('when the return versions contain a "reason"', () => {
beforeEach(() => {
session.licence.returnVersions.unshift({
id: '22ecef19-3a13-44a0-a55e-8f4d34dd59a5', reason: 'major-change', startDate: '2024-05-07T00:00:00.000Z'
})
})

it('returns the version ID as the option value and the start date and reason as the option text', () => {
const result = ExistingPresenter.go(session)

expect(result.existingOptions).to.equal([
{ value: '22ecef19-3a13-44a0-a55e-8f4d34dd59a5', text: '7 May 2024 - Major change' },
{ value: '60b5d10d-1372-4fb2-b222-bfac81da69ab', text: '1 January 2023' }
])
})
})
})
})
1 change: 1 addition & 0 deletions test/services/return-requirements/existing.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('Return Requirements - Existing service', () => {
expect(result).to.equal({
activeNavBar: 'search',
pageTitle: 'Select an existing return requirement from',
existingOptions: [{ value: '60b5d10d-1372-4fb2-b222-bfac81da69ab', text: '1 January 2023' }],
licenceRef: '01/ABC'
}, { skip: ['sessionId'] })
})
Expand Down

0 comments on commit c9f8dd9

Please sign in to comment.