-
Notifications
You must be signed in to change notification settings - Fork 92
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: allow filtering library by publish status #1570
Changes from all commits
fd2c357
7cf5d2e
8889017
6d3db8d
73ef429
3f0f95e
2effa32
09d1284
54913f7
777fe25
f28faaa
fd35ce8
419ac2f
dc3f657
daa98b8
19f9420
a338fe8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import React from 'react'; | ||
import { | ||
Badge, | ||
Form, | ||
Menu, | ||
MenuItem, | ||
} from '@openedx/paragon'; | ||
import { FilterList } from '@openedx/paragon/icons'; | ||
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n'; | ||
import messages from './messages'; | ||
import SearchFilterWidget from './SearchFilterWidget'; | ||
import { useSearchContext } from './SearchManager'; | ||
import { PublishStatus, SearchSortOption } from './data/api'; | ||
|
||
/** | ||
* A button with a dropdown that allows filtering the current search by publish status | ||
*/ | ||
const FilterByPublished: React.FC<Record<never, never>> = () => { | ||
const [onlyPublished, setOnlyPublished] = React.useState(false); | ||
const intl = useIntl(); | ||
const { | ||
publishStatus, | ||
publishStatusFilter, | ||
setPublishStatusFilter, | ||
searchSortOrder, | ||
} = useSearchContext(); | ||
|
||
const clearFilters = React.useCallback(() => { | ||
setPublishStatusFilter([]); | ||
}, []); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Selecting a Published Status filter doesn't show the "clear filters" link? Need to modify |
||
|
||
React.useEffect(() => { | ||
if (searchSortOrder === SearchSortOption.RECENTLY_PUBLISHED) { | ||
setPublishStatusFilter([PublishStatus.Published, PublishStatus.Modified]); | ||
setOnlyPublished(true); | ||
} else { | ||
setOnlyPublished(false); | ||
} | ||
}, [searchSortOrder]); | ||
|
||
const toggleFilterMode = React.useCallback((mode: PublishStatus) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Selecting one publish_status filter clears all the others? Maybe see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was because I forgot to add the setter to the dependencies. this was somehow breaking the tags filter tests so thank you so much for finding this :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for working out a simple fix! it's working great now. |
||
setPublishStatusFilter(oldList => { | ||
if (oldList.includes(mode)) { | ||
return oldList.filter(m => m !== mode); | ||
} | ||
return [...oldList, mode]; | ||
}); | ||
}, [setPublishStatusFilter]); | ||
const modeToLabel = { | ||
published: intl.formatMessage(messages.publishStatusPublished), | ||
modified: intl.formatMessage(messages.publishStatusModified), | ||
never: intl.formatMessage(messages.publishStatusNeverPublished), | ||
}; | ||
const appliedFilters = publishStatusFilter.map(mode => ({ label: modeToLabel[mode] })); | ||
|
||
return ( | ||
<SearchFilterWidget | ||
appliedFilters={appliedFilters} | ||
label={<FormattedMessage {...messages.publishStatusFilter} />} | ||
clearFilter={clearFilters} | ||
icon={FilterList} | ||
> | ||
<Form.Group className="mb-0"> | ||
<Form.CheckboxSet | ||
name="publish-status-filter" | ||
value={publishStatusFilter} | ||
> | ||
<Menu className="block-type-refinement-menu" style={{ boxShadow: 'none' }}> | ||
<MenuItem | ||
as={Form.Checkbox} | ||
value={PublishStatus.Published} | ||
onChange={() => { toggleFilterMode(PublishStatus.Published); }} | ||
> | ||
<div> | ||
{intl.formatMessage(messages.publishStatusPublished)} | ||
<Badge variant="light" pill>{publishStatus[PublishStatus.Published] ?? 0}</Badge> | ||
</div> | ||
</MenuItem> | ||
<MenuItem | ||
as={Form.Checkbox} | ||
value={PublishStatus.Modified} | ||
onChange={() => { toggleFilterMode(PublishStatus.Modified); }} | ||
> | ||
<div> | ||
{intl.formatMessage(messages.publishStatusModified)} | ||
<Badge variant="light" pill>{publishStatus[PublishStatus.Modified] ?? 0}</Badge> | ||
</div> | ||
</MenuItem> | ||
<MenuItem | ||
as={Form.Checkbox} | ||
value={PublishStatus.NeverPublished} | ||
onChange={() => { toggleFilterMode(PublishStatus.NeverPublished); }} | ||
disabled={onlyPublished} | ||
> | ||
<div> | ||
{intl.formatMessage(messages.publishStatusNeverPublished)} | ||
<Badge variant="light" pill>{publishStatus[PublishStatus.NeverPublished] ?? 0}</Badge> | ||
</div> | ||
</MenuItem> | ||
</Menu> | ||
</Form.CheckboxSet> | ||
</Form.Group> | ||
</SearchFilterWidget> | ||
); | ||
}; | ||
|
||
export default FilterByPublished; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add
<FilterByPublished />
to the LibraryCollectionPage too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do