Skip to content

Live search#6

Open
foxy-eyed wants to merge 3 commits intoday-2from
feature/live-search
Open

Live search#6
foxy-eyed wants to merge 3 commits intoday-2from
feature/live-search

Conversation

@foxy-eyed
Copy link
Owner

@foxy-eyed foxy-eyed commented Jul 31, 2023

Реализовать функционал «живого» поиска (search autocomplete), используя технологии Turbo Frames и Stimulus.

NY9ObJLDmT

Copy link

@palkan palkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отличный вариант 👍

q = params.fetch(:q, "")

if q.blank? || q.length < 3
if !turbo_frame_request? && q.length < 3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для превью тоже имеет смысл подождать, пока пользователь введёт первые символы; ограничение можно "продублировать" на клиенте через Values в контроллере, например (или вообще через стандартные HTML атрибуты).


reloadFrame() {
this.previewFrame.src = this.searchUrl;
this.previewFrame.reload();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А разве нужен отдельный reload()? Turbo Frame, на сколько я помню, отслеживает изменения src и сам обновляется.

onFrameUpdate() {
if (this.navigationalElements) {
this.navigationalElements.forEach((el) =>
el.removeEventListener("turbo:click", this.resetFrame),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хорошая идея — использовать turbo:click. Но навешивать на каждую ссылку не обязательно, можно было добавить только на контейнер (или даже в data-action прописать).


scope :search, ->(q) {
where(arel_table[:title].lower.matches("%#{q.downcase}%"))
q.blank? ? none : where(arel_table[:title].lower.matches("%#{q.downcase}%"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше так (надо использовать фишки скоупов):

Suggested change
q.blank? ? none : where(arel_table[:title].lower.matches("%#{q.downcase}%"))
where(arel_table[:title].lower.matches("%#{q.downcase}%")) if q.present?

"live-search-preview-frame-id-value": :search_results,
} %>
<% end %>
<%= turbo_frame_tag :search_results, class: "nav--search--frame", src: search_path %>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А нужен ли тут search_path? Кажется, он будет делать запрос при начальной загрузке страницы.

Comment on lines +39 to +41
return buildUrlWithQuery(this.searchPathValue, {
[this.searchParamNameValue]: this.query,
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот это красиво. Вроде и запрос конструируем сами, а вроде и всё контролируется извне.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants