Skip to content

Commit

Permalink
Merge pull request #69 from solver-it-sro/GO-223/default_box_selection
Browse files Browse the repository at this point in the history
 single-box-tenants solution for boxselection
  • Loading branch information
stage-rl authored Sep 29, 2023
2 parents c8ae445 + 8c1b2ed commit 26ea8c0
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 55 deletions.
24 changes: 10 additions & 14 deletions app/components/layout/box_list_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<%= tag.turbo_frame id:'box-list', class:"flex-col self-stretch" do %>
<% @boxes.each do |box| %>
<%= link_to select_box_path(box), class:"flex justify-between items-center self-stretch flex-grow-0 flex-shrink-0 px-4 py-2", data: { turbo_frame: "_top" } do %>
<div class="flex justify-start items-center flex-grow-0 flex-shrink-0 relative gap-3">
<div class="justify-start w-16">
<%= render Common::BoxLabelComponent.new(box) %>
</div>
<p class="flex-grow-0 flex-shrink-0 text-base font-medium text-left text-gray-900"><%= box.name %></p>
</div>
<div class="flex justify-center items-center flex-grow-0 flex-shrink-0 relative overflow-hidden gap-1.5 px-1.5 py-0.5 rounded-md bg-gray-50 border border-gray-300">
<p class="flex-grow-0 flex-shrink-0 text-sm text-left text-gray-600"><%= box.messages.where(read: false).size %></p>
</div>
<% end %>
<% end %>
<%= link_to select_box_path(@box), class:"flex justify-between items-center self-stretch flex-grow-0 flex-shrink-0 px-4 py-2", data: { turbo_frame: "_top" } do %>
<div class="flex justify-start items-center flex-grow-0 flex-shrink-0 relative gap-3">
<div class="justify-start w-16">
<%= render Common::BoxLabelComponent.new(@box) %>
</div>
<p class="flex-grow-0 flex-shrink-0 text-base font-medium text-left text-gray-900"><%= @box.name %></p>
</div>
<div class="flex justify-center items-center flex-grow-0 flex-shrink-0 relative overflow-hidden gap-1.5 px-1.5 py-0.5 rounded-md bg-gray-50 border border-gray-300">
<p class="flex-grow-0 flex-shrink-0 text-sm text-left text-gray-600"><%= @unread_messages %></p>
</div>
<% end %>
10 changes: 6 additions & 4 deletions app/components/layout/box_list_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Layout::BoxListComponent < ViewComponent::Base
def initialize(boxes)
@boxes = boxes
end
end
with_collection_parameter :box
def initialize(box:)
@box = box
@unread_messages = Pundit.policy_scope(Current.user, Message).joins(thread: { folder: :box }).where(box: { id: @box.id}, read: false).size
end
end
4 changes: 2 additions & 2 deletions app/components/layout/box_selector_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
data-dropdown-leave-timeout="75"
>
<div id="dropdown-button" data-action="click->dropdown#toggle click@window->dropdown#hide" role="button" data-dropdown-target="button" tabindex="0" class="inline-block select-none">
<button type="button" class="flex items-center gap-2" aria-expanded="false" aria-haspopup="true">
<button type="button" class="flex items-center gap-2" <%= "disabled=true" if @disabled %> aria-expanded="false" aria-haspopup="true">
<div class="flex justify-start items-center relative gap-3 py-1.5 pl-8 rounded-md bg-white">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" class=" w-5 h-5 relative" preserveAspectRatio="xMidYMid meet">
<path d="M15.8333 9.16667H4.16667M15.8333 9.16667C16.2754 9.16667 16.6993 9.34226 17.0118 9.65482C17.3244 9.96738 17.5 10.3913 17.5 10.8333V15.8333C17.5 16.2754 17.3244 16.6993 17.0118 17.0118C16.6993 17.3244 16.2754 17.5 15.8333 17.5H4.16667C3.72464 17.5 3.30072 17.3244 2.98816 17.0118C2.67559 16.6993 2.5 16.2754 2.5 15.8333V10.8333C2.5 10.3913 2.67559 9.96738 2.98816 9.65482C3.30072 9.34226 3.72464 9.16667 4.16667 9.16667M15.8333 9.16667V7.5C15.8333 7.05797 15.6577 6.63405 15.3452 6.32149C15.0326 6.00893 14.6087 5.83333 14.1667 5.83333M4.16667 9.16667V7.5C4.16667 7.05797 4.34226 6.63405 4.65482 6.32149C4.96738 6.00893 5.39131 5.83333 5.83333 5.83333M14.1667 5.83333V4.16667C14.1667 3.72464 13.9911 3.30072 13.6785 2.98816C13.366 2.67559 12.942 2.5 12.5 2.5H7.5C7.05797 2.5 6.63405 2.67559 6.32149 2.98816C6.00893 3.30072 5.83333 3.72464 5.83333 4.16667V5.83333M14.1667 5.83333H5.83333" stroke="#9CA3AF" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
Expand All @@ -24,6 +24,6 @@
</button>
</div>
<div data-dropdown-target="menu" class="z-50 absolute transform transition hidden opacity-0 scale-95" >
<%= render Layout::BoxSelectorPopupComponent.new %>
<%= tag.turbo_frame id: 'box-selector-popup', loading: :lazy, src: get_selector_boxes_path %>
</div>
</div>
3 changes: 3 additions & 0 deletions app/components/layout/box_selector_component.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Layout::BoxSelectorComponent < ViewComponent::Base
def initialize(current_tenant_boxes_count)
@disabled = current_tenant_boxes_count == 1
end
end
52 changes: 28 additions & 24 deletions app/components/layout/box_selector_popup_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
<div class="flex flex-col justify-start items-start w-[360px] gap-px py-0.5 rounded-lg bg-white border border-gray-300" style="box-shadow: 1px 1px 4px 0 rgba(0,0,0,0.1);">
<%= link_to select_all_boxes_path, class:"flex justify-between items-center self-stretch flex-grow-0 flex-shrink-0 px-4 py-2" do %>
<div class="flex justify-center items-center flex-grow-0 flex-shrink-0 relative overflow-hidden gap-[29px]">
<p class="flex-grow-0 flex-shrink-0 text-base font-medium text-left text-gray-900">Všetky schránky</p>
</div>
<div class="flex justify-center items-center flex-grow-0 flex-shrink-0 relative overflow-hidden gap-1.5 px-1.5 py-0.5 rounded-md bg-gray-50 border border-gray-300">
<p class="flex-grow-0 flex-shrink-0 text-sm text-left text-gray-600"><%= @all_unread_messages&.count %></p>
</div>
<% end %>
<div class="flex justify-start items-center self-stretch flex-grow-0 flex-shrink-0 relative">
<svg width="360" height="1" viewBox="0 0 360 1" fill="none" xmlns="http://www.w3.org/2000/svg" class="flex-grow" preserveAspectRatio="xMidYMid meet">
<line y1="0.5" x2="360" y2="0.5" stroke="#E5E7EB"></line>
</svg>
</div>
<%= form_with url: search_boxes_path, class:"flex justify-start items-center self-stretch flex-grow-0 flex-shrink-0 gap-2 p-4 border-t-0 border-r-0 border-b border-l-0 border-gray-200" do |form| %>
<div class="flex w-full items-center rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-indigo-600">
<div class="p-2">
<%= render Icons::SearchComponent.new %>
<%= tag.turbo_frame id:'box-selector-popup' do %>
<div class="flex flex-col justify-start items-start w-[360px] gap-px py-0.5 rounded-lg bg-white border border-gray-300" style="box-shadow: 1px 1px 4px 0 rgba(0,0,0,0.1);">
<%= link_to select_all_boxes_path, class:"flex justify-between items-center self-stretch flex-grow-0 flex-shrink-0 px-4 py-2" do %>
<div class="flex justify-center items-center flex-grow-0 flex-shrink-0 relative overflow-hidden gap-[29px]">
<p class="flex-grow-0 flex-shrink-0 text-base font-medium text-left text-gray-900">Všetky schránky</p>
</div>
<div class="flex justify-center items-center flex-grow-0 flex-shrink-0 relative overflow-hidden gap-1.5 px-1.5 py-0.5 rounded-md bg-gray-50 border border-gray-300">
<p class="flex-grow-0 flex-shrink-0 text-sm text-left text-gray-600"><%= @all_unread_messages_count %></p>
</div>
<%= tag.turbo_frame id:'box-search-results' %>
<%= form.search_field :name_search, value: params[:name_search], placeholder: "Vyhľadaj schránku",
<% end %>
<div class="flex justify-start items-center self-stretch flex-grow-0 flex-shrink-0 relative">
<svg width="360" height="1" viewBox="0 0 360 1" fill="none" xmlns="http://www.w3.org/2000/svg" class="flex-grow" preserveAspectRatio="xMidYMid meet">
<line y1="0.5" x2="360" y2="0.5" stroke="#E5E7EB"></line>
</svg>
</div>
<%= form_with url: search_boxes_path, class:"flex justify-start items-center self-stretch flex-grow-0 flex-shrink-0 gap-2 p-4 border-t-0 border-r-0 border-b border-l-0 border-gray-200" do |form| %>
<div class="flex w-full items-center rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-indigo-600">
<div class="p-2">
<%= render Icons::SearchComponent.new %>
</div>
<%= tag.turbo_frame id:'box-search-results' %>
<%= form.search_field :name_search, value: params[:name_search], placeholder: "Vyhľadaj schránku",
oninput: "this.form.requestSubmit()",
onreset: "this.form.requestSubmit()",
class: "block w-full flex-1 border-0 bg-transparent py-1.5 pl-1 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6" %>
</div>
<% end %>
<%= render Layout::BoxListComponent.new(@boxes) %>
</div>
</div>
<% end %>
<%= tag.turbo_frame id:'box-list', class:"flex-col self-stretch" do %>
<%= render Layout::BoxListComponent.with_collection(@boxes) %>
<% end %>
</div>
<% end %>
10 changes: 5 additions & 5 deletions app/components/layout/box_selector_popup_component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Layout::BoxSelectorPopupComponent < ViewComponent::Base
def initialize
@boxes = Current.tenant.boxes.where.not(boxes: { id: nil }) || []
@all_unread_messages = Pundit.policy_scope(Current.user, Message).where(read:false) if Current.user
end
end
def initialize(boxes, all_unread_messages_count)
@boxes = boxes
@all_unread_messages_count = all_unread_messages_count
end
end
2 changes: 1 addition & 1 deletion app/components/t_w/top_navigation_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="flex justify-stretch items-center grow h-16 relative gap-4 px-4 bg-white border-t-0 border-r-0 border-b border-l-0 border-gray-200">
<%= render Layout::BoxSelectorComponent.new %>
<%= render Layout::BoxSelectorComponent.new(@current_tenant_boxes_count) %>
<svg width="1" height="64" viewBox="0 0 1 64" fill="none" xmlns="http://www.w3.org/2000/svg" class="todo " preserveAspectRatio="xMidYMid meet">
<line x1="0.5" y1="2.18557e-8" x2="0.499997" y2="64" stroke="#E5E7EB"></line>
</svg>
Expand Down
3 changes: 3 additions & 0 deletions app/components/t_w/top_navigation_component.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class TW::TopNavigationComponent < ViewComponent::Base
def initialize(current_tenant_boxes_count)
@current_tenant_boxes_count = current_tenant_boxes_count
end
end
7 changes: 5 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def get_menu
end

