-
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
b603ee2
commit a8d49ba
Showing
4 changed files
with
204 additions
and
16 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,117 @@ | ||
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 UseInfiniteSearchApiResult = { | ||
pages: SearchResponse[]; | ||
error?: unknown; | ||
status: Status; | ||
isFetchingNextPage: boolean; | ||
hasNextPage: boolean; | ||
fetchNextPage: () => Promise<void>; | ||
}; | ||
|
||
type AggregationsConfig = { | ||
resources: ResourceAggregationsEnum[]; | ||
content_files: ContentFileAggregationsEnum[]; | ||
}; | ||
|
||
type UseInfiniteSearchApiProps = { | ||
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 useInfiniteSearchApi = ({ | ||
params, | ||
limit = DEFAULT_LIMIT, | ||
makeRequest = defaultMakeRequest, | ||
baseUrl, | ||
aggregations, | ||
}: UseInfiniteSearchApiProps): UseInfiniteSearchApiResult => { | ||
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 getPageUrl = useCallback( | ||
(page: number) => { | ||
const offset = page * limit; | ||
return getSearchUrl(baseUrl, { | ||
limit, | ||
offset, | ||
...params, | ||
aggregations: aggregations?.[params.endpoint] ?? [], | ||
}); | ||
}, | ||
[aggregations, baseUrl, limit, params] | ||
); | ||
|
||
const fetchNextPage = useCallback(async () => { | ||
if (!hasNextPage || isFetchingNextPage) return; | ||
const url = getPageUrl(nextPage); | ||
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); | ||
} | ||
}, [getPageUrl, hasNextPage, isFetchingNextPage, makeRequest, nextPage]); | ||
|
||
useEffect(() => { | ||
setNextPage(0); | ||
setPages([]); | ||
setError(undefined); | ||
setIsFetchingNextPage(false); | ||
setStatus("pending"); | ||
urlRef.current = null; | ||
fetchNextPage(); | ||
}, [getPageUrl(0)]); | ||
Check warning on line 105 in src/revamp/useInfiniteSearch.ts GitHub Actions / javascript-tests
|
||
|
||
return { | ||
pages, | ||
error, | ||
status, | ||
isFetchingNextPage, | ||
hasNextPage, | ||
fetchNextPage, | ||
}; | ||
}; | ||
|
||
export default useInfiniteSearchApi; |
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(); | ||
}; |