Skip to content

Commit

Permalink
Add remote typeahead spinner and no results message
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWalkingLeek authored and amaierhofer committed May 1, 2024
1 parent b584e2f commit 009ebab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/helpers/standard_form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ def person_field(attr, html_options = {}) # rubocop:disable Metrics/MethodLength
disabled: disabled,
data: { provide: 'entity',
id_field: "#{object_name}_#{attr_id}",
no_results_message: I18n.t('global.no_list_entries'),
url: html_options&.dig(:data, :url) || @template.query_people_path })
end

Expand Down
31 changes: 22 additions & 9 deletions app/javascript/javascripts/modules/remote_autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,30 @@ import { mark } from "@tarekraafat/autocomplete.js/src/helpers/io";
if (input.dataset.typeaheadDisabled === "true") return;

try {
if (isQuickSearch) {
document.getElementById(QUICKSEARCH_ID).classList.add("input-loading");
}
input.classList.add("input-loading");

// Fetch data via AJAX request
const url = new URL(input.dataset.url, location.origin)
const queryKey = document.getElementById(input.id).dataset.param || "q";
url.searchParams.set(queryKey, query)
const url = new URL(input.dataset.url, location.origin);
const queryKey =
document.getElementById(input.id).dataset.param || "q";
url.searchParams.set(queryKey, query);
const source = await fetch(url);
const data = await source.json();

if (isQuickSearch) {
document.getElementById(QUICKSEARCH_ID).classList.remove("input-loading");
input.classList.remove("input-loading");
var noResultsMessage =
autoCompleteInput.input.dataset.noResultsMessage;
if (!!noResultsMessage && !data.length) {
return [
{
id: null,
label: noResultsMessage,
noResultsItem: true,
},
];
} else {
return data;
}
return data;
} catch (error) {
return error;
}
Expand All @@ -76,6 +85,10 @@ import { mark } from "@tarekraafat/autocomplete.js/src/helpers/io";
input: {
selection: (event) => {
var selection = event.detail.selection.value;
if (selection.noResultsItem) {
return;
}

autoCompleteInput.input.value = selection.label;

if (isQuickSearch) {
Expand Down
14 changes: 14 additions & 0 deletions spec/features/event/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ def click_save
end
end

it 'shows no results message for contact and does not interact with message click' do
sign_in
visit edit_path

notification_checkbox_visible(false)

query = 'querythatdoesnotmatch'
fill_in 'Kontaktperson', with: query
expect(find('ul[role="listbox"] li[role="option"]')).to have_content 'Keine Einträge gefunden.'
find('ul[role="listbox"] li[role="option"]').click

expect(find('#event_contact').value).to eq(query)
end

it 'toggles participation notifications' do
event.update(contact: people(:top_leader))

Expand Down

0 comments on commit 009ebab

Please sign in to comment.