generated from hmcts/expressjs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Hdpi 1779 address capture #615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 27 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
18b0ee2
fix: email address validation and trim all strings
paddy-hmcts 6a36d19
Merge branch 'master' into HDPI-1599-form-builder-date-and-email-issues
paddy-hmcts 05857b7
fix: date validation
paddy-hmcts 31e52b2
Merge branch 'master' into HDPI-1599-form-builder-date-and-email-issues
paddy-hmcts eb62035
chore: lint issues
paddy-hmcts 5b44f32
chore: lint issues
paddy-hmcts a53f713
test: fixed failing tests
paddy-hmcts 67e1782
test: fixed failing tests
paddy-hmcts 697866e
test: fixed failing tests
paddy-hmcts db7f885
fix: wrong error message
paddy-hmcts 8d10a4b
Merge branch 'master' into HDPI-1599-form-builder-date-and-email-issues
paddy-hmcts 7334be2
Merge branch 'master' into HDPI-1599-form-builder-date-and-email-issues
paddy-hmcts 73fa577
feat: postcode lookup
paddy-hmcts f138291
fix: summary page for address fields, changed format to use summary c…
paddy-hmcts fd2f37e
feat: address lookup and validation fixes
paddy-hmcts 98bff68
Merge branch 'master' into HDPI-1779-address-capture
paddy-hmcts ec5189b
chore: added example journey steps back in
paddy-hmcts fbb1a0d
test: added tests for lookup functionality on client
paddy-hmcts 71b74e5
test: added tests for lookup functionality on client
paddy-hmcts 2dcebc6
security: copilot suggestion
paddy-hmcts 69abf25
tests: coverage increase
paddy-hmcts 850856e
chore: lint issues
paddy-hmcts 2e2f8ed
Merge branch 'master' into HDPI-1779-address-capture
paddy-hmcts 2e25cf8
chore: sonar issues
paddy-hmcts ebc72aa
chore: sonar issues
paddy-hmcts 38be52f
chore: sonar/lint issues
paddy-hmcts 79f3613
chore: sonar/lint issues
paddy-hmcts 505d59c
Merge branch 'master' into HDPI-1779-address-capture
paddy-hmcts 76767e4
Merge branch 'master' into HDPI-1779-address-capture
paddy-hmcts cc6b186
Merge branch 'master' into HDPI-1779-address-capture
paddy-hmcts ca4ff9a
feat: implemented correspondence address page as per design
paddy-hmcts a1f7c9e
fix: sha hash for CSP update
paddy-hmcts 1849865
fix: sha hash for CSP update
paddy-hmcts cb067bb
fix: removed unneccessary fields and also fixed issue where validatio…
paddy-hmcts 8729ba5
Merge branch 'master' into HDPI-1779-address-capture
paddy-hmcts 99392ce
fix: removed unneccessary fields and also fixed issue where validatio…
paddy-hmcts 8d11df6
fix: removed data when field no longer becomes a required field
paddy-hmcts 5d58af7
fix: removed data when field no longer becomes a required field
paddy-hmcts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import '../scss/main.scss'; | ||
import { initAll } from 'govuk-frontend'; | ||
|
||
import { initPostcodeLookup } from './postcode-lookup'; | ||
import { initPostcodeSelection } from './postcode-select'; | ||
|
||
initAll(); | ||
initPostcodeSelection(); | ||
initPostcodeLookup(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
/** | ||
* Lightweight postcode lookup UI behaviour. | ||
* Expects the following DOM elements (ids are fixed for now): | ||
* - #lookupPostcode: input for entering postcode | ||
* - #findAddressBtn: button (type=button) to trigger lookup | ||
* - #selectedAddress: select populated with results | ||
* - #addressLine1, #addressLine2, #addressLine3, #town, #county, #postcode: inputs to populate | ||
*/ | ||
let postcodeLookupDelegatedBound = false; | ||
|
||
export function initPostcodeLookup(): void { | ||
const containers = Array.from(document.querySelectorAll<HTMLElement>('[data-address-component]')); | ||
|
||
// Helper utilities that work per-container | ||
const getParts = (container: HTMLElement) => { | ||
const prefix = container.dataset.namePrefix || 'address'; | ||
const byId = (id: string) => container.querySelector<HTMLElement>(`#${prefix}-${id}`); | ||
return { | ||
prefix, | ||
byId, | ||
postcodeInput: byId('lookupPostcode') as HTMLInputElement | null, | ||
findBtn: byId('findAddressBtn') as HTMLButtonElement | null, | ||
select: byId('selectedAddress') as HTMLSelectElement | null, | ||
selectContainer: byId('addressSelectContainer') as HTMLDivElement | null, | ||
addressLine1: byId('addressLine1') as HTMLInputElement | null, | ||
addressLine2: byId('addressLine2') as HTMLInputElement | null, | ||
addressLine3: byId('addressLine3') as HTMLInputElement | null, | ||
town: byId('town') as HTMLInputElement | null, | ||
county: byId('county') as HTMLInputElement | null, | ||
postcodeOut: byId('postcode') as HTMLInputElement | null, | ||
country: byId('country') as HTMLInputElement | null, | ||
details: container.querySelector('.govuk-details, details'), | ||
}; | ||
}; | ||
|
||
const clearOptions = (select: HTMLSelectElement) => { | ||
while (select.options.length) { | ||
select.remove(0); | ||
} | ||
}; | ||
|
||
const populateOptions = ( | ||
select: HTMLSelectElement, | ||
selectContainer: HTMLDivElement | null, | ||
addresses: Record<string, string>[] | ||
) => { | ||
clearOptions(select); | ||
const defaultOpt = document.createElement('option'); | ||
defaultOpt.value = ''; | ||
defaultOpt.textContent = `${addresses.length} address${addresses.length === 1 ? '' : 'es'} found`; | ||
select.appendChild(defaultOpt); | ||
|
||
for (let i = 0; i < addresses.length; i++) { | ||
const addr = addresses[i]; | ||
const opt = document.createElement('option'); | ||
opt.value = String(i); | ||
opt.textContent = addr.fullAddress || ''; | ||
opt.dataset.line1 = addr.addressLine1 || ''; | ||
opt.dataset.line2 = addr.addressLine2 || ''; | ||
opt.dataset.line3 = addr.addressLine3 || ''; | ||
opt.dataset.town = addr.town || ''; | ||
opt.dataset.county = addr.county || ''; | ||
opt.dataset.postcode = addr.postcode || ''; | ||
opt.dataset.country = addr.country || ''; | ||
select.appendChild(opt); | ||
} | ||
if (selectContainer) { | ||
selectContainer.hidden = false; | ||
} | ||
select.hidden = false; | ||
select.focus(); | ||
}; | ||
|
||
const populateAddressFields = (container: HTMLElement, selected: HTMLOptionElement) => { | ||
const { addressLine1, addressLine2, addressLine3, town, county, postcodeOut, country, details } = | ||
getParts(container); | ||
|
||
if (details && !(details as HTMLDetailsElement).open) { | ||
(details as HTMLDetailsElement).open = true; | ||
} | ||
|
||
const fieldMappings = [ | ||
{ field: addressLine1, value: selected.dataset.line1 }, | ||
{ field: addressLine2, value: selected.dataset.line2 }, | ||
{ field: addressLine3, value: selected.dataset.line3 }, | ||
{ field: town, value: selected.dataset.town }, | ||
{ field: county, value: selected.dataset.county }, | ||
{ field: postcodeOut, value: selected.dataset.postcode }, | ||
{ field: country, value: selected.dataset.country }, | ||
]; | ||
|
||
fieldMappings.forEach(({ field, value }) => { | ||
if (field) { | ||
field.value = value || ''; | ||
} | ||
}); | ||
|
||
addressLine1?.focus(); | ||
}; | ||
|
||
const handleSelectionChange = (container: HTMLElement, select: HTMLSelectElement) => { | ||
const selected = select.options[select.selectedIndex]; | ||
if (!selected?.value) { | ||
return; | ||
} | ||
populateAddressFields(container, selected); | ||
}; | ||
|
||
const performPostcodeLookup = async ( | ||
postcode: string, | ||
select: HTMLSelectElement, | ||
selectContainer: HTMLDivElement | null, | ||
button: HTMLButtonElement | ||
) => { | ||
button.disabled = true; | ||
try { | ||
const resp = await fetch(`/api/postcode-lookup?postcode=${encodeURIComponent(postcode)}`, { | ||
headers: { Accept: 'application/json' }, | ||
credentials: 'same-origin', | ||
}); | ||
if (!resp.ok) { | ||
throw new Error('Lookup failed'); | ||
} | ||
const json = (await resp.json()) as { addresses?: Record<string, string>[] }; | ||
const addresses = json.addresses || []; | ||
populateOptions(select, selectContainer, addresses); | ||
} catch { | ||
clearOptions(select); | ||
const opt = document.createElement('option'); | ||
opt.value = ''; | ||
opt.textContent = 'No addresses found'; | ||
select.appendChild(opt); | ||
if (selectContainer) { | ||
selectContainer.hidden = false; | ||
} | ||
select.hidden = false; | ||
} finally { | ||
button.disabled = false; | ||
} | ||
}; | ||
|
||
// If more than one component, use event delegation to avoid multiple handlers | ||
if (containers.length > 1) { | ||
if (postcodeLookupDelegatedBound) { | ||
return; | ||
} | ||
postcodeLookupDelegatedBound = true; | ||
|
||
document.addEventListener('click', async evt => { | ||
const target = evt.target as Element | null; | ||
if (!target) { | ||
return; | ||
} | ||
const btn = target.closest('button[id$="-findAddressBtn"]') as HTMLButtonElement; | ||
if (!btn) { | ||
return; | ||
} | ||
const container = btn.closest('[data-address-component]') as HTMLElement; | ||
if (!container) { | ||
return; | ||
} | ||
const { postcodeInput, select, selectContainer } = getParts(container); | ||
if (!postcodeInput || !select) { | ||
return; | ||
} | ||
|
||
const value = postcodeInput.value?.trim(); | ||
if (!value) { | ||
return; | ||
} | ||
await performPostcodeLookup(value, select, selectContainer, btn); | ||
}); | ||
|
||
document.addEventListener('change', evt => { | ||
const target = evt.target as Element | null; | ||
if (!target) { | ||
return; | ||
} | ||
const select = target.closest('select[id$="-selectedAddress"]') as HTMLSelectElement; | ||
if (!select) { | ||
return; | ||
} | ||
const container = select.closest('[data-address-component]') as HTMLElement; | ||
if (!container) { | ||
return; | ||
} | ||
handleSelectionChange(container, select); | ||
}); | ||
return; | ||
} | ||
|
||
// Fallback: bind directly when 0 or 1 components present | ||
if (!containers.length) { | ||
return; | ||
} | ||
|
||
containers.forEach(container => { | ||
const { postcodeInput, findBtn, select, selectContainer } = getParts(container); | ||
if (!postcodeInput || !findBtn || !select) { | ||
return; | ||
} | ||
|
||
// Selection behaviour for single component | ||
select.addEventListener('change', () => handleSelectionChange(container, select)); | ||
|
||
findBtn.addEventListener('click', async () => { | ||
const value = postcodeInput.value?.trim(); | ||
if (!value) { | ||
return; | ||
} | ||
await performPostcodeLookup(value, select, selectContainer, findBtn); | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.