Skip to content

Commit

Permalink
Add filters
Browse files Browse the repository at this point in the history
  • Loading branch information
SelwynAng committed Jul 28, 2024
1 parent 0992311 commit 3447908
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ class Admin::ProductsController < AdminController

# GET /admin/products or /admin/products.json
def index
@admin_products_pagy, @admin_products = pagy(Product.all)
if params[:query].present?
@admin_products_pagy, @admin_products = pagy(Product.where("name LIKE ?", "%#{params[:query]}%"))
else
@admin_products_pagy, @admin_products = pagy(Product.all)
end
end

# GET /admin/products/1 or /admin/products/1.json
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@ class CategoriesController < ApplicationController
def show
@category = Category.find(params[:id])
@products = @category.products

if params[:max].present?
@products = @products.where("price <= ?", params[:max])
end
if params[:min].present?
@products = @products.where("price >= ?", params[:min])
end

end
end
10 changes: 10 additions & 0 deletions app/views/admin/products/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
<%= link_to "New product", new_admin_product_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>

<div class="flex mt-4 flex-col md:flex-row items-start md:items-center justify-betwee w-full">
<%= form_with url: admin_products_path, method: :get, class: "flex" do |form| %>
<%= form.text_field :query, placeholder: "Search", class: "rounded border-gray-500 text-gray-800" %>
<%= form.submit "Submit", class: "cursor-pointer bg-gray-500 ml-2 hover:bg-gray-600 text-white py-2 px-4 rounded" %>
<% end %>
<%= form_with url: admin_products_path, method: :get, class: 'flex' do |form| %>
<%= form.submit "Clear", class: "cursor-pointer bg-gray-500 ml-0 md:ml-2 mt-2 md:mt-0 hover:bg-gray-600 text-white py-2 px-4 rounded" %>
<% end %>
</div>

<div class="overflow-x-auto sm:mx-0.5 lg:mx-0.5">
<div class="py-2 inline-block min-w-full">
<div class="overflow-x-auto">
Expand Down
11 changes: 11 additions & 0 deletions app/views/categories/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<h1>Category Show Page</h1>
<div class="min-h-screen flex justify-center">
<div class="flex flex-wrap flex-col mt-6">
<h2 class="text-lg text-gray-800">Filter</h2>
<%= form_with url: category_path(@category), method: :get, class: "flex flex-col" do |form| %>
<%= form.number_field :min, placeholder: "Min Price", class: "border border-gray-800 rounded-md p-2 w-32" %>
<%= form.number_field :max, placeholder: "Max Price", class: "border border-gray-800 rounded-md p-2 mt-2 w-32" %>
<%= form.submit "Filter", class: "bg-gray-800 text-white rounded-md p-2 mt-2 cursor-pointer" %>
<% end %>
<%= form_with url: category_path(@category), method: :get, class: "flex flex-col" do |form| %>
<%= form.submit "Clear", class: "bg-gray-800 text-white rounded-md p-2 mt-2 cursor-pointer" %>
<% end %>
</div>
<div class="flex flex-wrap mt-6 gap-10 flex-grow justify-center items-start">
<% if @products.empty? %>
<p class="text-2xl text-left text-gray-800">No products found</p>
Expand Down

0 comments on commit 3447908

Please sign in to comment.