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

Filter definitions by definition_group #15

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
diver_down (0.0.1.alpha1)
diver_down (0.0.1.alpha2)
activesupport (>= 7.0.0)
rack-contrib (>= 2.3.0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type SearchDefinitionsOptions = {
title: string
source: string
folding: boolean
definitionGroup: string
}

type Props = {
Expand Down Expand Up @@ -38,6 +39,13 @@ export const ConfigureSearchOptionsDialog: React.FC<Props> = ({
onClickClose()
}

const onChangeDefinitionGroup = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
setTemporarySearchDefinitionsOptions((prev) => ({ ...prev, definitionGroup: event.target.value }))
},
[setTemporarySearchDefinitionsOptions],
)

const onChangeTitle = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
setTemporarySearchDefinitionsOptions((prev) => ({ ...prev, title: event.target.value }))
Expand Down Expand Up @@ -77,6 +85,15 @@ export const ConfigureSearchOptionsDialog: React.FC<Props> = ({
<p>Configure settings related to the display of definitions.</p>

<Stack gap={1.5}>
<FormControl title="Filtering definition group" helpMessage="Refine the definition with a definition group">
<Input
name="definitionGroup"
type="text"
onChange={onChangeDefinitionGroup}
value={temporarySearchDefinitionsOptions.definitionGroup}
/>
</FormControl>

<FormControl title="Filtering title" helpMessage="Refine the definition with a title">
<Input name="title" type="text" onChange={onChangeTitle} value={temporarySearchDefinitionsOptions.title} />
</FormControl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ type DialogType = 'configureSearchOptionsDiaglog'
export const DefinitionList: FC<Props> = ({ selectedDefinitionIds, setSelectedDefinitionIds }) => {
const [visibleDialog, setVisibleDialog] = useState<DialogType | null>(null)
const [searchDefinitionsOptions, setSearchDefinitionsOptions] = useLocalStorage<SearchDefinitionsOptions>(
'Home-DefinitionList-SearchDefinitionOptions',
{ title: '', source: '', folding: false },
'Home-DefinitionList-SearchDefinitionOptions-v1',
{ definitionGroup: '', title: '', source: '', folding: false },
)
const [foldingSection, setFoldingSection] = useState<boolean>(false)

const { isLoading, definitions, isValidating, setSize, isReachingEnd } = useDefinitionList({
definitionGroup: searchDefinitionsOptions.definitionGroup,
title: searchDefinitionsOptions.title,
source: searchDefinitionsOptions.source,
})
Expand Down
9 changes: 7 additions & 2 deletions frontend/repositories/definitionListRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ type DefinitionsResponse = {

export const PER = 100

export const useDefinitionList = (query: { title: string; source: string }, keepPreviousData: boolean = false) => {
export const useDefinitionList = (
query: { definitionGroup: string; title: string; source: string },
keepPreviousData: boolean = false,
) => {
const getKey = (pageIndex: number, previousPageData: DefinitionReponse[] | null) => {
if (previousPageData && previousPageData.length === 0) {
return null
}
const params = {
per: PER,
page: pageIndex + 1,
...query,
definition_group: query.definitionGroup,
title: query.title,
source: query.source,
}

return `${path.api.definitions.index()}?${stringify(params)}`
Expand Down
3 changes: 2 additions & 1 deletion lib/diver_down/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def call(env)
page: request.params['page']&.to_i || 1,
per: request.params['per']&.to_i || 100,
title: request.params['title'] || '',
source: request.params['source'] || ''
source: request.params['source'] || '',
definition_group: request.params['definition_group'] || ''
)
in ['GET', %r{\A/api/sources\.json\z}]
action.sources
Expand Down
5 changes: 3 additions & 2 deletions lib/diver_down/web/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ def module(module_names)
# @param per [Integer]
# @param title [String]
# @param source [String]
def definitions(page:, per:, title:, source:)
definition_enumerator = DiverDown::Web::DefinitionEnumerator.new(@store, title:, source:)
# @param definition_group [String]
def definitions(page:, per:, title:, source:, definition_group:)
definition_enumerator = DiverDown::Web::DefinitionEnumerator.new(@store, title:, source:, definition_group:)
definitions, pagination = paginate(definition_enumerator, page, per)

json(
Expand Down
8 changes: 7 additions & 1 deletion lib/diver_down/web/definition_enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class DefinitionEnumerator

# @param store [DiverDown::Definition::Store]
# @param query [String]
def initialize(store, title: '', source: '')
# @param definition_group [String]
def initialize(store, title: '', source: '', definition_group: '')
@store = store
@title = title
@source = source
@definition_group = definition_group
end

# @yield [parent_bit_id, bit_id, definition]
Expand Down Expand Up @@ -47,6 +49,10 @@ def match_definition?(definition)
matched &&= definition.sources.any? { _1.source_name.include?(@source) }
end

unless @definition_group.empty?
matched &&= definition.definition_group.to_s.include?(@definition_group)
end

matched
end
end
Expand Down
39 changes: 39 additions & 0 deletions spec/diver_down/web/definition_enumerator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,45 @@
)
end

describe 'with definition_group' do
def assert_query(store, definition_group, expected)
actual = described_class.new(store, definition_group:).each.to_a
expect(actual).to eq(expected), -> {
"definition_group: #{definition_group.inspect}\n" \
"expected: #{expected.inspect}\n" \
"actual: #{actual.inspect}"
}
end

it 'filters by definition_group' do
store = DiverDown::Web::DefinitionStore.new

definition_1 = DiverDown::Definition.new(
definition_group: 'group_1',
sources: [
DiverDown::Definition::Source.new(
source_name: 'a.rb'
),
]
)
definition_2 = DiverDown::Definition.new(
definition_group: 'group_2',
sources: [
DiverDown::Definition::Source.new(
source_name: 'b.rb'
),
]
)

store.set(definition_1, definition_2)

assert_query store, 'unknown', []
assert_query store, 'group', [definition_1, definition_2]
assert_query store, 'group_1', [definition_1]
assert_query store, 'group_2', [definition_2]
end
end

describe 'with title' do
def assert_query(store, title, expected)
actual = described_class.new(store, title:).each.to_a
Expand Down
41 changes: 41 additions & 0 deletions spec/diver_down/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,47 @@ def assert_source(source, expected_ids)
assert_source 'b.', [definition_2_id]
end
end

describe 'definition_group' do
def assert_definition_group(definition_group, expected_ids)
get "/api/definitions.json?definition_group=#{definition_group}"

definitions = JSON.parse(last_response.body)['definitions']
ids = definitions.map { _1['id'] }

expect(ids).to match_array(expected_ids), -> {
"definition_group: #{definition_group.inspect}\n" \
"expected_ids: #{expected_ids.inspect}\n" \
"actual_ids: #{ids.inspect}"
}
end

it 'filters definitions by definition_group=value' do
definition_1 = DiverDown::Definition.new(
definition_group: 'group_1',
sources: [
DiverDown::Definition::Source.new(
source_name: 'a.rb'
),
]
)
definition_2 = DiverDown::Definition.new(
definition_group: 'group_2',
sources: [
DiverDown::Definition::Source.new(
source_name: 'b.rb'
),
]
)

definition_1_id, definition_2_id = store.set(definition_1, definition_2)

assert_definition_group 'unknown', []
assert_definition_group 'group', [definition_1_id, definition_2_id]
assert_definition_group 'group_1', [definition_1_id]
assert_definition_group 'group_2', [definition_2_id]
end
end
end

describe 'GET /api/initialization_status.json' do
Expand Down