mirror of
https://github.com/strapi/strapi.git
synced 2025-11-08 14:19:40 +00:00
fix: gracefully fail 404 results from API
This commit is contained in:
parent
55c20e9773
commit
c1f8d20245
@ -8,8 +8,9 @@ export const normalizeSearchResults = (relations, { mainFieldName }) => {
|
|||||||
...relations,
|
...relations,
|
||||||
data: pages
|
data: pages
|
||||||
.map((page) =>
|
.map((page) =>
|
||||||
(page ?? []).results.map((relation) => normalizeRelation(relation, { mainFieldName }))
|
page?.results.map((relation) => normalizeRelation(relation, { mainFieldName }))
|
||||||
)
|
)
|
||||||
|
.filter(Boolean)
|
||||||
.flat(),
|
.flat(),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -49,13 +49,26 @@ export const useRelation = (cacheKey, { name, relation, search }) => {
|
|||||||
const relationsRes = useInfiniteQuery(['relation', cacheKey], fetchRelations, {
|
const relationsRes = useInfiniteQuery(['relation', cacheKey], fetchRelations, {
|
||||||
cacheTime: 0,
|
cacheTime: 0,
|
||||||
enabled: relation.enabled,
|
enabled: relation.enabled,
|
||||||
|
/**
|
||||||
|
* @type {(lastPage:
|
||||||
|
* | { data: null }
|
||||||
|
* | { results: any[],
|
||||||
|
* pagination: {
|
||||||
|
* page: number,
|
||||||
|
* pageCount: number,
|
||||||
|
* pageSize: number,
|
||||||
|
* total: number
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* ) => number}
|
||||||
|
*/
|
||||||
getNextPageParam(lastPage) {
|
getNextPageParam(lastPage) {
|
||||||
const isXToOneRelation = !lastPage?.pagination;
|
const isXToOneRelation = !lastPage?.pagination;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!lastPage || // the API may send an empty 204 response
|
!lastPage || // the API may send an empty 204 response
|
||||||
isXToOneRelation || // xToOne relations do not have a pagination
|
isXToOneRelation || // xToOne relations do not have a pagination
|
||||||
lastPage.pagination.page >= lastPage.pagination.pageCount
|
lastPage?.pagination.page >= lastPage?.pagination.pageCount
|
||||||
) {
|
) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -126,8 +139,21 @@ export const useRelation = (cacheKey, { name, relation, search }) => {
|
|||||||
fetchSearch,
|
fetchSearch,
|
||||||
{
|
{
|
||||||
enabled: Object.keys(searchParams).length > 0,
|
enabled: Object.keys(searchParams).length > 0,
|
||||||
|
/**
|
||||||
|
* @type {(lastPage:
|
||||||
|
* | { data: null }
|
||||||
|
* | { results: any[],
|
||||||
|
* pagination: {
|
||||||
|
* page: number,
|
||||||
|
* pageCount: number,
|
||||||
|
* pageSize: number,
|
||||||
|
* total: number
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* ) => number}
|
||||||
|
*/
|
||||||
getNextPageParam(lastPage) {
|
getNextPageParam(lastPage) {
|
||||||
if (lastPage.pagination.page >= lastPage.pagination.pageCount) {
|
if (!lastPage?.pagination || lastPage.pagination.page >= lastPage.pagination.pageCount) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user