-
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.
- Loading branch information
1 parent
f3927bc
commit 0f064d4
Showing
4 changed files
with
202 additions
and
13 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,119 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import type { SearchParams } from "./configs"; | ||
import { getSearchUrl } from "./util"; | ||
import type { | ||
SearchResponse, | ||
ContentFileSearchRetrieveAggregationsEnum as ContentFileAggregationsEnum, | ||
LearningResourcesSearchRetrieveAggregationsEnum as ResourceAggregationsEnum, | ||
} from "../open_api_generated"; | ||
|
||
type Status = "pending" | "error" | "success"; | ||
|
||
type UseSearchApiInfiniteResult = { | ||
pages: SearchResponse[]; | ||
error?: unknown; | ||
status: Status; | ||
isFetchingNextPage: boolean; | ||
hasNextPage: boolean; | ||
fetchNextPage: () => Promise<void>; | ||
}; | ||
|
||
type AggregationsConfig = { | ||
resources: ResourceAggregationsEnum[]; | ||
content_files: ContentFileAggregationsEnum[]; | ||
}; | ||
|
||
type UseSearchApiInfiniteProps = { | ||
params: SearchParams; | ||
baseUrl: string; | ||
limit?: number; | ||
aggregations?: AggregationsConfig; | ||
makeRequest?: (url: string) => Promise<{ data: any }>; | ||
}; | ||
|
||
const DEFAULT_LIMIT = 10; | ||
|
||
const defaultMakeRequest = async (url: string) => { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error("Failed to fetch data"); | ||
} | ||
const data = await response.json(); | ||
return { data }; | ||
}; | ||
|
||
const useSearchApiInfinite = ({ | ||
params, | ||
limit = DEFAULT_LIMIT, | ||
makeRequest = defaultMakeRequest, | ||
baseUrl, | ||
aggregations, | ||
}: UseSearchApiInfiniteProps): UseSearchApiInfiniteResult => { | ||
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 urlRef = useRef<string | null>(); | ||
|
||
const hasNextPage = | ||
pages[0] === undefined || pages[0]?.count > nextPage * limit; | ||
|
||
const fetchNextPage = useCallback(async () => { | ||
if (!hasNextPage || isFetchingNextPage) return; | ||
const offset = nextPage * limit; | ||
const url = getSearchUrl(baseUrl, { | ||
limit, | ||
offset, | ||
...params, | ||
aggregations: aggregations?.[params.endpoint] ?? [], | ||
}); | ||
urlRef.current = url; | ||
try { | ||
setIsFetchingNextPage(true); | ||
const { data } = await makeRequest(url); | ||
if (url !== urlRef.current) return; | ||
urlRef.current = null; | ||
setIsFetchingNextPage(false); | ||
setStatus("success"); | ||
setPages((pages) => { | ||
return [...pages, data]; | ||
}); | ||
setNextPage(nextPage + 1); | ||
} catch (err) { | ||
if (url !== urlRef.current) return; | ||
setStatus("error"); | ||
setError(err); | ||
} | ||
}, [ | ||
aggregations, | ||
baseUrl, | ||
hasNextPage, | ||
isFetchingNextPage, | ||
limit, | ||
makeRequest, | ||
nextPage, | ||
params, | ||
]); | ||
|
||
useEffect(() => { | ||
setNextPage(0); | ||
setPages([]); | ||
setError(undefined); | ||
setIsFetchingNextPage(false); | ||
setStatus("pending"); | ||
urlRef.current = null; | ||
fetchNextPage(); | ||
}, [params, aggregations, limit, baseUrl]); | ||
|
||
return { | ||
pages, | ||
error, | ||
status, | ||
isFetchingNextPage, | ||
hasNextPage, | ||
fetchNextPage, | ||
}; | ||
}; | ||
|
||
export default useSearchApiInfinite; |
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,56 @@ | ||
import type { SearchParams, Endpoint } from "./configs"; | ||
|
||
const endpointUrls: Record<Endpoint, string> = { | ||
resources: "api/v1/learning_resources_search/", | ||
content_files: "api/v1/content_file_search/", | ||
}; | ||
|
||
export const getSearchUrl = ( | ||
baseUrl: string, | ||
{ | ||
endpoint, | ||
queryText, | ||
sort, | ||
activeFacets, | ||
aggregations, | ||
limit, | ||
offset, | ||
}: SearchParams & { | ||
aggregations: string[]; | ||
limit?: number; | ||
offset?: number; | ||
} | ||
): string => { | ||
const url = new URL(endpointUrls[endpoint], baseUrl); | ||
|
||
if (queryText) { | ||
url.searchParams.append("q", queryText); | ||
} | ||
if (offset) { | ||
url.searchParams.append("offset", offset.toString()); | ||
} | ||
|
||
if (limit) { | ||
url.searchParams.append("limit", limit.toString()); | ||
} | ||
|
||
if (sort) { | ||
url.searchParams.append("sortby", sort); | ||
} | ||
|
||
if (aggregations && aggregations.length > 0) { | ||
url.searchParams.append("aggregations", aggregations.join(",")); | ||
} | ||
|
||
if (activeFacets) { | ||
for (const [key, value] of Object.entries(activeFacets)) { | ||
const asArray = Array.isArray(value) ? value : [value]; | ||
if (asArray.length > 0) { | ||
url.searchParams.append(key, asArray.join(",")); | ||
} | ||
} | ||
} | ||
|
||
url.searchParams.sort(); | ||
return url.toString(); | ||
}; |