From f2f27c55dee8cc74a0c6dd971cc34ebd481f102e Mon Sep 17 00:00:00 2001 From: Braden MacDonald Date: Fri, 27 Sep 2024 14:26:32 -0700 Subject: [PATCH 1/3] feat: allow filtering library by publish status --- .../LibraryAuthoringPage.tsx | 2 + .../components/BaseComponentCard.tsx | 19 +++-- .../components/ComponentCard.tsx | 3 + src/search-manager/FilterByPublished.tsx | 77 +++++++++++++++++++ src/search-manager/index.ts | 1 + 5 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 src/search-manager/FilterByPublished.tsx diff --git a/src/library-authoring/LibraryAuthoringPage.tsx b/src/library-authoring/LibraryAuthoringPage.tsx index 76cef54178..73f45f62b1 100644 --- a/src/library-authoring/LibraryAuthoringPage.tsx +++ b/src/library-authoring/LibraryAuthoringPage.tsx @@ -31,6 +31,7 @@ import { ClearFiltersButton, FilterByBlockType, FilterByTags, + FilterByPublished, SearchContextProvider, SearchKeywordsField, SearchSortWidget, @@ -249,6 +250,7 @@ const LibraryAuthoringPage = ({ returnToLibrarySelection }: LibraryAuthoringPage
+
diff --git a/src/library-authoring/components/BaseComponentCard.tsx b/src/library-authoring/components/BaseComponentCard.tsx index 3b5aa748c9..1c3d137ee5 100644 --- a/src/library-authoring/components/BaseComponentCard.tsx +++ b/src/library-authoring/components/BaseComponentCard.tsx @@ -1,5 +1,6 @@ import React, { useMemo } from 'react'; import { + Badge, Card, Container, Icon, @@ -11,12 +12,14 @@ import TagCount from '../../generic/tag-count'; import { BlockTypeLabel, type ContentHitTags, Highlight } from '../../search-manager'; type BaseComponentCardProps = { - componentType: string, - displayName: string, description: string, - numChildren?: number, - tags: ContentHitTags, - actions: React.ReactNode, - openInfoSidebar: () => void + componentType: string; + displayName: string; + description: string; + numChildren?: number; + tags: ContentHitTags; + actions: React.ReactNode; + openInfoSidebar: () => void; + hasUnpublishedChanges?: boolean; }; const BaseComponentCard = ({ @@ -27,6 +30,7 @@ const BaseComponentCard = ({ tags, actions, openInfoSidebar, + ...props } : BaseComponentCardProps) => { const tagCount = useMemo(() => { if (!tags) { @@ -75,7 +79,8 @@ const BaseComponentCard = ({
- +
+ {props.hasUnpublishedChanges ? Unpublished changes : null} diff --git a/src/library-authoring/components/ComponentCard.tsx b/src/library-authoring/components/ComponentCard.tsx index c21f15467e..5c7691c5ac 100644 --- a/src/library-authoring/components/ComponentCard.tsx +++ b/src/library-authoring/components/ComponentCard.tsx @@ -189,6 +189,8 @@ const ComponentCard = ({ contentHit }: ComponentCardProps) => { formatted, tags, usageKey, + modified, + lastPublished, } = contentHit; const componentDescription: string = ( showOnlyPublished ? formatted.published?.description : formatted.description @@ -213,6 +215,7 @@ const ComponentCard = ({ contentHit }: ComponentCardProps) => { )} openInfoSidebar={() => openComponentInfoSidebar(usageKey)} + hasUnpublishedChanges={modified >= (lastPublished ?? 0)} /> ); }; diff --git a/src/search-manager/FilterByPublished.tsx b/src/search-manager/FilterByPublished.tsx new file mode 100644 index 0000000000..ad9ef662a3 --- /dev/null +++ b/src/search-manager/FilterByPublished.tsx @@ -0,0 +1,77 @@ +import React from 'react'; +import { FormattedMessage } from '@edx/frontend-platform/i18n'; +import { + Badge, + Form, + Menu, + MenuItem, +} from '@openedx/paragon'; +import { FilterList } from '@openedx/paragon/icons'; +import SearchFilterWidget from './SearchFilterWidget'; +import messages from './messages'; +// import { useSearchContext } from './SearchManager'; + +/** + * A button with a dropdown that allows filtering the current search by publish status + */ +const FilterByPublished: React.FC> = () => { + // const { + // publishedFilter, + // setPublishedFilter, + // } = useSearchContext(); + + const clearFilters = React.useCallback(() => { + // setPublishedFilter(undefined); + }, []); + + return ( + + + + + {}} + > +
+ Published + 15 +
+
+ {}} + > +
+ Modified since publish + 5 +
+
+ {}} + > +
+ Never published + 2 +
+
+
+
+
+
+ ); +}; + +export default FilterByPublished; diff --git a/src/search-manager/index.ts b/src/search-manager/index.ts index e2d4188be1..495f8c822f 100644 --- a/src/search-manager/index.ts +++ b/src/search-manager/index.ts @@ -3,6 +3,7 @@ export { default as BlockTypeLabel } from './BlockTypeLabel'; export { default as ClearFiltersButton } from './ClearFiltersButton'; export { default as FilterByBlockType } from './FilterByBlockType'; export { default as FilterByTags } from './FilterByTags'; +export { default as FilterByPublished } from './FilterByPublished'; export { default as Highlight } from './Highlight'; export { default as SearchKeywordsField } from './SearchKeywordsField'; export { default as SearchSortWidget } from './SearchSortWidget'; From f02e334d55fc748653c1b532c3baf7d4c75b4a0c Mon Sep 17 00:00:00 2001 From: Braden MacDonald Date: Fri, 18 Oct 2024 18:30:05 -0700 Subject: [PATCH 2/3] temp: more work toward working publish status filter --- src/search-manager/FilterByPublished.tsx | 42 +++++++++++++++--------- src/search-manager/SearchManager.ts | 12 ++++++- src/search-manager/data/api.ts | 19 +++++++++++ src/search-manager/data/apiHooks.ts | 5 +++ 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/search-manager/FilterByPublished.tsx b/src/search-manager/FilterByPublished.tsx index ad9ef662a3..9f369f18f7 100644 --- a/src/search-manager/FilterByPublished.tsx +++ b/src/search-manager/FilterByPublished.tsx @@ -9,19 +9,29 @@ import { import { FilterList } from '@openedx/paragon/icons'; import SearchFilterWidget from './SearchFilterWidget'; import messages from './messages'; -// import { useSearchContext } from './SearchManager'; +import { useSearchContext } from './SearchManager'; +import { PublishStatus } from './data/api'; /** * A button with a dropdown that allows filtering the current search by publish status */ const FilterByPublished: React.FC> = () => { - // const { - // publishedFilter, - // setPublishedFilter, - // } = useSearchContext(); + const { + publishedFilter, + setPublishedFilter, + } = useSearchContext(); const clearFilters = React.useCallback(() => { - // setPublishedFilter(undefined); + setPublishedFilter([]); + }, []); + + const toggleFilterMode = React.useCallback((mode: PublishStatus) => { + setPublishedFilter(oldList => { + if (oldList.includes(mode)) { + return oldList.filter(m => m !== mode); + } + return [...oldList, mode]; + }); }, []); return ( @@ -34,37 +44,37 @@ const FilterByPublished: React.FC> = () => { {}} + value={PublishStatus.Published} + onChange={() => { toggleFilterMode(PublishStatus.Published); }} >
Published - 15 + {' '}15
{}} + value={PublishStatus.Modified} + onChange={() => { toggleFilterMode(PublishStatus.Modified); }} >
Modified since publish - 5 + {' '}5
{}} + value={PublishStatus.NeverPublished} + onChange={() => { toggleFilterMode(PublishStatus.NeverPublished); }} >
Never published - 2 + {' '}2
diff --git a/src/search-manager/SearchManager.ts b/src/search-manager/SearchManager.ts index 314c90020a..bce30f47d7 100644 --- a/src/search-manager/SearchManager.ts +++ b/src/search-manager/SearchManager.ts @@ -10,7 +10,11 @@ import { MeiliSearch, type Filter } from 'meilisearch'; import { union } from 'lodash'; import { - CollectionHit, ContentHit, SearchSortOption, forceArray, + CollectionHit, + ContentHit, + SearchSortOption, + forceArray, + type PublishStatus, } from './data/api'; import { useContentSearchConnection, useContentSearchResults } from './data/apiHooks'; @@ -23,6 +27,8 @@ export interface SearchContextData { setBlockTypesFilter: React.Dispatch>; problemTypesFilter: string[]; setProblemTypesFilter: React.Dispatch>; + publishedFilter: PublishStatus[]; + setPublishedFilter: React.Dispatch>; tagsFilter: string[]; setTagsFilter: React.Dispatch>; blockTypes: Record; @@ -99,6 +105,7 @@ export const SearchContextProvider: React.FC<{ const [searchKeywords, setSearchKeywords] = React.useState(''); const [blockTypesFilter, setBlockTypesFilter] = React.useState([]); const [problemTypesFilter, setProblemTypesFilter] = React.useState([]); + const [publishedFilter, setPublishedFilter] = React.useState([]); const [tagsFilter, setTagsFilter] = React.useState([]); const [usageKey, setUsageKey] = useStateWithUrlSearchParam( '', @@ -163,6 +170,7 @@ export const SearchContextProvider: React.FC<{ searchKeywords, blockTypesFilter, problemTypesFilter, + publishedFilter, tagsFilter, sort, skipBlockTypeFetch, @@ -178,6 +186,8 @@ export const SearchContextProvider: React.FC<{ setBlockTypesFilter, problemTypesFilter, setProblemTypesFilter, + publishedFilter, + setPublishedFilter, tagsFilter, setTagsFilter, extraFilter, diff --git a/src/search-manager/data/api.ts b/src/search-manager/data/api.ts index 0763000f55..f00a675d24 100644 --- a/src/search-manager/data/api.ts +++ b/src/search-manager/data/api.ts @@ -25,6 +25,12 @@ export enum SearchSortOption { RECENTLY_MODIFIED = 'modified:desc', } +export enum PublishStatus { + Published = 'published', + Modified = 'modified', + NeverPublished = 'never', +} + /** * Get the content search configuration from the CMS. */ @@ -179,6 +185,7 @@ interface FetchSearchParams { searchKeywords: string, blockTypesFilter?: string[], problemTypesFilter?: string[], + publishedFilter?: PublishStatus[], /** The full path of tags that each result MUST have, e.g. ["Difficulty > Hard", "Subject > Math"] */ tagsFilter?: string[], extraFilter?: Filter, @@ -194,6 +201,7 @@ export async function fetchSearchResults({ searchKeywords, blockTypesFilter, problemTypesFilter, + publishedFilter, tagsFilter, extraFilter, sort, @@ -215,6 +223,16 @@ export async function fetchSearchResults({ const problemTypesFilterFormatted = problemTypesFilter?.length ? [problemTypesFilter.map(pt => `content.problem_types = ${pt}`)] : []; + /* eslint-disable */ + const publishStatusFilterFormatted = publishedFilter?.length ? publishedFilter.map(pt => ( + pt === PublishStatus.Published ? 'modified = last_published' : + pt === PublishStatus.Modified ? 'modified > last_published' : + pt === PublishStatus.NeverPublished ? 'last_published IS NULL' : + 'false' + )) : []; + console.log(publishStatusFilterFormatted) + /* eslint-enable */ + const tagsFilterFormatted = formatTagsFilter(tagsFilter); const limit = 20; // How many results to retrieve per page. @@ -235,6 +253,7 @@ export async function fetchSearchResults({ ...typeFilters, ...extraFilterFormatted, ...tagsFilterFormatted, + ...publishStatusFilterFormatted, ], attributesToHighlight: ['display_name', 'description', 'published'], highlightPreTag: HIGHLIGHT_PRE_TAG, diff --git a/src/search-manager/data/apiHooks.ts b/src/search-manager/data/apiHooks.ts index 923749b20d..1e759fb6ac 100644 --- a/src/search-manager/data/apiHooks.ts +++ b/src/search-manager/data/apiHooks.ts @@ -10,6 +10,7 @@ import { fetchTagsThatMatchKeyword, getContentSearchConfig, fetchBlockTypes, + type PublishStatus, } from './api'; /** @@ -53,6 +54,7 @@ export const useContentSearchResults = ({ searchKeywords, blockTypesFilter = [], problemTypesFilter = [], + publishedFilter = [], tagsFilter = [], sort = [], skipBlockTypeFetch = false, @@ -69,6 +71,7 @@ export const useContentSearchResults = ({ blockTypesFilter?: string[]; /** Only search for these problem types (e.g. `["choiceresponse", "multiplechoiceresponse"]`) */ problemTypesFilter?: string[]; + publishedFilter?: PublishStatus[]; /** Required tags (all must match), e.g. `["Difficulty > Hard", "Subject > Math"]` */ tagsFilter?: string[]; /** Sort search results using these options */ @@ -88,6 +91,7 @@ export const useContentSearchResults = ({ searchKeywords, blockTypesFilter, problemTypesFilter, + publishedFilter, tagsFilter, sort, ], @@ -103,6 +107,7 @@ export const useContentSearchResults = ({ searchKeywords, blockTypesFilter, problemTypesFilter, + publishedFilter, tagsFilter, sort, // For infinite pagination of results, we can retrieve additional pages if requested. From cac19a11288cc8180e8fb0fd47cc1300b806fe6a Mon Sep 17 00:00:00 2001 From: Daniel Valenzuela Date: Fri, 13 Dec 2024 18:41:50 -0300 Subject: [PATCH 3/3] feat: implement pills and filter --- src/search-manager/FilterByPublished.tsx | 19 +++++++++---------- src/search-manager/SearchManager.ts | 13 +++++++------ src/search-manager/data/api.ts | 18 ++++++------------ src/search-manager/data/apiHooks.ts | 9 +++++---- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/search-manager/FilterByPublished.tsx b/src/search-manager/FilterByPublished.tsx index 9f369f18f7..b0d7ee18ec 100644 --- a/src/search-manager/FilterByPublished.tsx +++ b/src/search-manager/FilterByPublished.tsx @@ -1,5 +1,4 @@ import React from 'react'; -import { FormattedMessage } from '@edx/frontend-platform/i18n'; import { Badge, Form, @@ -8,7 +7,6 @@ import { } from '@openedx/paragon'; import { FilterList } from '@openedx/paragon/icons'; import SearchFilterWidget from './SearchFilterWidget'; -import messages from './messages'; import { useSearchContext } from './SearchManager'; import { PublishStatus } from './data/api'; @@ -17,16 +15,17 @@ import { PublishStatus } from './data/api'; */ const FilterByPublished: React.FC> = () => { const { - publishedFilter, - setPublishedFilter, + publishStatus, + publishStatusFilter, + setPublishStatusFilter, } = useSearchContext(); const clearFilters = React.useCallback(() => { - setPublishedFilter([]); + setPublishStatusFilter([]); }, []); const toggleFilterMode = React.useCallback((mode: PublishStatus) => { - setPublishedFilter(oldList => { + setPublishStatusFilter(oldList => { if (oldList.includes(mode)) { return oldList.filter(m => m !== mode); } @@ -44,7 +43,7 @@ const FilterByPublished: React.FC> = () => { > = () => { >
Published - {' '}15 + {' '}{publishStatus[PublishStatus.Published] ?? 0}
> = () => { >
Modified since publish - {' '}5 + {' '}{publishStatus[PublishStatus.Modified] ?? 0}
> = () => { >
Never published - {' '}2 + {' '}{publishStatus[PublishStatus.NeverPublished] ?? 0}
diff --git a/src/search-manager/SearchManager.ts b/src/search-manager/SearchManager.ts index bce30f47d7..6fdf5ec744 100644 --- a/src/search-manager/SearchManager.ts +++ b/src/search-manager/SearchManager.ts @@ -27,12 +27,13 @@ export interface SearchContextData { setBlockTypesFilter: React.Dispatch>; problemTypesFilter: string[]; setProblemTypesFilter: React.Dispatch>; - publishedFilter: PublishStatus[]; - setPublishedFilter: React.Dispatch>; + publishStatusFilter: PublishStatus[]; + setPublishStatusFilter: React.Dispatch>; tagsFilter: string[]; setTagsFilter: React.Dispatch>; blockTypes: Record; problemTypes: Record; + publishStatus: Record; extraFilter?: Filter; canClearFilters: boolean; clearFilters: () => void; @@ -105,7 +106,7 @@ export const SearchContextProvider: React.FC<{ const [searchKeywords, setSearchKeywords] = React.useState(''); const [blockTypesFilter, setBlockTypesFilter] = React.useState([]); const [problemTypesFilter, setProblemTypesFilter] = React.useState([]); - const [publishedFilter, setPublishedFilter] = React.useState([]); + const [publishStatusFilter, setPublishStatusFilter] = React.useState([]); const [tagsFilter, setTagsFilter] = React.useState([]); const [usageKey, setUsageKey] = useStateWithUrlSearchParam( '', @@ -170,7 +171,7 @@ export const SearchContextProvider: React.FC<{ searchKeywords, blockTypesFilter, problemTypesFilter, - publishedFilter, + publishStatusFilter, tagsFilter, sort, skipBlockTypeFetch, @@ -186,8 +187,8 @@ export const SearchContextProvider: React.FC<{ setBlockTypesFilter, problemTypesFilter, setProblemTypesFilter, - publishedFilter, - setPublishedFilter, + publishStatusFilter, + setPublishStatusFilter, tagsFilter, setTagsFilter, extraFilter, diff --git a/src/search-manager/data/api.ts b/src/search-manager/data/api.ts index f00a675d24..f1a0f0a288 100644 --- a/src/search-manager/data/api.ts +++ b/src/search-manager/data/api.ts @@ -185,7 +185,7 @@ interface FetchSearchParams { searchKeywords: string, blockTypesFilter?: string[], problemTypesFilter?: string[], - publishedFilter?: PublishStatus[], + publishStatusFilter?: PublishStatus[], /** The full path of tags that each result MUST have, e.g. ["Difficulty > Hard", "Subject > Math"] */ tagsFilter?: string[], extraFilter?: Filter, @@ -201,7 +201,7 @@ export async function fetchSearchResults({ searchKeywords, blockTypesFilter, problemTypesFilter, - publishedFilter, + publishStatusFilter, tagsFilter, extraFilter, sort, @@ -213,6 +213,7 @@ export async function fetchSearchResults({ totalHits: number, blockTypes: Record, problemTypes: Record, + publishStatus: Record, }> { const queries: MultiSearchQuery[] = []; @@ -223,15 +224,7 @@ export async function fetchSearchResults({ const problemTypesFilterFormatted = problemTypesFilter?.length ? [problemTypesFilter.map(pt => `content.problem_types = ${pt}`)] : []; - /* eslint-disable */ - const publishStatusFilterFormatted = publishedFilter?.length ? publishedFilter.map(pt => ( - pt === PublishStatus.Published ? 'modified = last_published' : - pt === PublishStatus.Modified ? 'modified > last_published' : - pt === PublishStatus.NeverPublished ? 'last_published IS NULL' : - 'false' - )) : []; - console.log(publishStatusFilterFormatted) - /* eslint-enable */ + const publishStatusFilterFormatted = publishStatusFilter?.length ? [publishStatusFilter.map(ps => `publish_status = ${ps}`)] : []; const tagsFilterFormatted = formatTagsFilter(tagsFilter); @@ -268,7 +261,7 @@ export async function fetchSearchResults({ if (!skipBlockTypeFetch) { queries.push({ indexUid: indexName, - facets: ['block_type', 'content.problem_types'], + facets: ['block_type', 'content.problem_types', 'publish_status'], filter: [ ...extraFilterFormatted, // We exclude the block type filter here so we get all the other available options for it. @@ -285,6 +278,7 @@ export async function fetchSearchResults({ totalHits: results[0].totalHits ?? results[0].estimatedTotalHits ?? hitLength, blockTypes: results[1]?.facetDistribution?.block_type ?? {}, problemTypes: results[1]?.facetDistribution?.['content.problem_types'] ?? {}, + publishStatus: results[1]?.facetDistribution?.publish_status ?? {}, nextOffset: hitLength === limit ? offset + limit : undefined, }; } diff --git a/src/search-manager/data/apiHooks.ts b/src/search-manager/data/apiHooks.ts index 1e759fb6ac..c2fe73bf7c 100644 --- a/src/search-manager/data/apiHooks.ts +++ b/src/search-manager/data/apiHooks.ts @@ -54,7 +54,7 @@ export const useContentSearchResults = ({ searchKeywords, blockTypesFilter = [], problemTypesFilter = [], - publishedFilter = [], + publishStatusFilter = [], tagsFilter = [], sort = [], skipBlockTypeFetch = false, @@ -71,7 +71,7 @@ export const useContentSearchResults = ({ blockTypesFilter?: string[]; /** Only search for these problem types (e.g. `["choiceresponse", "multiplechoiceresponse"]`) */ problemTypesFilter?: string[]; - publishedFilter?: PublishStatus[]; + publishStatusFilter?: PublishStatus[]; /** Required tags (all must match), e.g. `["Difficulty > Hard", "Subject > Math"]` */ tagsFilter?: string[]; /** Sort search results using these options */ @@ -91,7 +91,7 @@ export const useContentSearchResults = ({ searchKeywords, blockTypesFilter, problemTypesFilter, - publishedFilter, + publishStatusFilter, tagsFilter, sort, ], @@ -107,7 +107,7 @@ export const useContentSearchResults = ({ searchKeywords, blockTypesFilter, problemTypesFilter, - publishedFilter, + publishStatusFilter, tagsFilter, sort, // For infinite pagination of results, we can retrieve additional pages if requested. @@ -133,6 +133,7 @@ export const useContentSearchResults = ({ // The distribution of block type filter options blockTypes: pages?.[0]?.blockTypes ?? {}, problemTypes: pages?.[0]?.problemTypes ?? {}, + publishStatus: pages?.[0]?.publishStatus ?? {}, status: query.status, isLoading: query.isLoading, isError: query.isError,