|
| 1 | +/** |
| 2 | + * Lightweight postcode lookup UI behaviour. |
| 3 | + * Expects the following DOM elements (ids are fixed for now): |
| 4 | + * - #lookupPostcode: input for entering postcode |
| 5 | + * - #findAddressBtn: button (type=button) to trigger lookup |
| 6 | + * - #selectedAddress: select populated with results |
| 7 | + * - #addressLine1, #addressLine2, #town, #county, #postcode: inputs to populate |
| 8 | + */ |
| 9 | +let postcodeLookupDelegatedBound = false; |
| 10 | + |
| 11 | +export function initPostcodeLookup(): void { |
| 12 | + const containers = Array.from(document.querySelectorAll<HTMLElement>('[data-address-component]')); |
| 13 | + |
| 14 | + // Helper utilities that work per-container |
| 15 | + const getParts = (container: HTMLElement) => { |
| 16 | + const prefix = container.dataset.namePrefix || 'address'; |
| 17 | + const byId = (id: string) => container.querySelector<HTMLElement>(`#${prefix}-${id}`); |
| 18 | + return { |
| 19 | + prefix, |
| 20 | + byId, |
| 21 | + postcodeInput: byId('lookupPostcode') as HTMLInputElement | null, |
| 22 | + findBtn: byId('findAddressBtn') as HTMLButtonElement | null, |
| 23 | + select: byId('selectedAddress') as HTMLSelectElement | null, |
| 24 | + selectContainer: byId('addressSelectContainer') as HTMLDivElement | null, |
| 25 | + addressLine1: byId('addressLine1') as HTMLInputElement | null, |
| 26 | + addressLine2: byId('addressLine2') as HTMLInputElement | null, |
| 27 | + town: byId('town') as HTMLInputElement | null, |
| 28 | + county: byId('county') as HTMLInputElement | null, |
| 29 | + postcodeOut: byId('postcode') as HTMLInputElement | null, |
| 30 | + details: container.querySelector('.govuk-details, details'), |
| 31 | + }; |
| 32 | + }; |
| 33 | + |
| 34 | + const clearOptions = (select: HTMLSelectElement) => { |
| 35 | + while (select.options.length) { |
| 36 | + select.remove(0); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + const populateOptions = ( |
| 41 | + select: HTMLSelectElement, |
| 42 | + selectContainer: HTMLDivElement | null, |
| 43 | + addresses: Record<string, string>[] |
| 44 | + ) => { |
| 45 | + clearOptions(select); |
| 46 | + const defaultOpt = document.createElement('option'); |
| 47 | + defaultOpt.value = ''; |
| 48 | + defaultOpt.textContent = `${addresses.length} address${addresses.length === 1 ? '' : 'es'} found`; |
| 49 | + select.appendChild(defaultOpt); |
| 50 | + |
| 51 | + for (let i = 0; i < addresses.length; i++) { |
| 52 | + const addr = addresses[i]; |
| 53 | + const opt = document.createElement('option'); |
| 54 | + opt.value = String(i); |
| 55 | + opt.textContent = addr.fullAddress || ''; |
| 56 | + opt.dataset.line1 = addr.addressLine1 || ''; |
| 57 | + opt.dataset.line2 = addr.addressLine2 || ''; |
| 58 | + opt.dataset.town = addr.town || ''; |
| 59 | + opt.dataset.county = addr.county || ''; |
| 60 | + opt.dataset.postcode = addr.postcode || ''; |
| 61 | + select.appendChild(opt); |
| 62 | + } |
| 63 | + if (selectContainer) { |
| 64 | + selectContainer.hidden = false; |
| 65 | + } |
| 66 | + select.hidden = false; |
| 67 | + select.focus(); |
| 68 | + }; |
| 69 | + |
| 70 | + const populateAddressFields = (container: HTMLElement, selected: HTMLOptionElement) => { |
| 71 | + const { addressLine1, addressLine2, town, county, postcodeOut, details } = getParts(container); |
| 72 | + |
| 73 | + if (details && !(details as HTMLDetailsElement).open) { |
| 74 | + (details as HTMLDetailsElement).open = true; |
| 75 | + } |
| 76 | + |
| 77 | + const fieldMappings = [ |
| 78 | + { field: addressLine1, value: selected.dataset.line1 }, |
| 79 | + { field: addressLine2, value: selected.dataset.line2 }, |
| 80 | + { field: town, value: selected.dataset.town }, |
| 81 | + { field: county, value: selected.dataset.county }, |
| 82 | + { field: postcodeOut, value: selected.dataset.postcode }, |
| 83 | + ]; |
| 84 | + |
| 85 | + fieldMappings.forEach(({ field, value }) => { |
| 86 | + if (field) { |
| 87 | + field.value = value || ''; |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + addressLine1?.focus(); |
| 92 | + }; |
| 93 | + |
| 94 | + const handleSelectionChange = (container: HTMLElement, select: HTMLSelectElement) => { |
| 95 | + const selected = select.options[select.selectedIndex]; |
| 96 | + if (!selected?.value) { |
| 97 | + return; |
| 98 | + } |
| 99 | + populateAddressFields(container, selected); |
| 100 | + }; |
| 101 | + |
| 102 | + const performPostcodeLookup = async ( |
| 103 | + postcode: string, |
| 104 | + select: HTMLSelectElement, |
| 105 | + selectContainer: HTMLDivElement | null, |
| 106 | + button: HTMLButtonElement |
| 107 | + ) => { |
| 108 | + button.disabled = true; |
| 109 | + try { |
| 110 | + const resp = await fetch(`/api/postcode-lookup?postcode=${encodeURIComponent(postcode)}`, { |
| 111 | + headers: { Accept: 'application/json' }, |
| 112 | + credentials: 'same-origin', |
| 113 | + }); |
| 114 | + if (!resp.ok) { |
| 115 | + throw new Error('Lookup failed'); |
| 116 | + } |
| 117 | + const json = (await resp.json()) as { addresses?: Record<string, string>[] }; |
| 118 | + const addresses = json.addresses || []; |
| 119 | + populateOptions(select, selectContainer, addresses); |
| 120 | + } catch { |
| 121 | + clearOptions(select); |
| 122 | + const opt = document.createElement('option'); |
| 123 | + opt.value = ''; |
| 124 | + opt.textContent = 'No addresses found'; |
| 125 | + select.appendChild(opt); |
| 126 | + if (selectContainer) { |
| 127 | + selectContainer.hidden = false; |
| 128 | + } |
| 129 | + select.hidden = false; |
| 130 | + } finally { |
| 131 | + button.disabled = false; |
| 132 | + } |
| 133 | + }; |
| 134 | + |
| 135 | + // If more than one component, use event delegation to avoid multiple handlers |
| 136 | + if (containers.length > 1) { |
| 137 | + if (postcodeLookupDelegatedBound) { |
| 138 | + return; |
| 139 | + } |
| 140 | + postcodeLookupDelegatedBound = true; |
| 141 | + |
| 142 | + document.addEventListener('click', async evt => { |
| 143 | + const target = evt.target as Element | null; |
| 144 | + if (!target) { |
| 145 | + return; |
| 146 | + } |
| 147 | + const btn = target.closest('button[id$="-findAddressBtn"]') as HTMLButtonElement; |
| 148 | + if (!btn) { |
| 149 | + return; |
| 150 | + } |
| 151 | + const container = btn.closest('[data-address-component]') as HTMLElement; |
| 152 | + if (!container) { |
| 153 | + return; |
| 154 | + } |
| 155 | + const { postcodeInput, select, selectContainer } = getParts(container); |
| 156 | + if (!postcodeInput || !select) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + const value = postcodeInput.value?.trim(); |
| 161 | + if (!value) { |
| 162 | + return; |
| 163 | + } |
| 164 | + await performPostcodeLookup(value, select, selectContainer, btn); |
| 165 | + }); |
| 166 | + |
| 167 | + document.addEventListener('change', evt => { |
| 168 | + const target = evt.target as Element | null; |
| 169 | + if (!target) { |
| 170 | + return; |
| 171 | + } |
| 172 | + const select = target.closest('select[id$="-selectedAddress"]') as HTMLSelectElement; |
| 173 | + if (!select) { |
| 174 | + return; |
| 175 | + } |
| 176 | + const container = select.closest('[data-address-component]') as HTMLElement; |
| 177 | + if (!container) { |
| 178 | + return; |
| 179 | + } |
| 180 | + handleSelectionChange(container, select); |
| 181 | + }); |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + // Fallback: bind directly when 0 or 1 components present |
| 186 | + if (!containers.length) { |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + containers.forEach(container => { |
| 191 | + const { postcodeInput, findBtn, select, selectContainer } = getParts(container); |
| 192 | + if (!postcodeInput || !findBtn || !select) { |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + // Selection behaviour for single component |
| 197 | + select.addEventListener('change', () => handleSelectionChange(container, select)); |
| 198 | + |
| 199 | + findBtn.addEventListener('click', async () => { |
| 200 | + const value = postcodeInput.value?.trim(); |
| 201 | + if (!value) { |
| 202 | + return; |
| 203 | + } |
| 204 | + await performPostcodeLookup(value, select, selectContainer, findBtn); |
| 205 | + }); |
| 206 | + }); |
| 207 | +} |
0 commit comments