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: allow filtering library by publish status #1570

Merged
merged 17 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
43 changes: 43 additions & 0 deletions src/library-authoring/LibraryAuthoringPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,49 @@ describe('<LibraryAuthoringPage />', () => {
});
});

it('filters by publish status', async () => {
await renderLibraryPage();

// Open the publish status filter dropdown
const filterButton = screen.getByRole('button', { name: /publish status/i });
fireEvent.click(filterButton);

// Test each publish status filter option
const publishedCheckbox = screen.getByRole('checkbox', { name: /^published \d+$/i });
const modifiedCheckbox = screen.getByRole('checkbox', { name: /^modified since publish \d+$/i });
const neverPublishedCheckbox = screen.getByRole('checkbox', { name: /^never published \d+$/i });

// Test Published filter
fireEvent.click(publishedCheckbox);
await waitFor(() => {
expect(fetchMock).toHaveBeenLastCalledWith(searchEndpoint, {
body: expect.stringContaining('"publish_status = published"'),
method: 'POST',
headers: expect.anything(),
});
});

// Test Modified filter
fireEvent.click(modifiedCheckbox);
await waitFor(() => {
expect(fetchMock).toHaveBeenLastCalledWith(searchEndpoint, {
body: expect.stringContaining('"publish_status = modified"'),
method: 'POST',
headers: expect.anything(),
});
});

// Test Never Published filter
fireEvent.click(neverPublishedCheckbox);
await waitFor(() => {
expect(fetchMock).toHaveBeenLastCalledWith(searchEndpoint, {
body: expect.stringContaining('"publish_status = never"'),
method: 'POST',
headers: expect.anything(),
});
});
});