def set_menu_context
@tags = policy_scope(Tag, policy_scope_class: TagPolicy::Scope).where(visible: true)
@menu = SidebarMenu.new(controller_name, action_name, { tags: @tags })
if Current.user
@tags = policy_scope(Tag, policy_scope_class: TagPolicy::Scope).where(visible: true)
@menu = SidebarMenu.new(controller_name, action_name, { tags: @tags })
@current_tenant_boxes_count = Current.tenant.boxes.count
end
end
end
7 changes: 6 additions & 1 deletion app/controllers/boxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def select

def select_all
authorize Box
# TODO: Chceme to takto? nil = vsetky Alebo chceme pridavat inu variablu pre tento stav?
session[:box_id] = nil
redirect_to request.referrer
end
Expand All @@ -37,6 +36,12 @@ def search
.order(:name)
end

def get_selector
authorize(Box)
@boxes = Current.tenant.boxes
@all_unread_messages_count = Pundit.policy_scope(Current.user, Message).joins(thread: { folder: :box }).where(box: { tenant_id: Current.tenant.id }, read: false).count
end

private

def load_box
Expand Down
1 change: 1 addition & 0 deletions app/controllers/concerns/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def create_session
session[:login_expires_at] = SESSION_TIMEOUT.from_now
session[:tenant_id] = Current.user.tenant_id
session[:user_profile_picture_url] = auth_hash.info.image
session[:box_id] = Current.user.tenant.boxes.first.id if Current.user.tenant.boxes.one?
redirect_to session[:after_login_path] || default_after_login_path
else
render html: 'Not authorized', status: :forbidden
Expand Down
4 changes: 4 additions & 0 deletions app/policies/box_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ def search?
true
end

def get_selector?
true
end

end
1 change: 1 addition & 0 deletions app/views/boxes/get_selector.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render Layout::BoxSelectorPopupComponent.new(@boxes, @all_unread_messages_count) %>
2 changes: 1 addition & 1 deletion app/views/boxes/search.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= turbo_stream.update 'box-list' do %>
<%= render Layout::BoxListComponent.new(@boxes) %>
<%= render Layout::BoxListComponent.with_collection(@boxes) %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<%= render TW::StaticSidebarComponent.new %>
<% end %>
<% component.with_top_navigation do %>
<%= render TW::TopNavigationComponent.new %>
<%= render TW::TopNavigationComponent.new(@current_tenant_boxes_count) %>
<% end %>
<% component.with_main_content do %>
<%= yield %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
post :sync
get :select, on: :member
get :select_all, on: :collection
get :get_selector, on: :collection
post :search, on: :collection
end

Expand Down

0 comments on commit 26ea8c0

Please sign in to comment.