-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving search query functionality to helpe
...and add some forgotten defined config.r
- Loading branch information
Showing
2 changed files
with
87 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { GetNextPageParamFunction } from "@tanstack/react-query" | ||
import { ReadonlyURLSearchParams } from "next/navigation" | ||
|
||
import goConfig from "@/lib/config/config" | ||
import { | ||
FacetFieldEnum, | ||
SearchFiltersInput, | ||
SearchWithPaginationQuery, | ||
} from "@/lib/graphql/generated/fbi/graphql" | ||
|
||
import { FilterItemTerm } from "./SearchPageLayout" | ||
|
||
export const getSearchQueryArguments = ({ | ||
q, | ||
currentPage, | ||
facetFilters, | ||
}: { | ||
q: string | ||
currentPage: number | ||
facetFilters: SearchFiltersInput | ||
}) => { | ||
const limit = goConfig<number>("search.item.limit") | ||
return { | ||
q: { all: q }, | ||
offset: currentPage * limit, | ||
limit: limit, | ||
filters: { | ||
branchId: goConfig<`${number}`[]>("search.branch.ids"), | ||
...facetFilters, | ||
}, | ||
} | ||
} | ||
|
||
export const getFacetsForSearchRequest = ({ | ||
facetDefinitions, | ||
searchParams, | ||
mapFacetsToFilters, | ||
}: { | ||
facetDefinitions: FacetFieldEnum[] | ||
searchParams: ReadonlyURLSearchParams | ||
mapFacetsToFilters: Record<FacetFieldEnum, keyof SearchFiltersInput> | ||
}) => { | ||
return facetDefinitions.reduce( | ||
(acc: SearchFiltersInput, facetDefinition) => { | ||
const values = searchParams.getAll(mapFacetsToFilters[facetDefinition]) | ||
if (values.length > 0) { | ||
return { | ||
...acc, | ||
[mapFacetsToFilters[facetDefinition]]: [...values], | ||
} | ||
} | ||
return acc | ||
}, | ||
{} as { [key: string]: keyof SearchFiltersInput[] } | ||
) | ||
} | ||
|
||
export const getNextPageParamsFunc = ( | ||
currentPage: number | ||
): GetNextPageParamFunction<number, SearchWithPaginationQuery> => { | ||
const limit = goConfig<number>("search.item.limit") | ||
|
||
return ({ search: { hitcount } }) => { | ||
const totalPages = Math.ceil(hitcount / limit) | ||
const nextPage = currentPage + 1 | ||
return currentPage < totalPages ? nextPage : undefined // By returning undefined if there are no more pages, hasNextPage boolean will be set to false | ||
} | ||
} |