diff --git a/app/frontend/controllers/address_autocomplete_controller.js b/app/frontend/controllers/address_autocomplete_controller.js index 95d5060c0..d131e16a7 100644 --- a/app/frontend/controllers/address_autocomplete_controller.js +++ b/app/frontend/controllers/address_autocomplete_controller.js @@ -18,7 +18,6 @@ const fetchAndPopulateSearchResults = async (query, populateResults, populateOpt console.log(results) // address and uprn keys returned per result populateOptions(results, selectEl) populateResults(Object.values(results).map((o) => o.address)) - // populateResults(results) } } @@ -35,36 +34,10 @@ const populateOptions = (results, selectEl) => { }) } -// const populateOptions = (results, selectEl) => { -// selectEl.innerHTML = '' -// -// Object.keys(results).forEach((key) => { -// const option = document.createElement('option') -// option.value = key -// option.innerHTML = results[key].value -// if (results[key].hint) { option.setAttribute('data-hint', results[key].hint) } -// option.setAttribute('text', searchableName(results[key])) -// selectEl.appendChild(option) -// options.push(option) -// }) -// } - export default class extends Controller { connect () { const selectEl = this.element - const currentValue = this.getCurrentValue() - console.log(selectEl) - - if (currentValue && currentValue.stored_value) { - console.log(currentValue) - const option = document.createElement('option') - option.value = currentValue.stored_value.uprn - option.innerHTML = currentValue.stored_value.address - option.selected = true - selectEl.appendChild(option) - } - accessibleAutocomplete.enhanceSelectElement({ defaultValue: '', selectElement: selectEl, @@ -74,7 +47,7 @@ export default class extends Controller { }, autoselect: true, showNoOptionsFound: true, - placeholder: currentValue?.stored_value?.address || 'Start typing to search', + placeholder: 'Start typing to search', templates: { suggestion: (value) => value }, onConfirm: (val) => { const selectedResult = Array.from(selectEl.options).find(option => option.address === val) @@ -86,30 +59,5 @@ export default class extends Controller { }) } - fetchOptions(query, populateResults) { - fetch(`/address_options?query=${query}`) - .then(response => response.json()) - .then(data => { - console.log(data) - const results = data.map(result => result.uprn) - populateResults(results.slice(0, 10)) - }) - } - - async getCurrentValue() { - const currentPageUrl = window.location.href; - console.log(currentPageUrl); - const match = currentPageUrl.match(/sales-logs\/(\d+)\/address-search/); - const id = match ? match[1] : null; - - if (id) { - const response = await fetch(`/address_options/current?log_id=${id}`); - const data = await response.json(); - console.log(data); - return data; - } - - return null; - } } diff --git a/app/models/form/lettings/pages/address_search.rb b/app/models/form/lettings/pages/address_search.rb new file mode 100644 index 000000000..5837d1168 --- /dev/null +++ b/app/models/form/lettings/pages/address_search.rb @@ -0,0 +1,23 @@ +class Form::Lettings::Pages::AddressSearch < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = "address_search" + @depends_on = [ + { "uprn_known" => nil }, + { "uprn_known" => 0 }, + { "uprn_confirmed" => 0 }, + ] + end + + def questions + @questions ||= [ + Form::Lettings::Questions::AddressSearch.new(nil, nil, self), + ] + end + + def skip_href(log = nil) + return unless log + + "/#{log.model_name.param_key.dasherize}s/#{log.id}/property-unit-type" + end +end diff --git a/app/models/form/lettings/questions/address_search.rb b/app/models/form/lettings/questions/address_search.rb new file mode 100644 index 000000000..43ec1df37 --- /dev/null +++ b/app/models/form/lettings/questions/address_search.rb @@ -0,0 +1,30 @@ +class Form::Lettings::Questions::AddressSearch < ::Form::Question + def initialize(id, hsh, page) + super + @id = "address_search" + @type = "address_autocomplete" + @plain_label = true + end + + def answer_options(log = nil, _user = nil) + return {} unless ActiveRecord::Base.connected? + return {} unless log&.address_options + + answer_opts = {} + + (0...[log.address_options.count, 10].min).each do |i| + answer_opts[log.address_options[i][:uprn]] = { "value" => log.address_options[i][:address] } + end + + answer_opts["divider"] = { "value" => true } + answer_opts + end + + def displayed_answer_options(log, user = nil) + answer_options(log, user).transform_values { |value| value["value"] } || {} + end + + def hidden_in_check_answers?(log, _current_user = nil) + (log.uprn_known == 1 || log.uprn_confirmed == 1) + end +end diff --git a/app/models/form/lettings/questions/uprn_confirmation.rb b/app/models/form/lettings/questions/uprn_confirmation.rb index 2f03cb357..88e45c874 100644 --- a/app/models/form/lettings/questions/uprn_confirmation.rb +++ b/app/models/form/lettings/questions/uprn_confirmation.rb @@ -37,6 +37,6 @@ class Form::Lettings::Questions::UprnConfirmation < ::Form::Question end def hidden_in_check_answers?(log, _current_user = nil) - log.uprn_known != 1 || log.uprn_confirmed.present? + log.uprn_known != 1 || log.uprn_confirmed.present? || log.address_search.present? end end diff --git a/app/models/form/lettings/subsections/property_information.rb b/app/models/form/lettings/subsections/property_information.rb index 4d3e022c0..5bd04f2c6 100644 --- a/app/models/form/lettings/subsections/property_information.rb +++ b/app/models/form/lettings/subsections/property_information.rb @@ -34,6 +34,7 @@ class Form::Lettings::Subsections::PropertyInformation < ::Form::Subsection [ Form::Lettings::Pages::Uprn.new(nil, nil, self), Form::Lettings::Pages::UprnConfirmation.new(nil, nil, self), + Form::Lettings::Pages::AddressSearch.new(nil, nil, self), Form::Lettings::Pages::AddressMatcher.new(nil, nil, self), Form::Lettings::Pages::NoAddressFound.new(nil, nil, self), # soft validation Form::Lettings::Pages::UprnSelection.new(nil, nil, self), diff --git a/app/models/form/sales/pages/address_search.rb b/app/models/form/sales/pages/address_search.rb index 4db733f97..799291e60 100644 --- a/app/models/form/sales/pages/address_search.rb +++ b/app/models/form/sales/pages/address_search.rb @@ -2,7 +2,6 @@ class Form::Sales::Pages::AddressSearch < ::Form::Page def initialize(id, hsh, subsection) super @id = "address_search" - @copy_key = "sales.property_information.address_search" @depends_on = [ { "uprn_known" => nil }, { "uprn_known" => 0 }, diff --git a/app/models/form/sales/questions/address_search.rb b/app/models/form/sales/questions/address_search.rb index 16c4562c2..d9d318837 100644 --- a/app/models/form/sales/questions/address_search.rb +++ b/app/models/form/sales/questions/address_search.rb @@ -2,8 +2,6 @@ class Form::Sales::Questions::AddressSearch < ::Form::Question def initialize(id, hsh, page) super @id = "address_search" - @copy_key = "sales.property_information.address_search" - @error_label = "Enter search query" @type = "address_autocomplete" @plain_label = true end diff --git a/config/locales/forms/2024/lettings/property_information.en.yml b/config/locales/forms/2024/lettings/property_information.en.yml index 383a788ce..6af91d4f1 100644 --- a/config/locales/forms/2024/lettings/property_information.en.yml +++ b/config/locales/forms/2024/lettings/property_information.en.yml @@ -50,6 +50,13 @@ en: hint_text: "" question_text: "Select the correct address" + address_search: + page_header: "Find address" + check_answer_label: "Find address" + check_answer_prompt: "Search for address or UPRN" + hint_text: "Start typing an address to see suggestions and select the correct one. Some properties may not be available yet; you might need to enter them manually in the next question. For UPRN (Unique Property Reference Number), please enter the full value exactly." + question_text: "Enter the address or UPRN to search" + address: page_header: "Q12 - What is the property's address?" address_line1: diff --git a/config/locales/forms/2024/sales/property_information.en.yml b/config/locales/forms/2024/sales/property_information.en.yml index 6977616dc..45a52b4b7 100644 --- a/config/locales/forms/2024/sales/property_information.en.yml +++ b/config/locales/forms/2024/sales/property_information.en.yml @@ -43,6 +43,13 @@ en: hint_text: "" question_text: "Select the correct address" + address_search: + page_header: "Find address" + check_answer_label: "Find address" + check_answer_prompt: "Search for address or UPRN" + hint_text: "Start typing an address to see suggestions and select the correct one. Some properties may not be available yet; you might need to enter them manually in the next question. For UPRN (Unique Property Reference Number), please enter the full value exactly." + question_text: "Enter the address or UPRN to search" + address: page_header: "Q12 - What is the property's address?" address_line1: diff --git a/config/locales/forms/2025/lettings/property_information.en.yml b/config/locales/forms/2025/lettings/property_information.en.yml index 71a2b6124..db9b015a8 100644 --- a/config/locales/forms/2025/lettings/property_information.en.yml +++ b/config/locales/forms/2025/lettings/property_information.en.yml @@ -50,6 +50,13 @@ en: hint_text: "" question_text: "Select the correct address" + address_search: + page_header: "Find address" + check_answer_label: "Find address" + check_answer_prompt: "Search for address or UPRN" + hint_text: "Start typing an address to see suggestions and select the correct one. Some properties may not be available yet; you might need to enter them manually in the next question. For UPRN (Unique Property Reference Number), please enter the full value exactly." + question_text: "Enter the address or UPRN to search" + address: page_header: "Q12 - What is the property's address?" address_line1: diff --git a/config/locales/forms/2025/sales/property_information.en.yml b/config/locales/forms/2025/sales/property_information.en.yml index 2bfeb52a2..f891528f6 100644 --- a/config/locales/forms/2025/sales/property_information.en.yml +++ b/config/locales/forms/2025/sales/property_information.en.yml @@ -43,6 +43,13 @@ en: hint_text: "" question_text: "Select the correct address" + address_search: + page_header: "Find address" + check_answer_label: "Find address" + check_answer_prompt: "Search for address or UPRN" + hint_text: "Start typing an address to see suggestions and select the correct one. Some properties may not be available yet; you might need to enter them manually in the next question. For UPRN (Unique Property Reference Number), please enter the full value." + question_text: "Enter the address or UPRN to search" + address: page_header: "Q12 - What is the property's address?" address_line1: