Skip to content

Commit

Permalink
Pass filter via url params and tweak message threads table title base…
Browse files Browse the repository at this point in the history
…d on filter or query
  • Loading branch information
alhafoudh committed Dec 11, 2023
1 parent 7a3f004 commit 473af6d
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/components/layout/filter_list_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<p class="text-sm text-center text-gray-400">Filtre</p>
</div>
<% @filters.each do |filter| %>
<% url = message_threads_path(q: filter.query) %>
<% url = filtered_message_threads_path(filter:) %>
<%= link_to url, class: "text-gray-700 hover:text-indigo-600 hover:bg-gray-50 group w-72 flex gap-x-3 rounded-md p-2 px-4 text-sm leading-6 font-semibold data-[active=true]:bg-gray-50 data-[active=true]:text-indigo-600", data: { active: current_page?(url) } do %>
<%= render Icons::BookmarkComponent.new %>
<p class="truncate text-base font-medium"><%= filter.name %></p>
Expand Down
2 changes: 2 additions & 0 deletions app/components/layout/filter_list_component.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Layout::FilterListComponent < ViewComponent::Base
include MessageThreadHelper

def initialize(filters:)
@filters = filters
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= tag.turbo_frame id: "bulk_actions" do %>
<div class="flex justify-stretch items-center gap-4 px-4 lg:py-4 border-t-0 border-r-0 border-b border-l-0 border-gray-200 min-h-[3rem] sm:min-h-[5rem]">
<%= check_box_tag("", nil, false, { class: "hidden sm:block h-4 w-4 rounded border-gray-300 text-blue-500 focus:ring-0", type: "checkbox", id: "checkbox-all", data: { action: "all-checkboxes#toggle", "all-checkboxes-target": "checkbox" } }) %>
<span class="grow sm:text-xl text-base font-semibold text-left text-gray-900"><%= @ids.present? ? t(:selected_message, count: @ids.count) : "Správy v schránke" %></span>
<span class="grow sm:text-xl text-base font-semibold text-left text-gray-900"><%= title %></span>
<% if @ids.present? %>
<%= form_with url: bulk_merge_message_threads_path, data: { turbo_frame: "_top" } do %>
<% @ids.each do |id| %>
Expand Down
12 changes: 11 additions & 1 deletion app/components/message_threads_bulk_actions_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
class MessageThreadsBulkActionsComponent < ViewComponent::Base
def initialize(ids:)
def initialize(ids: nil, filter: nil, query: nil)
@ids = ids
@filter = filter
@query = query
end

def title
return t(:selected_message, count: @ids.count) if @ids.present?
return "Správy z filtra '#{@filter.name}'" if @filter.present?
return "Správy vyhovujúce hľadanému výrazu '#{@query}'" if @query.present?

"Správy v schránke"
end
end
2 changes: 1 addition & 1 deletion app/components/message_threads_table_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="flex flex-col justify-stretch items-stretch gap-4 sm:p-4">
<div class="flex flex-col justify-stretch items-stretch sm:rounded-md bg-white sm:border sm:border-gray-200" data-controller="form all-checkboxes">
<%= render MessageThreadsBulkActionsComponent.new(ids: []) %>
<%= render MessageThreadsBulkActionsComponent.new(ids: [], filter:, query:) %>
<%= form_with url: bulk_actions_message_threads_path, data: { "form-target": "form", "all-checkboxes-target": "form" } do %>
<ul role="list" id="message_threads" class="divide-y divide-gray-100">
<% message_threads.each do |message_thread| %>
Expand Down
7 changes: 7 additions & 0 deletions app/components/message_threads_table_component.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
class MessageThreadsTableComponent < ViewComponent::Base
renders_many :message_threads
renders_one :next_page_area

attr_reader :filter, :query

