Skip to content

Commit

Permalink
Moving search query functionality to helpe
Browse files Browse the repository at this point in the history
...and add some forgotten defined config.r
  • Loading branch information
spaceo committed Oct 31, 2024
1 parent 2fa2fb0 commit 45e0fda
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 52 deletions.
71 changes: 19 additions & 52 deletions components/pages/searchPageLayout/SearchPageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,12 @@ import {

import SearchFilterBar from "../../shared/searchFilters/SearchFilterBar"
import SearchResults from "./SearchResults"
import { getFacetsForSearchRequest, getNextPageParamsFunc, getSearchQueryArguments } from "./helper"

const branchIds = goConfig<`${number}`[]>("search.branch.ids")
const SEARCH_RESULTS_LIMIT = goConfig<number>("search.item.limit")

export type FilterItemTerm = Omit<FacetValue, "__typename">

export const formatFacetTerms = (filters: { [key: string]: { [key: string]: FilterItemTerm } }) => {
return Object.keys(filters).reduce(
(acc, key) => ({
...acc,
[key]: Object.keys(filters[key]),
}),
{}
)
}

const SearchPageLayout = ({ searchQuery }: { searchQuery?: string }) => {
const searchParams = useSearchParams()
const q = searchQuery || searchParams.get("q") || ""
Expand All @@ -42,62 +32,39 @@ const SearchPageLayout = ({ searchQuery }: { searchQuery?: string }) => {
const loadMoreRef = useRef(null)
const isInView = useInView(loadMoreRef)

const facetsForSearchRequest = 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[] }
)
const facetsForSearchRequest = getFacetsForSearchRequest({
facetDefinitions,
searchParams,
mapFacetsToFilters,
})
const searchQueryArguments = getSearchQueryArguments({
q: currentQueryString,
currentPage,
facetFilters,
})

const {
data,
fetchNextPage,
isLoading: isLoadingResults,
} = useInfiniteQuery({
queryKey: useSearchWithPaginationQuery.getKey({
q: { all: currentQueryString },
offset: 0,
limit: SEARCH_RESULTS_LIMIT,
filters: {
branchId: branchIds,
...facetFilters,
},
...searchQueryArguments,
offset: goConfig("search.offset.initial"),
}),
queryFn: useSearchWithPaginationQuery.fetcher({
q: { all: currentQueryString },
offset: currentPage * SEARCH_RESULTS_LIMIT,
limit: SEARCH_RESULTS_LIMIT,
filters: {
branchId: branchIds,
...facetFilters,
},
}),
getNextPageParam: lastPage => {
const totalPages = Math.ceil(lastPage.search.hitcount / SEARCH_RESULTS_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
},
initialPageParam: 0,
queryFn: useSearchWithPaginationQuery.fetcher(searchQueryArguments),
getNextPageParam: getNextPageParamsFunc(currentPage),
initialPageParam: goConfig<number>("search.param.initial"),
refetchOnWindowFocus: false,
enabled: currentQueryString?.length > 0, // Disable search result & search filter queries if q doesn't exist
})

const { data: dataFacets, isLoading: isLoadingFacets } = useSearchFacetsQuery(
{
q: { all: currentQueryString },
facetLimit: 100,
q: searchQueryArguments.q,
facetLimit: goConfig("search.facet.limit"),
facets: facetDefinitions,
filters: {
branchId: branchIds,
...facetsForSearchRequest,
},
filters: searchQueryArguments.filters,
},
{
refetchOnWindowFocus: false,
Expand Down
68 changes: 68 additions & 0 deletions components/pages/searchPageLayout/helper.ts
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
}
}

0 comments on commit 45e0fda

Please sign in to comment.