Skip to content

Commit

Permalink
feat: implement pills and filter
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielVZ96 committed Dec 13, 2024
1 parent f02e334 commit cac19a1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
19 changes: 9 additions & 10 deletions src/search-manager/FilterByPublished.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { FormattedMessage } from '@edx/frontend-platform/i18n';
import {
Badge,
Form,
Expand All @@ -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';

Expand All @@ -17,16 +15,17 @@ import { PublishStatus } from './data/api';
*/
const FilterByPublished: React.FC<Record<never, never>> = () => {
const {
publishedFilter,
setPublishedFilter,
publishStatus,
publishStatusFilter,
setPublishStatusFilter,
} = useSearchContext();

const clearFilters = React.useCallback(() => {
setPublishedFilter([]);
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
}, []);

const toggleFilterMode = React.useCallback((mode: PublishStatus) => {
setPublishedFilter(oldList => {
setPublishStatusFilter(oldList => {

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

View check run for this annotation

Codecov / codecov/patch

src/search-manager/FilterByPublished.tsx#L28

Added line #L28 was not covered by tests
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
}
Expand All @@ -44,7 +43,7 @@ const FilterByPublished: React.FC<Record<never, never>> = () => {
<Form.Group className="mb-0">
<Form.CheckboxSet
name="block-type-filter"
value={publishedFilter}
value={publishStatusFilter}
>
<Menu className="block-type-refinement-menu" style={{ boxShadow: 'none' }}>
<MenuItem
Expand All @@ -54,7 +53,7 @@ const FilterByPublished: React.FC<Record<never, never>> = () => {
>
<div>
Published
{' '}<Badge variant="light" pill>15</Badge>
{' '}<Badge variant="light" pill>{publishStatus[PublishStatus.Published] ?? 0}</Badge>
</div>
</MenuItem>
<MenuItem
Expand All @@ -64,7 +63,7 @@ const FilterByPublished: React.FC<Record<never, never>> = () => {
>
<div>
Modified since publish
{' '}<Badge variant="light" pill>5</Badge>
{' '}<Badge variant="light" pill>{publishStatus[PublishStatus.Modified] ?? 0}</Badge>
</div>
</MenuItem>
<MenuItem
Expand All @@ -74,7 +73,7 @@ const FilterByPublished: React.FC<Record<never, never>> = () => {
>
<div>
Never published
{' '}<Badge variant="light" pill>2</Badge>
{' '}<Badge variant="light" pill>{publishStatus[PublishStatus.NeverPublished] ?? 0}</Badge>
</div>
</MenuItem>
</Menu>
Expand Down
13 changes: 7 additions & 6 deletions src/search-manager/SearchManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ 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[]>>;
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 @@ -105,7 +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 [publishedFilter, setPublishedFilter] = React.useState<PublishStatus[]>([]);
const [publishStatusFilter, setPublishStatusFilter] = React.useState<PublishStatus[]>([]);
const [tagsFilter, setTagsFilter] = React.useState<string[]>([]);
const [usageKey, setUsageKey] = useStateWithUrlSearchParam(
'',
Expand Down Expand Up @@ -170,7 +171,7 @@ export const SearchContextProvider: React.FC<{
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishedFilter,
publishStatusFilter,
tagsFilter,
sort,
skipBlockTypeFetch,
Expand All @@ -186,8 +187,8 @@ export const SearchContextProvider: React.FC<{
setBlockTypesFilter,
problemTypesFilter,
setProblemTypesFilter,
publishedFilter,
setPublishedFilter,
publishStatusFilter,
setPublishStatusFilter,
tagsFilter,
setTagsFilter,
extraFilter,
Expand Down
18 changes: 6 additions & 12 deletions src/search-manager/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -201,7 +201,7 @@ export async function fetchSearchResults({
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishedFilter,
publishStatusFilter,
tagsFilter,
extraFilter,
sort,
Expand All @@ -213,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 @@ -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);

Expand Down Expand Up @@ -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.
Expand All @@ -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,
};
}
Expand Down
9 changes: 5 additions & 4 deletions src/search-manager/data/apiHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const useContentSearchResults = ({
searchKeywords,
blockTypesFilter = [],
problemTypesFilter = [],
publishedFilter = [],
publishStatusFilter = [],
tagsFilter = [],
sort = [],
skipBlockTypeFetch = false,
Expand All @@ -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 */
Expand All @@ -91,7 +91,7 @@ export const useContentSearchResults = ({
searchKeywords,
blockTypesFilter,
problemTypesFilter,
publishedFilter,
publishStatusFilter,
tagsFilter,
sort,
],
Expand All @@ -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.
Expand All @@ -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,
Expand Down

0 comments on commit cac19a1

Please sign in to comment.