Skip to content

Commit

Permalink
fix: use limit and offset (#450)
Browse files Browse the repository at this point in the history
* fix: use limit and offset

* fix: tests

* fix: improve usage of limit and page if offset and limit are specified
  • Loading branch information
spaenleh authored Sep 29, 2023
1 parent f77a0c4 commit 7cd8608
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const axios = configureAxios();

export type MeiliSearchProps = {
limit?: number;
offset?: number;
sort?: string[];
attributesToCrop?: string[];
cropLength?: number;
highlightPreTag?: string;
highlightPostTag?: string;
page?: number;
elementsPerPage?: number;
};

/* eslint-disable import/prefer-default-export */
Expand All @@ -23,12 +25,12 @@ export const searchPublishedItems = async (
categories,
isPublishedRoot = true,
limit,
offset,
sort,
attributesToCrop,
cropLength,
highlightPreTag,
highlightPostTag,
page,
}: {
query?: string;
categories?: Category['id'][][];
Expand All @@ -48,10 +50,10 @@ export const searchPublishedItems = async (
cropLength,
q,
limit,
offset,
sort,
highlightPreTag,
highlightPostTag,
page,
};

// handle filters
Expand Down
5 changes: 3 additions & 2 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,12 @@ export const buildSearchPublishedItemsKey = (args: {
categories?: Category['id'][][];
isPublishedRoot?: boolean;
limit?: number;
offset?: number;
sort?: string[];
highlightPreTag?: string;
highlightPostTag?: string;
page: number;
}) => [ITEMS_KEY, 'search', { isPublishedRoot: false, ...args }, args.page];
page?: number;
}) => [ITEMS_KEY, 'search', { isPublishedRoot: false, ...args }];

export const DATA_KEYS = {
APPS_KEY,
Expand Down
10 changes: 6 additions & 4 deletions src/hooks/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const RESPONSE = {
describe('Published Search Hook', () => {
afterEach(() => {
nock.cleanAll();
jest.clearAllMocks();
queryClient.clear();
});

Expand Down Expand Up @@ -159,6 +160,7 @@ describe('Published Search Hook', () => {
query,
categories,
isPublishedRoot,
page: 1,
});
const response = RESPONSE;
const endpoints = [{ route, response, method: HttpMethod.POST }];
Expand All @@ -179,8 +181,8 @@ describe('Published Search Hook', () => {
attributesToCrop: undefined,
highlightPostTag: undefined,
highlightPreTag: undefined,
limit: undefined,
page: 1,
limit: 24,
offset: 0,
sort: undefined,
},
],
Expand Down Expand Up @@ -229,11 +231,11 @@ describe('Published Search Hook', () => {
],
q: query,
filter: 'categories IN [mycategoryid] AND isPublishedRoot = true',
page: 3,
limit: 24,
offset: 48, // (3 - 1) * 24
attributesToCrop: undefined,
highlightPostTag: undefined,
highlightPreTag: undefined,
limit: undefined,
sort: undefined,
},
],
Expand Down
11 changes: 6 additions & 5 deletions src/hooks/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ export default (queryConfig: QueryClientConfig) => {
cropLength,
enabled = true,
isPublishedRoot = true,
limit,
query,
sort,
highlightPreTag,
highlightPostTag,
page = 1,
page,
limit,
offset,
elementsPerPage = 24,
}: {
categories?: Category['id'][][];
enabled?: boolean;
Expand All @@ -37,7 +39,6 @@ export default (queryConfig: QueryClientConfig) => {
query: debouncedQuery,
categories,
isPublishedRoot,
limit,
sort,
highlightPreTag,
highlightPostTag,
Expand All @@ -50,12 +51,12 @@ export default (queryConfig: QueryClientConfig) => {
categories,
cropLength,
isPublishedRoot,
limit,
limit: page ? elementsPerPage : limit,
offset: page ? elementsPerPage * (page - 1) : offset,
query: debouncedQuery,
sort,
highlightPreTag,
highlightPostTag,
page,
},
queryConfig,
).then((data) => convertJs(data)),
Expand Down

0 comments on commit 7cd8608

Please sign in to comment.