Skip to content

Commit

Permalink
Keeping filters state on pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
GPrimola committed Oct 4, 2020
1 parent 2cd6d56 commit 4623f16
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
28 changes: 22 additions & 6 deletions app/controllers/restrooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class RestroomsController < ApplicationController
respond_to :html, :json

before_action :restrooms_filters, only: [:index]
before_action :list_restrooms, only: [:index]
before_action :find_restroom, only: [:show, :update, :edit, :destroy]

Expand Down Expand Up @@ -74,13 +75,28 @@ def update
end

private
def restrooms_filters
@filters =
params
.fetch(:filters, '')
.split(',')
.reduce({}) do |filters, filter|
filters[filter] = true if ['accessible', 'changing_table', 'unisex'].include?(filter)

filters
end
end

def list_restrooms
@restrooms = Restroom.current.page(params[:page])
@restrooms = if params[:search].present? || params[:map] == "1"
@restrooms.near([params[:lat], params[:long]], 20, :order => 'distance')
else
@restrooms.reverse_order
end
@restrooms = Restroom.current.where(@filters).page(params[:page])
@restrooms =
if params[:search].present? || params[:map] == "1"
@restrooms.near([params[:lat], params[:long]], 20, :order => 'distance')
else
@restrooms.reverse_order
end

@restrooms = @restrooms.out_of_range? ? @restrooms.page(1) : @restrooms
end

def display_errors
Expand Down
47 changes: 41 additions & 6 deletions app/javascript/packs/views/restrooms/restrooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,62 @@
// You can use CoffeeScript in this file: http://coffeescript.org/

$(() => {
function getSearchParams() {
const { search: search } = location;
return new URLSearchParams(search);
}
function applyFilters(filters) {
const { origin: url, pathname: path } = location;
const searchParams = getSearchParams();
searchParams.set('filters', filters);
location = `${url}${path}?${searchParams.toString()}`
}

function getFilters() {
const searchParams = getSearchParams();
const filters = searchParams.get('filters') || '';
return filters.split(',').filter(filter => filter && filter.length > 0);
}

function removeFilter(toRemove) {
const filters =
getFilters()
.filter((filter) => filter != toRemove);

applyFilters(filters);
}

function addFilter(filter) {
let filters = getFilters();

if (filters.indexOf(filter) == -1) {
filters.push(filter);
}

applyFilters(filters);
}

$('#ada_filter').click(function() {
if ($(this).hasClass("active")) {
$('.listItem.not_accessible').show();
removeFilter('accessible');
} else {
$('.listItem.not_accessible').hide();
addFilter('accessible');
}
});

$('#unisex_filter').click(function() {
if ($(this).hasClass("active")) {
$('.listItem.not_unisex').show();
removeFilter('unisex');
} else {
$('.listItem.not_unisex').hide();
addFilter('unisex');
}
});

$('#changing_table_filter').click(function() {
if ($(this).hasClass("active")) {
$('.listItem.no_changing_table').show();
removeFilter('changing_table');
} else {
$('.listItem.no_changing_table').hide();
addFilter('changing_table');
}
});
});
12 changes: 6 additions & 6 deletions app/views/restrooms/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
.col-xs-12.headroom
#filters.clearfix
.btn-group{:data => {:toggle => "buttons"}}
%label#ada_filter.filter.btn.btn-light-purple.linkbutton{:title => t("restroom.accessible")}
%input{:type => "checkbox"}
%label#ada_filter.filter.btn.btn-light-purple.linkbutton{:title => t("restroom.accessible"), :class => ("active" if @filters.keys.include?('accessible'))}
%input{:type => "checkbox", :checked => @filters.keys.include?('accessible')}
%i.fa.fa-wheelchair
%label#unisex_filter.filter.btn-light-purple.btn.linkbutton{:title => t("restroom.type.unisex")}
%input{:type => "checkbox"}
%label#unisex_filter.filter.btn-light-purple.btn.linkbutton{:title => t("restroom.type.unisex"), :class => ("active" if @filters.keys.include?('unisex'))}
%input{:type => "checkbox", :checked => @filters.keys.include?('unisex')}
%i.fa.fa-transgender-alt
%label#changing_table_filter.filter.btn-light-purple.btn.linkbutton{:title => t("restroom.changing_table")}
%input{:type => "checkbox"}
%label#changing_table_filter.filter.btn-light-purple.btn.linkbutton{:title => t("restroom.changing_table"), :class => ("active" if @filters.keys.include?('changing_table'))}
%input{:type => "checkbox", :checked => @filters.keys.include?('changing_table')}
%i.fa.fa-child

.map-toggle-btn.mapToggle.linkbutton.btn-lg.btn-light-purple
Expand Down

0 comments on commit 4623f16

Please sign in to comment.