Skip to content

Commit

Permalink
add 'title' and 'user' ezfilters - implmenents #652
Browse files Browse the repository at this point in the history
  • Loading branch information
dcmorse committed Jul 4, 2020
1 parent 9035d67 commit 6e3254d
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/javascript/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export type Filter =
| ["if", Filter, Filter]
| ["if", Filter, Filter, Filter]
| ["all", Filter]
| ["title", string]
| ["choreographer", string]
| ["user", string]
| ["hook", string]
| ["count", Filter, Comparison, number]
| ["compare", NumericFilter, Comparison, NumericFilter]
Expand Down
24 changes: 20 additions & 4 deletions app/javascript/filters-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,37 @@ export const FiltersTab = ({

return (
<div>
<h4>Title:</h4>
<input
type="text"
className="ez-title-filter ez-text-filter form-control"
value={d.title}
onChange={e => d.setTitle(e.target.value)}
title="the name of the dance - optional"
/>
<br />
<h4>Choreographer:</h4>
<input
type="text"
className="ez-choreographer-filter form-control"
style={{ maxWidth: "15em" }}
className="ez-choreographer-filter ez-text-filter form-control"
value={d.choreographer}
onChange={e => d.setChoreographer(e.target.value)}
title="the person who wrote the dance - optional"
/>
<br />
<h4>User:</h4>
<input
type="text"
className="ez-user-filter ez-text-filter form-control"
value={d.user}
onChange={e => d.setUser(e.target.value)}
title="the person who transcribed the dance to contradb - optional"
/>
<br />
<h4>Hook:</h4>
<input
type="text"
className="ez-hook-filter form-control"
style={{ maxWidth: "15em" }}
className="ez-hook-filter ez-text-filter form-control"
value={d.hook}
onChange={e => d.setHook(e.target.value)}
title="search for words in reason the dance is interesting - optional"
Expand Down
28 changes: 27 additions & 1 deletion app/javascript/use-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const setterName = (s: string): string => {
}

interface FilterState {
title: string
choreographer: string
user: string
hook: string
verifiedChecked: boolean
notVerifiedChecked: boolean
Expand All @@ -32,7 +34,9 @@ interface FilterState {
}

const defaultFilterState: FilterState = {
title: "",
choreographer: "",
user: "",
hook: "",
verifiedChecked: true,
notVerifiedChecked: false,
Expand Down Expand Up @@ -81,6 +85,13 @@ export default function useFilter(
]
)

const debouncedTitle = useDebounce(d.title)

const titleFilters: Filter[] = useMemo(
() => (debouncedTitle ? [["title", debouncedTitle]] : []),
[debouncedTitle]
)

const debouncedChoreographer = useDebounce(d.choreographer)

const choreographerFilters: Filter[] = useMemo(
Expand All @@ -89,6 +100,13 @@ export default function useFilter(
[debouncedChoreographer]
)

const debouncedUser = useDebounce(d.user)

const userFilters: Filter[] = useMemo(
() => (debouncedUser ? [["user", debouncedUser]] : []),
[debouncedUser]
)

const debouncedHook = useDebounce(d.hook)

const hookFilters: Filter[] = useMemo(
Expand Down Expand Up @@ -121,12 +139,20 @@ export default function useFilter(
"and",
verifiedFilter,
publishFilter,
...[...choreographerFilters, ...hookFilters, ...formationFilters], // ts being grumpy!
...[
...titleFilters,
...choreographerFilters,
...userFilters,
...hookFilters,
...formationFilters,
], // ts being grumpy!
],
[
verifiedFilter,
publishFilter,
titleFilters,
choreographerFilters,
userFilters,
hookFilters,
formationFilters,
]
Expand Down
10 changes: 10 additions & 0 deletions lib/filter_dances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,21 @@ def self.matching_figures_for_then(filter, dance, filter_env)
going_concerns
end

def self.matching_figures_for_title(filter, dance, filter_env)
title = filter[1].downcase
dance.title.downcase.include?(title) ? Set[] : nil
end

def self.matching_figures_for_choreographer(filter, dance, filter_env)
choreographer = filter[1].downcase
dance.choreographer.name.downcase.include?(choreographer) ? Set[] : nil
end

def self.matching_figures_for_user(filter, dance, filter_env)
user = filter[1].downcase
dance.user.name.downcase.include?(user) ? Set[] : nil
end

def self.matching_figures_for_hook(filter, dance, filter_env)
hook = filter[1].downcase
JSLibFigure.string_in_dialect(dance.hook, filter_env.dialect).downcase.include?(hook) ? Set[] : nil
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/programs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
get :new, params: {}
expect(assigns(:program)).to be_a_new(Program)
json = JSON.parse(assigns(:dance_autocomplete_hash_json))
expect(json.map {|d| d['title']}).to eq(dances.select{|d| d.searchable?(sketchbook: true)}.map(&:title))
expect(json.map {|d| d['title']}).to contain_exactly(*dances.select{|d| d.searchable?(sketchbook: true)}.map(&:title))
end
end

Expand All @@ -68,7 +68,7 @@
get :edit, params: {:id => program.to_param}
expect(assigns(:program)).to eq(program)
json = JSON.parse(assigns(:dance_autocomplete_hash_json))
expect(json.map {|d| d['title']}).to eq(dances.select{|d| d.searchable?(sketchbook: true)}.map(&:title))
expect(json.map {|d| d['title']}).to contain_exactly(*dances.select{|d| d.searchable?(sketchbook: true)}.map(&:title))
end

it "If not logged in as program owner, refuses to show page" do
Expand Down
36 changes: 34 additions & 2 deletions spec/features/welcome/advanced_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,54 @@ def turn_page_button_css(label, disabled=false)
describe "ez-filters" do
let(:user) { FactoryGirl.create(:user) }

describe "choreographer" do
describe "title" do
let(:dances) { [:dance, :box_the_gnat_contra, :call_me].map {|name| FactoryGirl.create(name)} }

it "works" do
call_me = dances.last
dont_call_me = dances[0, dances.length-2]
tag_all_dances
visit(search_path)
with_filters_excursion { find('.ez-choreographer-filter').fill_in(with: call_me.choreographer.name)}
with_filters_excursion { find('.ez-title-filter').fill_in(with: call_me.title)}
dont_call_me.each do |dance|
expect(page).to_not have_content(dance.title)
end
expect(page).to have_content(call_me.title)
end
end

describe "choreographer" do
let(:dances) { [:dance, :box_the_gnat_contra, :call_me].map {|name| FactoryGirl.create(name)} }

it "works" do
call_me = dances.last
dont_call_me = dances[0, dances.length-2]
tag_all_dances
visit(search_path)
with_filters_excursion { find('.ez-choreographer-filter').fill_in(with: call_me.choreographer.name)}
dont_call_me.each do |dance|
expect(page).to_not have_content(dance.choreographer.name)
end
expect(page).to have_content(call_me.choreographer.name)
end
end

describe "user" do
let(:dances) { [:dance, :box_the_gnat_contra, :call_me].map {|name| FactoryGirl.create(name)} }

it "works" do
call_me = dances.last
dont_call_me = dances[0, dances.length-2]
tag_all_dances
visit(search_path)
with_filters_excursion { find('.ez-user-filter').fill_in(with: call_me.user.name)}
dont_call_me.each do |dance|
expect(page).to_not have_content(dance.user.name)
end
expect(page).to have_content(call_me.user.name)
end
end

describe "hook" do
it "works on the word 'easy'" do
easy_dance = FactoryGirl.create(:dance, hook: "so easy-peazy", title: "Mr. Toad's Wild Ride")
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/filter_dances_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,27 @@ def filtered_titles(comparison, number)
end
end

describe 'title' do
it "works case-insensitively" do
filtered = FilterDances.filter_dances(['user', dances.first.user.name.upcase], dialect: dialect)
expect(titles(filtered)).to eq([dances.first.title])
end
end

describe 'choreographer' do
it "works case-insensitively" do
filtered = FilterDances.filter_dances(['choreographer', dances.first.choreographer.name.upcase], dialect: dialect)
expect(titles(filtered)).to eq([dances.first.title])
end
end

describe 'user' do
it "works case-insensitively" do
filtered = FilterDances.filter_dances(['user', dances.first.user.name.upcase], dialect: dialect)
expect(titles(filtered)).to eq([dances.first.title])
end
end

describe 'hook' do
it "works case-insensitively" do
expect(dances.second.hook).to eq("1111111111 hook")
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es6", "dom"],
"lib": ["es6", "es2019.array", "dom"],
"module": "es6",
"moduleResolution": "node",
"sourceMap": true,
Expand Down

0 comments on commit 6e3254d

Please sign in to comment.