Skip to content

Feature/ability to see search result in index page's table #3712

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

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
9d96e76
chore: add @rails/request.js
SahSantoshh Jan 29, 2025
2f7cf2d
feat: add resource search controller
SahSantoshh Jan 29, 2025
f32a1c1
feat: add apply search query method to search resource items
SahSantoshh Jan 29, 2025
2c72da1
feat: set search param to index params also try to use
SahSantoshh Jan 29, 2025
fb8b887
feat: move apply_search method after @query is initialized
SahSantoshh Mar 4, 2025
afdb511
feature: inline search working now
SahSantoshh Apr 7, 2025
203253c
refactor: set_index_params to improve Cognitive Complexity analysis
SahSantoshh Apr 7, 2025
d34cc40
feat: wip add debounce for index search
SahSantoshh Apr 9, 2025
29ca638
replace pagination & update query params
Paul-Bob Apr 16, 2025
79af19d
refactor: remove unused, debug code
SahSantoshh May 1, 2025
2ce58b5
refactor: set oder for pagination and search params
SahSantoshh May 1, 2025
c521256
refactor: fix grid view search
SahSantoshh May 6, 2025
56b4371
refactor: remove search param from index param
SahSantoshh May 18, 2025
51b92c2
Merge branch 'main' into feature/ability_to_see_search_result_in_inde…
SahSantoshh Jun 3, 2025
e404460
Merge branch 'main' into feature/ability_to_see_search_result_in_inde…
SahSantoshh Jun 15, 2025
816fa27
Merge branch 'main' into feature/ability_to_see_search_result_in_inde…
SahSantoshh Jun 30, 2025
5084c46
Merge branch 'main' into feature/ability_to_see_search_result_in_inde…
SahSantoshh Jul 1, 2025
c7fa582
Merge branch 'main' into feature/ability_to_see_search_result_in_inde…
SahSantoshh Jul 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/components/avo/paginator_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="flex flex-col sm:flex-row items-center justify-between aborder-t aborder-slate-200 rounded-2xl space-y-2 sm:space-y-0">
<div id="<%= "#{@resource.model_key}_pagination" %>" class="flex flex-col sm:flex-row items-center justify-between aborder-t aborder-slate-200 rounded-2xl space-y-2 sm:space-y-0">
<div class="flex-2 sm:flex sm:items-center sm:justify-between">
<div>
<div class="text-sm leading-5 text-slate-600 flex items-center">
Expand Down
12 changes: 11 additions & 1 deletion app/components/avo/views/resource_index_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@
<div class="flex flex-col xs:flex-row xs:justify-between space-y-2 xs:space-y-0 py-4 <%= "hidden" unless header_visible? %>">
<div class="flex items-center px-4 w-64">
<% if show_search_input %>
<%= render partial: "avo/partials/resource_search", locals: {resource: @resource.route_key, via_reflection: via_reflection} %>
<label for="resource_search"></label>
<input type="text"
name="q"
id="resource_search"
data-controller="resource-search"
data-resource-search-target="input"
data-action="input->resource-search#search"
placeholder="<%= I18n.translate 'avo.search.placeholder' %>"
data-resource-search-debounce-value="500"
class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring-primary-500 sm:text-sm"
value="<%= params[:q] %>">
<% else %>
<%# Offset for the space-y-2 property when the search is missing %>
<div class="-mb-2"></div>
Expand Down
58 changes: 52 additions & 6 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def index
@query = @query.includes(*@resource.includes)
end

# Apply the search query if configured on the resource
apply_search

# Eager load attachments
if @resource.attachments.present?
@resource.attachments.each do |attachment|
Expand Down Expand Up @@ -64,6 +67,38 @@ def index
end

set_component_for __method__

respond_to do |format|
format.html
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace(
"#{@resource.model_key}_list",
partial: "avo/index/resource_#{@resource.view_type.to_sym == :grid ? :grid : :table}_component",
locals: {
resources: @resources,
resource: @resource,
reflection: @reflection,
parent_record: @parent_record,
parent_resource: @parent_resource,
pagy: @pagy,
query: @query,
actions: @actions
}
),
turbo_stream.replace("#{@resource.model_key}_pagination") do
Avo::Current.view_context.render Avo::PaginatorComponent.new(
pagy: @pagy,
turbo_frame: @turbo_frame,
index_params: @index_params,
resource: @resource,
parent_record: @parent_record,
parent_resource: @parent_resource
)
end
]
end
end
end

def show
Expand Down Expand Up @@ -92,7 +127,6 @@ def show
add_breadcrumb @resource.plural_name.humanize, resources_path(resource: @resource)
end


add_breadcrumb @resource.record_title
add_breadcrumb I18n.t("avo.details").upcase_first

Expand Down Expand Up @@ -325,17 +359,18 @@ def set_index_params
@index_params = {}

set_pagination_params

# Sorting
@index_params[:sort_by] = params[:sort_by] || @resource.sort_by_param

