Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters + search to admin hackathons page #262

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/controllers/admin/hackathons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ class Admin::HackathonsController < Admin::BaseController
before_action :set_hackathon, except: :index

def index
@pagy, @hackathons = pagy(Hackathon.all.order(created_at: :desc))
@hackathons = Hackathon.all.order(created_at: :desc)

@query = params[:q]
@status = params[:status]

@hackathons = @hackathons.where("name ILIKE ?", "%#{Hackathon.sanitize_sql_like(@query)}%") if @query.present?
@hackathons = @hackathons.where(status: @status) if @status.present?

@pagy, @hackathons = pagy(@hackathons)
end

def show
Expand Down
15 changes: 15 additions & 0 deletions app/views/admin/hackathons/_filters.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="mb3 mt2">
<%= form_with method: :get, class: "simple_form mb3" do |form| %>
<%= form.hidden_field :status, value: @status if @status.present? %>

<div style="display: flex">
<%= form.text_field :q, placeholder: "Search", value: @search, style: "flex: 1", class: "mr2" %>
northeastprince marked this conversation as resolved.
Show resolved Hide resolved
<button type="submit" class="btn">Search</button>
</div>
<% end %>

<%= link_to "All", params.permit(:q).merge(status: nil), class: "mr2", style: ("color: var(--muted); text-decoration: none" unless @status.nil?) %>
<% Hackathon.statuses.keys.each do |status| %>
<%= link_to status.humanize, params.permit(:q).merge(status:), class: "mr2", style: ("color: var(--muted); text-decoration: none" unless @status == status) %>
<% end %>
</div>
7 changes: 6 additions & 1 deletion app/views/admin/hackathons/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<% @nav_active_item = admin_hackathons_path %>
<h2 class="title">Hackathons</h2>

<%= render partial: "snippet", collection: @hackathons, as: :hackathon %>
<%= render "filters" %>
<% if @hackathons.any? %>
<%= render partial: "snippet", collection: @hackathons, as: :hackathon %>
<% else %>
<p>No hackathons to show.</p>
<% end %>

<%== pagy_nav(@pagy) %>