Skip to content

Commit

Permalink
temp: more work toward working publish status filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Oct 19, 2024
1 parent a1b1a47 commit a68529c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
42 changes: 26 additions & 16 deletions src/search-manager/FilterByPublished.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ import {
import { FilterList } from '@openedx/paragon/icons';
import SearchFilterWidget from './SearchFilterWidget';
import messages from './messages';

Check failure on line 11 in src/search-manager/FilterByPublished.tsx

View workflow job for this annotation

GitHub Actions / tests (20)

'messages' is defined but never used
// 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<Record<never, never>> = () => {
// 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 (
Expand All @@ -34,37 +44,37 @@ const FilterByPublished: React.FC<Record<never, never>> = () => {
<Form.Group className="mb-0">
<Form.CheckboxSet
name="block-type-filter"
value={[]}
value={publishedFilter}
>
<Menu className="block-type-refinement-menu" style={{ boxShadow: 'none' }}>
<MenuItem
as={Form.Checkbox}
value={1}
onChange={() => {}}
value={PublishStatus.Published}
onChange={() => { toggleFilterMode(PublishStatus.Published); }}
>
<div>
Published
<Badge variant="light" pill>15</Badge>
{' '}<Badge variant="light" pill>15</Badge>
</div>
</MenuItem>
<MenuItem
as={Form.Checkbox}
value={2}
onChange={() => {}}
value={PublishStatus.Modified}
onChange={() => { toggleFilterMode(PublishStatus.Modified); }}
>
<div>
Modified since publish
<Badge variant="light" pill>5</Badge>
{' '}<Badge variant="light" pill>5</Badge>
</div>
</MenuItem>
<MenuItem
as={Form.Checkbox}
value={3}
onChange={() => {}}
value={PublishStatus.NeverPublished}
onChange={() => { toggleFilterMode(PublishStatus.NeverPublished); }}
>
<div>
Never published
<Badge variant="light" pill>2</Badge>
{' '}<Badge variant="light" pill>2</Badge>
</div>
</MenuItem>
</Menu>
Expand Down
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,12 @@ import { MeiliSearch, type Filter } from 'meilisearch';
import { union } from 'lodash';

import {
CollectionHit, ContentHit, SearchSortOption, forceArray, OverrideQueries,
CollectionHit,
ContentHit,
SearchSortOption,
forceArray,
OverrideQueries,
type PublishStatus,
} from './data/api';
import { useContentSearchConnection, useContentSearchResults } from './data/apiHooks';

Expand All @@ -23,6 +28,8 @@ export interface SearchContextData {
setBlockTypesFilter: React.Dispatch<React.SetStateAction<string[]>>;
problemTypesFilter: string[];
setProblemTypesFilter: React.Dispatch<React.SetStateAction<string[]>>;
publishedFilter: PublishStatus[];
setPublishedFilter: React.Dispatch<React.SetStateAction<PublishStatus[]>>;
tagsFilter: string[];
setTagsFilter: React.Dispatch<React.SetStateAction<string[]>>;
blockTypes: Record<string, number>;
Expand Down Expand Up @@ -100,6 +107,7 @@ export const SearchContextProvider: React.FC<{
const [searchKeywords, setSearchKeywords] = React.useState('');
const [blockTypesFilter, setBlockTypesFilter] = React.useState<string[]>([]);
const [problemTypesFilter, setProblemTypesFilter] = React.useState<string[]>([]);
const [publishedFilter, setPublishedFilter] = React.useState<PublishStatus[]>([]);
const [tagsFilter, setTagsFilter] = React.useState<string[]>([]);
let extraFilter: string[] = forceArray(props.extraFilter);

Expand Down Expand Up @@ -150,6 +158,7 @@ export const SearchContextProvider: React.FC<{
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishedFilter,
tagsFilter,
sort,
overrideQueries,
Expand All @@ -165,6 +174,8 @@ export const SearchContextProvider: React.FC<{
setBlockTypesFilter,
problemTypesFilter,
setProblemTypesFilter,
publishedFilter,
setPublishedFilter,
tagsFilter,
setTagsFilter,
extraFilter,
Expand Down
19 changes: 19 additions & 0 deletions 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 @@ -185,6 +191,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,
Expand All @@ -200,6 +207,7 @@ export async function fetchSearchResults({
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishedFilter,
tagsFilter,
extraFilter,
sort,
Expand All @@ -223,6 +231,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.
Expand All @@ -246,6 +264,7 @@ export async function fetchSearchResults({
...typeFilters,
...extraFilterFormatted,
...tagsFilterFormatted,
...publishStatusFilterFormatted,
],
attributesToHighlight: ['display_name', 'content'],
highlightPreTag: HIGHLIGHT_PRE_TAG,
Expand Down
5 changes: 5 additions & 0 deletions src/search-manager/data/apiHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
fetchDocumentById,
fetchBlockTypes,
OverrideQueries,
type PublishStatus,
} from './api';

/**
Expand Down Expand Up @@ -55,6 +56,7 @@ export const useContentSearchResults = ({
searchKeywords,
blockTypesFilter = [],
problemTypesFilter = [],
publishedFilter = [],
tagsFilter = [],
sort = [],
overrideQueries,
Expand All @@ -71,6 +73,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 */
Expand All @@ -90,6 +93,7 @@ export const useContentSearchResults = ({
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishedFilter,
tagsFilter,
sort,
overrideQueries,
Expand All @@ -105,6 +109,7 @@ export const useContentSearchResults = ({
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishedFilter,
tagsFilter,
sort,
// For infinite pagination of results, we can retrieve additional pages if requested.
Expand Down

0 comments on commit a68529c

Please sign in to comment.