@index_params[:sort_direction] = params[:sort_direction] || @resource.default_sort_direction
set_sorting_params

if @resource.available_view_types.exclude? @resource.view_type.to_sym
raise "View type '#{@resource.view_type}' is unavailable for #{@resource.class}."
end
end

def set_sorting_params
@index_params[:sort_by] = params[:sort_by] || @resource.sort_by_param
@index_params[:sort_direction] = params[:sort_direction] || @resource.default_sort_direction
end

def set_filters
@filters = @resource
.get_filters
Expand Down Expand Up @@ -644,5 +679,16 @@ def set_pagination_params
def set_query
@query ||= @resource.class.query_scope
end

def apply_search
return if @resource.class.search_query.nil?
return if params[:q].nil?

@query = Avo::ExecutionContext.new(
target: @resource.class.search_query,
params: params.merge(q: params[:q]),
query: @query
).handle
end
end
end
2 changes: 2 additions & 0 deletions app/javascript/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import RecordSelectorController from './controllers/record_selector_controller'
import ReloadBelongsToFieldController from './controllers/fields/reload_belongs_to_field_controller'
import ResourceEditController from './controllers/resource_edit_controller'
import ResourceIndexController from './controllers/resource_index_controller'
import ResourceSearchController from './controllers/resource_search_controller'
import ResourceShowController from './controllers/resource_show_controller'
import SearchController from './controllers/search_controller'
import SelectController from './controllers/select_controller'
Expand Down Expand Up @@ -82,6 +83,7 @@ application.register('preview', PreviewController)
application.register('record-selector', RecordSelectorController)
application.register('resource-edit', ResourceEditController)
application.register('resource-index', ResourceIndexController)
application.register('resource-search', ResourceSearchController)
application.register('resource-show', ResourceShowController)
application.register('search', SearchController)
application.register('select-filter', SelectFilterController)
Expand Down
64 changes: 64 additions & 0 deletions app/javascript/js/controllers/resource_search_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Controller } from '@hotwired/stimulus'
import { get } from '@rails/request.js'

export default class extends Controller {
static targets = ['input']

static values = {
debounce: { type: Number, default: 300 },
}

search() {
this.debouncedSearch()
}

debouncedSearch = this.debounce(this.performSearch, this.debounceValue)

async performSearch() {
const query = this.inputTarget.value
const currentUrl = new URL(window.location.href)

// Get existing search params
const searchParams = new URLSearchParams(window.location.search)

// Update the search parameter
if (query) {
searchParams.set('q', query)
} else {
searchParams.delete('q')
}

// Reset to first page when searching
searchParams.set('page', '1')

// Construct the new URL with all parameters
const newUrl = `${currentUrl.pathname}?${searchParams.toString()}`

// Replace current URL without affecting browser history
window.history.replaceState({}, '', newUrl)

try {
await get(newUrl, {
responseKind: 'turbo-stream',
headers: {
Accept: 'text/vnd.turbo-stream.html',
},
})
} catch (error) {
console.error('Error performing search:', error)
}
}

debounce(func, wait) {
let timeout

return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout)
func.apply(this, args)
}
clearTimeout(timeout)
timeout = setTimeout(later, wait)
}
}
}
8 changes: 8 additions & 0 deletions app/views/avo/index/_resource_grid_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= render Avo::Index::ResourceGridComponent.new(
resources: resources,
resource: resource,
reflection: reflection,
parent_record: parent_record,
parent_resource: parent_resource,
actions: actions
) %>
10 changes: 10 additions & 0 deletions app/views/avo/index/_resource_table_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= render Avo::Index::ResourceTableComponent.new(
resources: resources,
resource: resource,
reflection: reflection,
parent_record: parent_record,
parent_resource: parent_resource,
pagy: pagy,
query: query,
actions: actions
) %>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@hotwired/stimulus": "^3.2.2",
"@hotwired/turbo-rails": "^8.0.16",
"@rails/activestorage": "^6.1.710",
"@rails/request.js": "^0.0.11",
"@stimulus-components/clipboard": "^5.0.0",
"@stimulus-components/password-visibility": "^3.0.0",
"@tailwindcss/container-queries": "^0.1.1",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,11 @@
dependencies:
spark-md5 "^3.0.0"

"@rails/request.js@^0.0.11":
version "0.0.11"
resolved "https://registry.yarnpkg.com/@rails/request.js/-/request.js-0.0.11.tgz#4d9be25a49d97911c64ccd0f00b79d57fca4c3b4"
integrity sha512-2U3uYS0kbljt+pAstN+LIlZOl7xmOKig5N6FrvtUWO1wq0zR1Hf90fHfD2SYiyV8yH1nyKpoTmbLqWT0xe1zDg==

"@remirror/[email protected]":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@remirror/core-constants/-/core-constants-3.0.0.tgz#96fdb89d25c62e7b6a5d08caf0ce5114370e3b8f"
Expand Down