Skip to content

Commit

Permalink
Feature: Add json button to search page (#562)
Browse files Browse the repository at this point in the history
* add json button ui to search page

* extract json_link method from annotator controller to application controller

* build the search page json link using json_link method

* move the search json button down besides the advanced options button

* remove this usage of api_params instead of direct params in search controller

---------

Co-authored-by: Syphax Bouazzouni <[email protected]>
  • Loading branch information
Bilelkihal and syphax-bouazzouni committed Apr 5, 2024
1 parent e9839ed commit 06a73eb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/search.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
}
.search-page-advanced-button{
display: flex;
align-items: center;
}
.search-page-advanced-button :hover{
cursor: pointer;
Expand Down
8 changes: 0 additions & 8 deletions app/controllers/annotator_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,6 @@ def empty_advanced_options
params[:fast_context].nil? &&
params[:lemmatize].nil?
end


def json_link(url, optional_params)
base_url = "#{url}?"
filtered_params = optional_params.reject { |_, value| value.nil? }
optional_params_str = filtered_params.map { |param, value| "#{param}=#{value}" }.join("&")
return base_url + optional_params_str + "&apikey=#{$API_KEY}"
end

def remove_special_chars(input)
regex = /^[a-zA-Z0-9\s]*$/
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,13 @@ def request_lang
helpers.request_lang
end

def json_link(url, optional_params)
base_url = "#{url}?"
filtered_params = optional_params.reject { |_, value| value.nil? }
optional_params_str = filtered_params.map { |param, value| "#{param}=#{value}" }.join("&")
return base_url + optional_params_str + "&apikey=#{$API_KEY}"
end

private
def not_found_record(exception)
@error_message = exception.message
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def index
params[:query] = nil
@advanced_options_open = false
@search_results = []
@json_url = json_link("#{rest_url}/search", {})

return if @search_query.empty?

Expand All @@ -19,6 +20,7 @@ def index

@advanced_options_open = !search_params_empty?
@search_results = aggregate_results(@search_query, results)
@json_url = json_link("#{rest_url}/search", params.permit!.to_h)

Check warning

Code scanning / Brakeman

Specify exact keys allowed for mass assignment instead of using permit! which allows any keys. Warning

Specify exact keys allowed for mass assignment instead of using permit! which allows any keys.
end

def json_search
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def url_to_endpoint(url)
endpoint
end

def search_json_link(link = @json_url, style: '')
custom_style = "font-size: 50px; line-height: 0.5; margin-left: 6px; #{style}".strip
render IconWithTooltipComponent.new(icon: "json.svg",link: link, target: '_blank', title: t('fair_score.go_to_api'), size:'small', style: custom_style)
end

def resolve_namespaces
RESOLVE_NAMESPACE
Expand Down
37 changes: 20 additions & 17 deletions app/views/search/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
= render Buttons::RegularButtonComponent.new(id:'search-page-button', value: "Search", variant: "primary", type: "submit") do |btn|
= btn.icon_right do
= inline_svg_tag "icons/search.svg"


.search-page-advanced{'data-reveal-component-target': 'item', class: "#{@advanced_options_open ? '' : 'd-none'}"}
.left
Expand All @@ -17,14 +17,14 @@
= t('search.advanced_options.search_language')
.field
= search_language_selector(name: 'lang', selected: params[:lang])

.filter-container
.title
= t("search.advanced_options.ontologies")
.field
= ontologies_selector(id:'search_page_ontologies' ,name: 'ontologies[]', selected: params[:ontologies]&.split(','))

.right
.right
.filter-container
.title
= t("search.advanced_options.include_in_search_title")
Expand All @@ -40,20 +40,23 @@
= render(ChipsComponent.new(name: 'require_exact_match', label: t('search.advanced_options.show_only_values.exact_matches'), checked: params[:require_exact_match]))
= render(ChipsComponent.new(name: 'require_definition', label: t('search.advanced_options.show_only_values.classes_with_definitions'), checked: params[:require_definition]))

.search-page-options
- if @search_results
.search-page-number-of-results
.search-page-options{class: @search_results.empty? ? 'justify-content-end': ''}
- unless @search_results.empty?
.search-page-number-of-results
= "#{t('search.match_in')} #{@search_results.length} #{t('search.ontologies')}"
.search-page-advanced-button.show-options{class: "#{@advanced_options_open ? 'd-none' : ''}",'data': {'action': 'click->reveal-component#show', 'reveal-component-target': 'showButton'}}
.icon
=inline_svg_tag 'icons/settings.svg'
.text
= t('search.show_advanced_options')
.search-page-advanced-button.hide-options{class: "#{@advanced_options_open ? '' : 'd-none'}", 'data': {'action': 'click->reveal-component#hide', 'reveal-component-target': 'hideButton'}}
.icon
=inline_svg_tag 'icons/hide.svg'
.text
= t('search.hide_advanced_options')
%div.d-flex
.search-page-json.mx-4
= search_json_link
.search-page-advanced-button.show-options{class: "#{@advanced_options_open ? 'd-none' : ''}",'data': {'action': 'click->reveal-component#show', 'reveal-component-target': 'showButton'}}
.icon
=inline_svg_tag 'icons/settings.svg'
.text
= t('search.show_advanced_options')
.search-page-advanced-button.hide-options{class: "#{@advanced_options_open ? '' : 'd-none'}", 'data': {'action': 'click->reveal-component#hide', 'reveal-component-target': 'hideButton'}}
.icon
=inline_svg_tag 'icons/hide.svg'
.text
= t('search.hide_advanced_options')
- if @search_results
.search-page-results-container
- number = 0
Expand All @@ -69,7 +72,7 @@
- number = number + 1
- c.reuse(r[:root].merge(is_sub_component: true, number: number)) do |b|
- r[:descendants].each { |dd| b.subresult(dd.merge(is_sub_component: true))}

- if @search_results.empty? && !@search_query.empty?
.browse-empty-illustration
%img{:src => "#{asset_path("empty-box.svg")}"}
Expand Down

0 comments on commit 06a73eb

Please sign in to comment.