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

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 10 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,16 @@
<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} %>
<%#= render partial: "avo/partials/resource_search", locals: {resource: @resource.route_key, via_reflection: via_reflection} %>

<input type="text"
name="q"
id="resource_search"
data-controller="resource-search"
data-resource-search-target="input"
data-action="input->resource-search#search"
placeholder="Type to search..."
class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring-primary-500 sm:text-sm">
<% else %>
<%# Offset for the space-y-2 property when the search is missing %>
<div class="-mb-2"></div>
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def index
end
add_breadcrumb @resource.plural_name.humanize

# Apply the search query if configured on the resource
set_index_params
apply_search
set_filters
set_actions
set_query
Expand Down Expand Up @@ -305,6 +307,9 @@ def set_index_params

set_pagination_params

# Search
@index_params[:q] = params[:q] if params[:q].present?

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

Expand Down Expand Up @@ -643,5 +648,19 @@ 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 @index_params[:q].nil?

search_query = @resource.search[:query]
return unless search_query.present?

@query = Avo::ExecutionContext.new(
target: @resource.class.search_query,
params: params,
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 @@ -33,6 +33,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 @@ -76,6 +77,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', SelectController)
Expand Down
24 changes: 24 additions & 0 deletions app/javascript/js/controllers/resource_search_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controller } from "@hotwired/stimulus"
import { get } from "@rails/request.js"

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

connect() {
console.log('Resource search controller connected')
}

async search() {
const query = this.inputTarget.value
const currentUrl = new URL(window.location.href)
currentUrl.searchParams.set('q', query)

try {
await get(currentUrl.pathname + currentUrl.search, {
responseKind: 'turbo-stream'
})
} catch (error) {
console.error('Error performing search:', error)
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@hotwired/stimulus": "^3.2.2",
"@hotwired/turbo-rails": "^8.0.12",
"@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/forms": "^0.5.10",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,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