Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherChudzicki committed Feb 26, 2024
1 parent ffb90c9 commit 10adaab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
24 changes: 16 additions & 8 deletions src/hooks/useInfiniteSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type UseInfiniteSearchProps = {
limit?: number
aggregations?: AggregationsConfig
makeRequest?: (url: string) => Promise<{ data: any }>
keepPreviousData?: boolean
}

const DEFAULT_LIMIT = 10
Expand Down Expand Up @@ -58,13 +59,15 @@ const useInfiniteSearch = ({
limit = DEFAULT_LIMIT,
makeRequest = defaultMakeRequest,
baseUrl,
aggregations
aggregations,
keepPreviousData
}: UseInfiniteSearchProps): UseInfiniteSearchResult => {
const [nextPage, setNextPage] = useState(0)
const [error, setError] = useState<unknown>()
const [pages, setPages] = useState<SearchResponse[]>([])
const [status, setStatus] = useState<Status>("pending")
const [isFetchingNextPage, setIsFetchingNextPage] = useState(false)
const [isPreviousData, setIsPreviousData] = useState(false)
const urlRef = useRef<string | null>()

const hasNextPage =
Expand Down Expand Up @@ -95,7 +98,9 @@ const useInfiniteSearch = ({
urlRef.current = null
setIsFetchingNextPage(false)
setStatus("success")
setIsPreviousData(false)
setPages(pages => {
if (nextPage === 0) return [data]
return [...pages, data]
})
setNextPage(nextPage + 1)
Expand All @@ -110,18 +115,21 @@ const useInfiniteSearch = ({
useEffect(() => {
// Reset state when first page changes
setNextPage(0)
setPages([])
setError(undefined)
setIsFetchingNextPage(false)
setStatus("pending")
if (!keepPreviousData) {
setIsPreviousData(true)
setPages([])
setError(undefined)
setIsFetchingNextPage(false)
setStatus("pending")
}
urlRef.current = null
}, [firstPageUrl])
}, [firstPageUrl, keepPreviousData])

useEffect(() => {
if (status === "pending") {
if (status === "pending" || isPreviousData) {
fetchNextPage()
}
}, [status, fetchNextPage])
}, [status, fetchNextPage, isPreviousData])

return {
pages,
Expand Down
36 changes: 18 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
export * from "./constants";
export * from "./constants"

export * from "./old_hooks/url_utils";
export * from "./open_api_generated/api";
export * from "./facet_display/types";
export * from "./old_hooks/url_utils"
export * from "./open_api_generated/api"
export * from "./facet_display/types"
export {
default as FacetDisplay,
getDepartmentName,
getLevelName,
} from "./facet_display/FacetDisplay";
export { default as FilterableFacet } from "./facet_display/FilterableFacet";
export { sanitizeFacets } from "./facet_display/SanitizeFacets";
getLevelName
} from "./facet_display/FacetDisplay"
export { default as FilterableFacet } from "./facet_display/FilterableFacet"
export { sanitizeFacets } from "./facet_display/SanitizeFacets"

export { buildSearchUrl, SearchQueryParams } from "./old_hooks/search";
export { buildSearchUrl, SearchQueryParams } from "./old_hooks/search"

export {
useCourseSearch,
useFacetOptions,
useSearchInputs,
useSyncUrlAndSearch,
} from "./old_hooks/useCourseSearch";
useSyncUrlAndSearch
} from "./old_hooks/useCourseSearch"

export { default as useSearchQueryParams } from "./hooks/useSearchQueryParams";
export { default as useSearchQueryParams } from "./hooks/useSearchQueryParams"
export type {
UseSearchQueryParamsProps,
UseSearchQueryParamsResult,
} from "./hooks/useSearchQueryParams";
UseSearchQueryParamsResult
} from "./hooks/useSearchQueryParams"

export type { Endpoint, FacetName } from "./hooks/configs";
export type { Endpoint, FacetName } from "./hooks/configs"

export { default as useInfiniteSearch } from "./hooks/useInfiniteSearch";
export { default as useInfiniteSearch } from "./hooks/useInfiniteSearch"
export type {
UseInfiniteSearchProps,
UseInfiniteSearchResult,
} from "./hooks/useInfiniteSearch";
UseInfiniteSearchResult
} from "./hooks/useInfiniteSearch"

0 comments on commit 10adaab

Please sign in to comment.