it('Shows an error if libraries V2 is disabled', async () => {
const { axiosMock } = initializeMocks();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, {
Expand Down
2 changes: 2 additions & 0 deletions src/library-authoring/LibraryAuthoringPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ClearFiltersButton,
FilterByBlockType,
FilterByTags,
FilterByPublished,
SearchContextProvider,
SearchKeywordsField,
SearchSortWidget,
Expand Down Expand Up @@ -254,6 +255,7 @@ const LibraryAuthoringPage = ({ returnToLibrarySelection }: LibraryAuthoringPage
<div className="d-flex mt-3 align-items-center">
<FilterByTags />
<FilterByBlockType />
<FilterByPublished />
<ClearFiltersButton />
<div className="flex-grow-1" />
<SearchSortWidget />
Expand Down
19 changes: 12 additions & 7 deletions src/library-authoring/components/BaseComponentCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useMemo } from 'react';
import {
Badge,
Card,
Container,
Icon,
Expand All @@ -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 = ({
Expand All @@ -27,6 +30,7 @@ const BaseComponentCard = ({
tags,
actions,
openInfoSidebar,
...props
} : BaseComponentCardProps) => {
const tagCount = useMemo(() => {
if (!tags) {
Expand Down Expand Up @@ -75,7 +79,8 @@ const BaseComponentCard = ({
<div className="text-truncate h3 mt-2">
<Highlight text={displayName} />
</div>
<Highlight text={description} />
<Highlight text={description} /><br />
{props.hasUnpublishedChanges ? <Badge variant="warning">Unpublished changes</Badge> : null}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract "Unpublished changes" into a message so it can be translated.

</Card.Section>
</Card.Body>
</Card>
Expand Down
3 changes: 3 additions & 0 deletions src/library-authoring/components/ComponentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ const ComponentCard = ({ contentHit }: ComponentCardProps) => {
formatted,
tags,
usageKey,
modified,
lastPublished,
} = contentHit;
const componentDescription: string = (
showOnlyPublished ? formatted.published?.description : formatted.description
Expand All @@ -216,6 +218,7 @@ const ComponentCard = ({ contentHit }: ComponentCardProps) => {
</ActionRow>
)}
openInfoSidebar={() => openComponentInfoSidebar(usageKey)}
hasUnpublishedChanges={modified >= (lastPublished ?? 0)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Why not pull this from the added published_status field? I'm ok either way, but it seems like we shouldn't duplicate functionality here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh nice catch

/>
);
};
Expand Down
86 changes: 86 additions & 0 deletions src/search-manager/FilterByPublished.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React from 'react';
import {
Badge,
Form,
Menu,
MenuItem,
} from '@openedx/paragon';
import { FilterList } from '@openedx/paragon/icons';
import SearchFilterWidget from './SearchFilterWidget';
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<Record<never, never>> = () => {
const {
publishStatus,
publishStatusFilter,
setPublishStatusFilter,
} = useSearchContext();

const clearFilters = React.useCallback(() => {
setPublishStatusFilter([]);

Check warning on line 24 in src/search-manager/FilterByPublished.tsx

View check run for this annotation

Codecov / codecov/patch

src/search-manager/FilterByPublished.tsx#L24

Added line #L24 was not covered by tests
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 canClearFilters.


const toggleFilterMode = React.useCallback((mode: PublishStatus) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Selecting one publish_status filter clears all the others? Maybe see FilterByBlockType for how its checkbox onChange handler works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 :)

Copy link
Contributor

Choose a reason for hiding this comment

The 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);

Check warning on line 30 in src/search-manager/FilterByPublished.tsx

View check run for this annotation

Codecov / codecov/patch

src/search-manager/FilterByPublished.tsx#L30

Added line #L30 was not covered by tests
}
return [...oldList, mode];
});
}, []);

return (
<SearchFilterWidget
appliedFilters={[]}
label="Publish Status"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract "Publish Status" to a message so it can be translated.

clearFilter={clearFilters}
icon={FilterList}
>
<Form.Group className="mb-0">
<Form.CheckboxSet
name="block-type-filter"
value={publishStatusFilter}
>
<Menu className="block-type-refinement-menu" style={{ boxShadow: 'none' }}>
<MenuItem
as={Form.Checkbox}
value={PublishStatus.Published}
onChange={() => { toggleFilterMode(PublishStatus.Published); }}
>
<div>
Published
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract these "Published", "Modified since publish", and "Never published" labels to messages so they can be translated.

{' '}<Badge variant="light" pill>{publishStatus[PublishStatus.Published] ?? 0}</Badge>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to have this {' '} in there to separate the label from the Badge, do you? FilterByTags doesn't: https://github.com/open-craft/frontend-app-authoring/blob/dvz/filter-by-published/src/search-manager/FilterByTags.tsx#L66-L67

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(And strictly speaking, the Badge should be part of the translated text so that RTL languages can put the count badge to the left of the text. But there's a number of places where we'd need to fix this, so it's ok if you don't do it here.)

</div>
</MenuItem>
<MenuItem
as={Form.Checkbox}
value={PublishStatus.Modified}
onChange={() => { toggleFilterMode(PublishStatus.Modified); }}
>
<div>
Modified since publish
{' '}<Badge variant="light" pill>{publishStatus[PublishStatus.Modified] ?? 0}</Badge>
</div>
</MenuItem>
<MenuItem
as={Form.Checkbox}
value={PublishStatus.NeverPublished}
onChange={() => { toggleFilterMode(PublishStatus.NeverPublished); }}
>
<div>
Never published
{' '}<Badge variant="light" pill>{publishStatus[PublishStatus.NeverPublished] ?? 0}</Badge>
</div>
</MenuItem>
</Menu>
</Form.CheckboxSet>
</Form.Group>
</SearchFilterWidget>
);
};

export default FilterByPublished;
13 changes: 12 additions & 1 deletion src/search-manager/SearchManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -23,10 +27,13 @@ export interface SearchContextData {
setBlockTypesFilter: React.Dispatch<React.SetStateAction<string[]>>;
problemTypesFilter: string[];
setProblemTypesFilter: React.Dispatch<React.SetStateAction<string[]>>;
publishStatusFilter: PublishStatus[];
setPublishStatusFilter: React.Dispatch<React.SetStateAction<PublishStatus[]>>;
tagsFilter: string[];
setTagsFilter: React.Dispatch<React.SetStateAction<string[]>>;
blockTypes: Record<string, number>;
problemTypes: Record<string, number>;
publishStatus: Record<string, number>;
extraFilter?: Filter;
canClearFilters: boolean;
clearFilters: () => void;
Expand Down Expand Up @@ -99,6 +106,7 @@ export const SearchContextProvider: React.FC<{
const [searchKeywords, setSearchKeywords] = React.useState('');
const [blockTypesFilter, setBlockTypesFilter] = React.useState<string[]>([]);
const [problemTypesFilter, setProblemTypesFilter] = React.useState<string[]>([]);
const [publishStatusFilter, setPublishStatusFilter] = React.useState<PublishStatus[]>([]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you resolve the conflicts for this file, please note that all of the search state variables are now managed as URL parameters, so this status should be too. Should look something like this:

  const [publishStatusFilter, setPublishStatusFilter] = useStateOrUrlSearchParam<PublishStatus>(
    [],
    'published',
    (value: string) => Object.values(PublishStatus).find((enumValue) => value === enumValue),
    (value: PublishStatus) => value.toString(),
    skipUrlUpdate,
  );

References:

const [tagsFilter, setTagsFilter] = React.useState<string[]>([]);
const [usageKey, setUsageKey] = useStateWithUrlSearchParam(
'',
Expand Down Expand Up @@ -163,6 +171,7 @@ export const SearchContextProvider: React.FC<{
searchKeywords,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some UI questions, feel free to bounce these off the UX folks if you agree :)

  1. Currently, sorting by "Recently Published" also filters out any "Never published" items, but this is entirely hidden from the user.
    Should we make this explicit force-selecting the "Published" and "Modified since publish" filters in the UI?
  2. Should the Publish Status drop-down label reveal when some filters are selected, like the Type and Tags do?
    image
    To make the drop-down more concise, I'd use "Status: Published" or whatever instead of "Published status: Published".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmakowski1123 @lizc577 @sdaitzman @marcotuts any comments on this? ^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmakowski1123 @lizc577 @sdaitzman @marcotuts gentle nudge on this since we're blocked waiting for a decision here.

blockTypesFilter,
problemTypesFilter,
publishStatusFilter,
tagsFilter,
sort,
skipBlockTypeFetch,
Expand All @@ -178,6 +187,8 @@ export const SearchContextProvider: React.FC<{
setBlockTypesFilter,
problemTypesFilter,
setProblemTypesFilter,
publishStatusFilter,
setPublishStatusFilter,
tagsFilter,
setTagsFilter,
extraFilter,
Expand Down
15 changes: 14 additions & 1 deletion src/search-manager/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -179,6 +185,7 @@ interface FetchSearchParams {
searchKeywords: string,
blockTypesFilter?: string[],
problemTypesFilter?: string[],
publishStatusFilter?: PublishStatus[],
/** The full path of tags that each result MUST have, e.g. ["Difficulty > Hard", "Subject > Math"] */
tagsFilter?: string[],
extraFilter?: Filter,
Expand All @@ -194,6 +201,7 @@ export async function fetchSearchResults({
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishStatusFilter,
tagsFilter,
extraFilter,
sort,
Expand All @@ -205,6 +213,7 @@ export async function fetchSearchResults({
totalHits: number,
blockTypes: Record<string, number>,
problemTypes: Record<string, number>,
publishStatus: Record<string, number>,
}> {
const queries: MultiSearchQuery[] = [];

Expand All @@ -215,6 +224,8 @@ export async function fetchSearchResults({

const problemTypesFilterFormatted = problemTypesFilter?.length ? [problemTypesFilter.map(pt => `content.problem_types = ${pt}`)] : [];

const publishStatusFilterFormatted = publishStatusFilter?.length ? [publishStatusFilter.map(ps => `publish_status = ${ps}`)] : [];

const tagsFilterFormatted = formatTagsFilter(tagsFilter);

const limit = 20; // How many results to retrieve per page.
Expand All @@ -235,6 +246,7 @@ export async function fetchSearchResults({
...typeFilters,
...extraFilterFormatted,
...tagsFilterFormatted,
...publishStatusFilterFormatted,
],
attributesToHighlight: ['display_name', 'description', 'published'],
highlightPreTag: HIGHLIGHT_PRE_TAG,
Expand All @@ -249,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.
Expand All @@ -266,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,
};
}
Expand Down
6 changes: 6 additions & 0 deletions src/search-manager/data/apiHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
fetchTagsThatMatchKeyword,
getContentSearchConfig,
fetchBlockTypes,
type PublishStatus,
} from './api';

/**
Expand Down Expand Up @@ -53,6 +54,7 @@ export const useContentSearchResults = ({
searchKeywords,
blockTypesFilter = [],
problemTypesFilter = [],
publishStatusFilter = [],
tagsFilter = [],
sort = [],
skipBlockTypeFetch = false,
Expand All @@ -69,6 +71,7 @@ export const useContentSearchResults = ({
blockTypesFilter?: string[];
/** Only search for these problem types (e.g. `["choiceresponse", "multiplechoiceresponse"]`) */
problemTypesFilter?: string[];
publishStatusFilter?: PublishStatus[];
/** Required tags (all must match), e.g. `["Difficulty > Hard", "Subject > Math"]` */
tagsFilter?: string[];
/** Sort search results using these options */
Expand All @@ -88,6 +91,7 @@ export const useContentSearchResults = ({
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishStatusFilter,
tagsFilter,
sort,
],
Expand All @@ -103,6 +107,7 @@ export const useContentSearchResults = ({
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishStatusFilter,
tagsFilter,
sort,
// For infinite pagination of results, we can retrieve additional pages if requested.
Expand All @@ -128,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,
Expand Down
1 change: 1 addition & 0 deletions src/search-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading