From 575077c34cf990c027678d52226c95518fb2f5d3 Mon Sep 17 00:00:00 2001 From: Diego Steiner Date: Wed, 6 Dec 2023 13:12:31 +0000 Subject: [PATCH] feature: global searchbar --- app/javascript/stylesheets/application.scss | 12 ++++++ app/models/booking/filter.rb | 17 ++++---- app/params/manage/booking_filter_params.rb | 2 +- app/views/layouts/_top_navigation.html.slim | 42 ++++++++++++------- .../manage/bookings/_filter_fields.html.slim | 4 +- config/locales/de.yml | 3 +- config/locales/en.yml | 2 - config/locales/fr.yml | 2 - config/locales/it.yml | 2 - 9 files changed, 48 insertions(+), 38 deletions(-) diff --git a/app/javascript/stylesheets/application.scss b/app/javascript/stylesheets/application.scss index 218664ebf..95166c27e 100644 --- a/app/javascript/stylesheets/application.scss +++ b/app/javascript/stylesheets/application.scss @@ -240,3 +240,15 @@ tr.disabled { } } } + +#searchbar { + opacity: 0.5; + + &>input { + flex-grow: 1; + } + + &:has(input:focus) { + opacity: 1; + } +} diff --git a/app/models/booking/filter.rb b/app/models/booking/filter.rb index 798d48564..5bc7047b9 100644 --- a/app/models/booking/filter.rb +++ b/app/models/booking/filter.rb @@ -2,8 +2,7 @@ class Booking class Filter < ApplicationFilter - attribute :ref - attribute :tenant + attribute :q attribute :categories attribute :homes, default: -> { [] } attribute :occupiables, default: -> { [] } @@ -33,10 +32,6 @@ class Filter < ApplicationFilter bookings.where(occupancy_type:) if occupancy_type.present? end - filter :ref do |bookings| - bookings.where(Booking.arel_table[:ref].matches("%#{ref.strip}%")) if ref.present? - end - filter :homes do |bookings| relevant_homes = Array.wrap(homes).compact_blank bookings.joins(:occupancies).where(home_id: relevant_homes) if relevant_homes.present? @@ -57,12 +52,14 @@ class Filter < ApplicationFilter bookings.where(booking_category_id: category_ids) end - filter :tenant do |bookings| - next bookings if tenant.blank? + filter :q do |bookings| + next bookings if q.blank? + match = "%#{q.strip}%" bookings.joins(:tenant) - .where(Tenant.arel_table[:search_cache].matches("%#{tenant}%") - .or(Booking.arel_table[:tenant_organisation].matches("%#{tenant}%"))) + .where(Tenant.arel_table[:search_cache].matches(match) + .or(Booking.arel_table[:tenant_organisation].matches(match)) + .or(Booking.arel_table[:ref].matches("#{q.strip}%"))) end filter :has_booking_state do |bookings| diff --git a/app/params/manage/booking_filter_params.rb b/app/params/manage/booking_filter_params.rb index 262afcf26..075f2e601 100644 --- a/app/params/manage/booking_filter_params.rb +++ b/app/params/manage/booking_filter_params.rb @@ -3,7 +3,7 @@ module Manage class BookingFilterParams < ApplicationParams def self.permitted_keys - %i[tenant ref begins_at_before begins_at_after ends_at_before ends_at_after at_date] + + %i[q begins_at_before begins_at_after ends_at_before ends_at_after at_date] + [current_booking_states: [], previous_booking_states: [], homes: [], occupiables: []] end end diff --git a/app/views/layouts/_top_navigation.html.slim b/app/views/layouts/_top_navigation.html.slim index e4aa3f8a2..dd9c218bb 100644 --- a/app/views/layouts/_top_navigation.html.slim +++ b/app/views/layouts/_top_navigation.html.slim @@ -1,18 +1,28 @@ nav#navbar-top.navbar.fixed-top.navbar-light.navbar-expand-lg .container-fluid - = link_to home_path, class: 'navbar-brand' do - = image_tag asset_pack_path('static/images/logo.svg'), class: "d-inline-block align-top", width: 90 - - ul.navbar-nav.ms-auto - - if current_user.present? - li.nav-item.dropdown - a.nav-link.dropdown-toggle aria-expanded="false" aria-haspopup="true" data-bs-toggle="dropdown" href="#" - i.fa.fa-user - =<> current_user&.email || t(:'nav.sign_in') - .dropdown-menu aria-labelledby="navbarDropdownMenuLink" - = link_to t(:'nav.manage'), manage_root_path(org: current_organisation), class: 'dropdown-item' if current_organisation - = link_to t(:'nav.edit_account'), edit_account_path, class: 'dropdown-item' - = link_to t(:'nav.sign_out'), destroy_user_session_path, :method=>'delete', class: 'dropdown-item' - - else - li.nav-item - = link_to t(:'nav.sign_in'), new_user_session_path, class: 'nav-link' + .col-md-3.col-12 + = link_to home_path, class: 'navbar-brand' do + = image_tag asset_pack_path('static/images/logo.svg'), class: "d-inline-block align-top", width: 90 + + - if current_user.present? + .col-md-4.offset-md-1.col-12 + = form_with(url: manage_bookings_path, method: :get, id: 'searchbar', class: 'input-group d-flex') do |f| + button.btn.bg-white.border-0[type="submit"] + span.fa.fa-search + input.d-none[name="filter[current_booking_states]" value="" type="hidden"] + input.form-control.bg-white.border-0[name="filter[q]" type="search" tabindex=1 value=params.dig(:filter, :q).presence placeholder=Booking::Filter.human_attribute_name(:q)] + + .col-md-3.col-12 + ul.navbar-nav.justify-content-end + - if current_user.present? + li.nav-item.dropdown + a.nav-link.dropdown-toggle aria-expanded="false" aria-haspopup="true" data-bs-toggle="dropdown" href="#" + i.fa.fa-user + =<> current_user&.email || t(:'nav.sign_in') + .dropdown-menu aria-labelledby="navbarDropdownMenuLink" + = link_to t(:'nav.manage'), manage_root_path(org: current_organisation), class: 'dropdown-item' if current_organisation + = link_to t(:'nav.edit_account'), edit_account_path, class: 'dropdown-item' + = link_to t(:'nav.sign_out'), destroy_user_session_path, :method=>'delete', class: 'dropdown-item' + - else + li.nav-item + = link_to t(:'nav.sign_in'), new_user_session_path, class: 'nav-link' diff --git a/app/views/manage/bookings/_filter_fields.html.slim b/app/views/manage/bookings/_filter_fields.html.slim index d3445f41c..13b2c15dc 100644 --- a/app/views/manage/bookings/_filter_fields.html.slim +++ b/app/views/manage/bookings/_filter_fields.html.slim @@ -1,9 +1,7 @@ / h3.my-2.text-center= Booking:.model_name.human .row.mt-3 .col - = f.text_field :ref - .col - = f.text_field :tenant + = f.text_field :q .row .col = f.datetime_field :begins_at_after, include_blank: true diff --git a/config/locales/de.yml b/config/locales/de.yml index 4a07bcd28..1c7b8fdc3 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -14,8 +14,7 @@ de: occupiables: Mietobjekte only_inconcluded: nur offene Buchungen previous_booking_states: Vergangene Status - ref: Buchungsreferenz - tenant: Mieter + q: Buchungsreferenz, Mieter, Organisation invoice/filter: invoice_type: Rechungsart issued_at_after: Rechnungsdatum nach diff --git a/config/locales/en.yml b/config/locales/en.yml index f78a4c03b..830064d13 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -14,8 +14,6 @@ en: occupiables: Mietobjekte only_inconcluded: nur offene Buchungen previous_booking_states: Vergangene Status - ref: Buchungsreferenz - tenant: Mieter invoice/filter: invoice_type: Rechungsart issued_at_after: Rechnungsdatum nach diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 6296c87b0..23bd1ff85 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -14,8 +14,6 @@ fr: occupiables: Objet de location only_inconcluded: Uniquement les réservations ouvertes/libres (?) previous_booking_states: Anciens statuts - ref: "(Numéro de) Référence de location" - tenant: Locataire invoice/filter: invoice_type: Type de facture/facturation issued_at_after: Délai de paiement après le diff --git a/config/locales/it.yml b/config/locales/it.yml index 865dd7314..ba74cd6b7 100644 --- a/config/locales/it.yml +++ b/config/locales/it.yml @@ -14,8 +14,6 @@ it: occupiables: Proprietà in affitto only_inconcluded: solo prenotazioni aperte previous_booking_states: Stato passato - ref: Riferimento di prenotazione - tenant: Inquilino invoice/filter: invoice_type: Tipo di fattura issued_at_after: Data di fatturazione dopo