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

feat: search filters refinement #980

Merged
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
40 changes: 36 additions & 4 deletions src/search-modal/FilterByBlockType.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,39 @@ const FilterByBlockType = () => {
blockTypesFilter,
setBlockTypesFilter,
} = useSearchContext();
// TODO: sort blockTypes first by count, then by name

// Sort blocktypes in order of hierarchy followed by alphabetically for components
const sortedBlockTypeKeys = Object.keys(blockTypes).sort((a, b) => {
const order = {
chapter: 1,
sequential: 2,
vertical: 3,
};

// If both blocktypes are in the order dictionary, sort them based on the order defined
if (order[a] && order[b]) {
return order[a] - order[b];
}

// If only blocktype 'a' is in the order dictionary, place it before 'b'
if (order[a]) {
return -1;
}

// If only blocktype 'b' is in the order dictionary, place it before 'a'
if (order[b]) {
return 1;
}

// If neither blocktype is in the order dictionary, sort alphabetically
return a.localeCompare(b);
});

// Rebuild sorted blocktypes dictionary
const sortedBlockTypes = {};
sortedBlockTypeKeys.forEach(key => {
sortedBlockTypes[key] = blockTypes[key];
});

const handleCheckboxChange = React.useCallback((e) => {
setBlockTypesFilter(currentFilters => {
Expand All @@ -46,9 +78,9 @@ const FilterByBlockType = () => {
name="block-type-filter"
defaultValue={blockTypesFilter}
>
<Menu style={{ boxShadow: 'none' }}>
<Menu className="block-type-refinement-menu" style={{ boxShadow: 'none' }}>
{
Object.entries(blockTypes).map(([blockType, count]) => (
Object.entries(sortedBlockTypes).map(([blockType, count]) => (
<MenuItem
key={blockType}
as={Form.Checkbox}
Expand All @@ -63,7 +95,7 @@ const FilterByBlockType = () => {
}
{
// Show a message if there are no options at all to avoid the impression that the dropdown isn't working
blockTypes.length === 0 ? (
sortedBlockTypes.length === 0 ? (
<MenuItem disabled><FormattedMessage {...messages['blockTypeFilter.empty']} /></MenuItem>
) : null
}
Expand Down
8 changes: 8 additions & 0 deletions src/search-modal/SearchModal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
}
}

// Options for the "filter by block type" menu
.pgn__menu.block-type-refinement-menu {
.pgn__menu-item {
// Make the "filter by block type" menu expand to fit longer block types names
width: 100%;
}
}

.pgn__menu-item {
// Fix a bug in Paragon menu dropdowns: the checkbox currently shrinks if the text is too long.
// https://github.com/openedx/paragon/pull/3019
Expand Down
1 change: 1 addition & 0 deletions src/search-modal/__mocks__/search-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
"estimatedTotalHits": 0,
"facetDistribution": {
"block_type": {
"chapter": 1,
"html": 2,
"problem": 16,
"vertical": 2,
Expand Down
Loading