Skip to content

Commit

Permalink
feat: add mutations and hooks for tags, remove categories
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Dec 10, 2024
1 parent 7361bca commit 829a42c
Show file tree
Hide file tree
Showing 29 changed files with 636 additions and 907 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@eslint/compat": "1.2.3",
"@eslint/eslintrc": "3.2.0",
"@eslint/js": "9.15.0",
"@graasp/sdk": "5.3.1",
"@graasp/sdk": "5.4.0",
"@graasp/translations": "1.42.0",
"@testing-library/dom": "10.4.0",
"@testing-library/react": "16.0.1",
Expand Down
84 changes: 0 additions & 84 deletions src/api/category.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from '../member/api.js';
export * from './action.js';
export * from './apps.js';
export * from './authentication.js';
export * from './category.js';
export * from './chat.js';
export * from './csvUserImport.js';
export * from './etherpad.js';
Expand All @@ -21,3 +20,4 @@ export * from './membership.js';
export * from './mention.js';
export * from './search.js';
export * from './shortLink.js';
export * from '../tag/api.js';
114 changes: 0 additions & 114 deletions src/api/search.test.ts

This file was deleted.

82 changes: 23 additions & 59 deletions src/api/search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Category, INDEX_NAME, MeiliSearchResults } from '@graasp/sdk';
import { MeiliSearchResults, Tag, TagCategory } from '@graasp/sdk';

import { SEARCH_PUBLISHED_ITEMS_ROUTE } from '../routes.js';
import {
SEARCH_PUBLISHED_ITEMS_ROUTE,
buildGetSearchFacets,
} from '../routes.js';
import { PartialQueryConfigForApi } from '../types.js';

export type MeiliSearchProps = {
Expand All @@ -13,69 +16,30 @@ export type MeiliSearchProps = {
highlightPostTag?: string;
page?: number;
elementsPerPage?: number;
query?: string;
tags?: Record<TagCategory, Tag['name'][]>;
isPublishedRoot?: boolean;
langs?: string[];
};

export const searchPublishedItems = async (
{
query: q,
categories,
isPublishedRoot = true,
limit,
offset,
sort,
attributesToCrop,
cropLength,
highlightPreTag,
highlightPostTag,
langs,
}: {
query?: string;
categories?: Category['id'][][];
isPublishedRoot?: boolean;
langs?: string[];
} & MeiliSearchProps,
query: MeiliSearchProps,
{ API_HOST, axios }: PartialQueryConfigForApi,
) => {
const query: {
indexUid: string;
attributesToHighlight: string[];
q?: string;
filter?: string;
} & MeiliSearchProps = {
indexUid: INDEX_NAME,
attributesToHighlight: ['name', 'description', 'content', 'creator'],
attributesToCrop,
cropLength,
q,
limit,
offset,
sort,
highlightPreTag,
highlightPostTag,
};

// handle filters
const categoriesFilter = categories
?.map(
(categoriesForType) => `categories IN [${categoriesForType.join(',')}]`,
)
?.join(' AND ');

const isPublishedFilter = isPublishedRoot
? `isPublishedRoot = ${isPublishedRoot}`
: '';
const langsFilter = langs?.length ? `lang IN [${langs.join(',')}]` : '';
const filters = [categoriesFilter, isPublishedFilter, langsFilter]
.filter((v) => v)
.join(' AND ');

if (filters) {
query.filter = filters;
}
return axios
.post<
MeiliSearchResults['results'][0]
>(`${API_HOST}/${SEARCH_PUBLISHED_ITEMS_ROUTE}`, query)
.then(({ data }) => data);
};

export const getSearchFacets = async (
query: MeiliSearchProps & { facetName: string },
{ API_HOST, axios }: PartialQueryConfigForApi,
) => {
return axios
.post<MeiliSearchResults>(`${API_HOST}/${SEARCH_PUBLISHED_ITEMS_ROUTE}`, {
queries: [query],
})
.post<
Record<string, number>
>(`${API_HOST}/${buildGetSearchFacets(query.facetName)}`, query)
.then(({ data }) => data);
};
Loading

0 comments on commit 829a42c

Please sign in to comment.