Skip to content
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

Add remote typeahead spinner and no results message #2590

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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