def initialize(filter: nil, query: nil)
@filter = filter
@query = query
end
end
6 changes: 3 additions & 3 deletions app/components/t_w/top_navigation_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<div class="flex justify-start items-stretch grow relative gap-3 rounded-md bg-white">
<%= form_with url: message_threads_path, method: :get, html: { class: 'relative flex flex-1' } do |f| %>
<%= render Icons::MagnifyingGlassComponent.gray %>
<%= f.search_field :q, id: "search", value: params[:q], placeholder: 'Vyhľadaj správu', class: 'block h-full w-full border-0 py-0 pr-0 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-base' %>
<%= f.search_field :q, id: "search", value: query, placeholder: 'Vyhľadaj správu', class: 'block h-full w-full border-0 py-0 pr-0 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-base' %>
<% end %>
<% if params[:q].present? %>
<%= link_to new_filter_path(query: params[:q]), data: { turbo_frame: "modal" }, class: "flex items-center gap-2 text-gray-500 hover:text-gray-900", title: "Pridať filter" do %>
<% if query.present? %>
<%= link_to new_filter_path(query:), data: { turbo_frame: "modal" }, class: "flex items-center gap-2 text-gray-500 hover:text-gray-900", title: "Pridať filter" do %>
<%= render Icons::BookmarkComponent.new %>
<% end %>
<% end %>
Expand Down
13 changes: 13 additions & 0 deletions app/components/t_w/top_navigation_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
class TW::TopNavigationComponent < ViewComponent::Base
include MessageThreadHelper

def initialize(current_tenant_boxes_count)
@current_tenant_boxes_count = current_tenant_boxes_count
end

def query
query_params = params
.permit(:filter_id, :q)
.slice(:filter_id, :q)

Searchable::QueryBuilder.new(
filter_id: query_params[:filter_id],
query: query_params[:q],
).build
end
end
4 changes: 2 additions & 2 deletions app/controllers/filters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def create
if @filter.save
flash[:notice] = 'Filter bol úspešne vytvorený'
if params[:to] == 'search'
redirect_to message_threads_path(q: @filter.query)
redirect_to helpers.filtered_message_threads_path(filter: @filter)
else
redirect_to filters_path
end
else
if params[:to] == 'search'
redirect_to message_threads_path(q: @filter.query), alert: 'Filter sa nepodarilo vytvoriť :('
redirect_to helpers.filtered_message_threads_path(query: @filter.query), alert: 'Filter sa nepodarilo vytvoriť :('
else
render :new
end
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/message_threads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def update

def index
authorize MessageThread

@filter = policy_scope(Filter, policy_scope_class: FilterPolicy::ScopeShowable).find_by(id: params[:filter_id]) if params[:filter_id].present?
@query = search_params[:q]
end

def scroll
Expand Down Expand Up @@ -62,13 +65,18 @@ def merge_threads(message_thread_ids)
end

def load_threads
query = Searchable::QueryBuilder.new(
filter_id: params[:filter_id],
query: search_params[:q],
).build

cursor = MessageThreadCollection.init_cursor(search_params[:cursor])

result =
MessageThreadCollection.all(
scope: message_thread_policy_scope.includes(:tags, :box),
search_permissions: search_permissions,
query: search_params[:q],
query:,
cursor: cursor
)

Expand Down
10 changes: 10 additions & 0 deletions app/helpers/message_thread_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module MessageThreadHelper
def filtered_message_threads_path(filter: nil, query: nil)
args = {
filter_id: filter&.id,
q: query
}.compact

message_threads_path(args)
end
end
24 changes: 24 additions & 0 deletions app/models/searchable/query_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Searchable::QueryBuilder
attr_reader :filter, :filter_id, :query, :user

def initialize(filter: nil, filter_id: nil, query: nil, user: nil)
@filter = filter
@filter_id = filter_id
@query = query
@user = user
end

def build
if filter.nil? && filter_id.present?
@filter = FilterPolicy::ScopeShowable.new(user, Filter).resolve.find_by(id: filter_id)
end

[
filter&.query,
query,
]
.compact
.map(&:strip)
.join(' ')
end
end
2 changes: 1 addition & 1 deletion app/views/message_threads/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= render TW::FlashComponent.new %>
<%= render MessageThreadsTableComponent.new do |component| %>
<%= render MessageThreadsTableComponent.new(filter: @filter, query: @query) do |component| %>
<% component.with_message_thread do %>
<%= render MessageThreadsTableRowComponent.with_collection(@message_threads) %>
<% end %>
Expand Down

0 comments on commit 473af6d

Please sign in to comment.