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

fix: disable filter by block on collections tab [FC-0076] #1576

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 src/library-authoring/LibraryAuthoringPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ const LibraryAuthoringPage = ({ returnToLibrarySelection }: LibraryAuthoringPage
<ActionRow className="my-3">
<SearchKeywordsField className="mr-3" />
<FilterByTags />
<FilterByBlockType />
<FilterByBlockType disabled={activeKey === ContentType.collections} />
<ClearFiltersButton />
<ActionRow.Spacer />
<SearchSortWidget />
Expand Down
34 changes: 26 additions & 8 deletions src/search-manager/FilterByBlockType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,16 @@ const FilterItem = ({ blockType, count } : FilterItemProps) => {
);
};

interface FilterByBlockTypeProps {
disabled?: boolean,
}
/**
* A button with a dropdown that allows filtering the current search by component type (XBlock type)
* e.g. Limit results to "Text" (html) and "Problem" (problem) components.
* The button displays the first type selected, and a count of how many other types are selected, if more than one.
* @param disabled - If true, the filter is disabled and hidden.
*/
const FilterByBlockType: React.FC<Record<never, never>> = () => {
const FilterByBlockType: React.FC<FilterByBlockTypeProps> = ({ disabled = false }) => {
const {
blockTypes,
blockTypesFilter,
Expand All @@ -225,6 +229,22 @@ const FilterByBlockType: React.FC<Record<never, never>> = () => {
setProblemTypesFilter([]);
}, []);

useEffect(() => {
if (disabled) {
// Clear filters when disabled
const selectedBlockTypesFilter = blockTypesFilter;
const selectedProblemTypesFilter = problemTypesFilter;
clearFilters();

return () => {
// Restore filters when re-enabled
setBlockTypesFilter(selectedBlockTypesFilter);
setProblemTypesFilter(selectedProblemTypesFilter);
};
}
return () => {};
}, [disabled]);

// Sort blocktypes in order of hierarchy followed by alphabetically for components
const sortedBlockTypeKeys = Object.keys(blockTypes).sort((a, b) => {
const order = {
Expand Down Expand Up @@ -262,7 +282,9 @@ const FilterByBlockType: React.FC<Record<never, never>> = () => {
blockType => ({ label: <BlockTypeLabel blockType={blockType} /> }),
);

const hiddenBlockTypes = blockTypesFilter.filter(blockType => !Object.keys(blockTypes).includes(blockType));
if (disabled) {
return null;
}

return (
<SearchFilterWidget
Expand All @@ -277,20 +299,16 @@ const FilterByBlockType: React.FC<Record<never, never>> = () => {
value={blockTypesFilter}
>
<Menu className="block-type-refinement-menu" style={{ boxShadow: 'none' }}>
{
// Show applied filter items for block types that are not in the current search results
hiddenBlockTypes.map(blockType => <FilterItem key={blockType} blockType={blockType} count={0} />)
}
{
Object.entries(sortedBlockTypes).map(([blockType, count]) => (
<FilterItem key={blockType} blockType={blockType} count={count} />
))
}
{
// Show a message if there are no options at all to avoid the impression that the dropdown isn't working
Object.keys(sortedBlockTypes).length === 0 && hiddenBlockTypes.length === 0 ? (
Object.keys(sortedBlockTypes).length === 0 && (
<MenuItem disabled><FormattedMessage {...messages['blockTypeFilter.empty']} /></MenuItem>
) : null
)
}
</Menu>
</Form.CheckboxSet>
Expand Down
Loading