-
Notifications
You must be signed in to change notification settings - Fork 17
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
Showing
14 changed files
with
130 additions
and
196 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,111 +1,36 @@ | ||
import { useCallback, useEffect, useMemo, useRef, useState } from "react"; | ||
import { useRollbar } from "@rollbar/react"; | ||
import { useMemo } from "react"; | ||
|
||
import { FetchOptions } from "../model/fetchOptions"; | ||
import { ItemsResponse } from "../model/itemsResponse"; | ||
import { PaginatedResource } from "../model/paginatedResource"; | ||
import { PaginationOptions } from "../model/paginationOptions"; | ||
import { DataError } from "../utils/error"; | ||
|
||
import { usePagination } from "./usePagination"; | ||
|
||
type PaginatedResourceProps<T> = Omit<PaginatedResource<T>, "notFound" | "pagination" | "refetch">; | ||
|
||
const defaultResourceProps = { | ||
data: [], | ||
totalCount: undefined, | ||
loading: true, | ||
error: undefined, | ||
}; | ||
import { useResource } from "./useResource"; | ||
|
||
export function usePaginatedResource<T = any, F extends any[] = any[]>( | ||
fetchItems: (...args: [...F, PaginationOptions]) => ItemsResponse<T>|Promise<ItemsResponse<T>>, | ||
args: F, | ||
options?: FetchOptions | ||
options: FetchOptions = {} | ||
) { | ||
const rollbar = useRollbar(); | ||
|
||
const fetchRequestKey = useMemo(() => JSON.stringify(args), [JSON.stringify(args)]); | ||
const previousFetchRequestKeyRef = useRef<string>(); | ||
|
||
const [resourceProps, setResourceProps] = useState<PaginatedResourceProps<T>>(defaultResourceProps); | ||
|
||
const pagination = usePagination(); | ||
|
||
const fetchData = useCallback(async () => { | ||
if (options?.waitUntil) { | ||
// wait until all required condition are met | ||
return; | ||
const resource = useResource(fetchItems, [...args, { | ||
limit: pagination.limit, | ||
offset: pagination.offset | ||
}], { | ||
...options, | ||
onSuccess: (data) => { | ||
data && pagination.set(data.pagination); | ||
} | ||
|
||
if (!options?.skip) { | ||
try { | ||
const items = await fetchItems(...args, { | ||
limit: pagination.limit, | ||
offset: pagination.offset, | ||
}); | ||
|
||
setResourceProps((props) => ({ | ||
...props, | ||
data: items.data, | ||
totalCount: items.totalCount | ||
})); | ||
|
||
pagination.set(items.pagination); | ||
} catch(error) { | ||
if (error instanceof DataError) { | ||
rollbar.error(error); | ||
setResourceProps((props) => ({ | ||
...props, | ||
error | ||
})); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
setResourceProps((props) => ({ | ||
...props, | ||
loading: false | ||
})); | ||
}, [ | ||
fetchItems, | ||
fetchRequestKey, | ||
pagination.limit, | ||
pagination.offset, | ||
options?.skip, | ||
]); | ||
|
||
useEffect(() => { | ||
setResourceProps((props) => ({ | ||
...props, | ||
data: [], | ||
error: undefined, | ||
loading: true | ||
})); | ||
fetchData(); | ||
}, [fetchData]); | ||
|
||
return useMemo<PaginatedResource<T>>( | ||
() => { | ||
if (previousFetchRequestKeyRef.current && previousFetchRequestKeyRef.current !== fetchRequestKey) { | ||
// do not return old data if the fetch args changed | ||
return ({ | ||
...defaultResourceProps, | ||
notFound: false, | ||
pagination, | ||
refetch: fetchData | ||
}); | ||
} | ||
|
||
return ({ | ||
...resourceProps, | ||
notFound: !resourceProps.loading && !resourceProps.error && (!resourceProps.data || resourceProps.data.length === 0), | ||
pagination, | ||
refetch: fetchData | ||
}); | ||
}, | ||
[fetchRequestKey, resourceProps, pagination, fetchData] | ||
); | ||
}); | ||
|
||
return useMemo<PaginatedResource<T>>(() => ({ | ||
data: resource.data?.data, | ||
totalCount: resource.data?.totalCount, | ||
loading: resource.loading, | ||
notFound: resource.notFound || resource.data?.data.length === 0, | ||
error: resource.error, | ||
pagination, | ||
}), [resource, pagination]); | ||
} |
Oops, something went wrong.