ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
220 lines • 8.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePrevNextController = void 0;
const react_query_1 = require("@tanstack/react-query");
const core_1 = require("../core/index.cjs");
const dataProvider_1 = require("../dataProvider/index.cjs");
const store_1 = require("../store/index.cjs");
const list_1 = require("./list/index.cjs");
const record_1 = require("./record/index.cjs");
const routing_1 = require("../routing/index.cjs");
/**
* A hook used to fetch the previous and next record identifiers for a given record and resource.
*
* It fetches the list of records according to the filters
* and the sort order configured in the list, and merges
* the filters and the sorting order passed as props.
*
* usePrevNextController can be used anywhere a record context is provided
* (often inside a `<Show>` or `<Edit>` component).
*
* @example <caption>Simple usage</caption>
*
* import { usePrevNextControllerProps } from 'ra-core';
* const {
* hasPrev,
* hasNext,
* prevPath,
* nextPath,
* index,
* total,
* error,
* isPending,
* } = usePrevNextController(props);
*
* @example <caption>Custom PrevNextButton</caption>
*
* import { UsePrevNextControllerProps, useTranslate } from 'ra-core';
* import NavigateBefore from '@mui/icons-material/NavigateBefore';
* import NavigateNext from '@mui/icons-material/NavigateNext';
* import ErrorIcon from '@mui/icons-material/Error';
* import { Link } from 'react-router-dom';
* import { CircularProgress, IconButton } from '@mui/material';
*
* const MyPrevNextButtons = props => {
* const {
* hasPrev,
* hasNext,
* nextPath,
* prevPath,
* index,
* total,
* error,
* isPending,
* } = usePrevNextController(props);
*
* const translate = useTranslate();
*
* if (isPending) {
* return <CircularProgress size={14} />;
* }
*
* if (error) {
* return (
* <ErrorIcon
* color="error"
* fontSize="small"
* titleAccess="error"
* aria-errormessage={error.message}
* />
* );
* }
*
* return (
* <ul>
* <li>
* <IconButton
* component={hasPrev ? Link : undefined}
* to={navigateToPrev}
* aria-label={translate('ra.navigation.previous')}
* disabled={!hasPrev}
* >
* <NavigateBefore />
* </IconButton>
* </li>
* {typeof index === 'number' && (
* <li>
* {index + 1} / {total}
* </li>
* )}
* <li>
* <IconButton
* component={hasNext ? Link : undefined}
* to={navigateToNext}
* aria-label={translate('ra.navigation.next')}
* disabled={!hasNext}
* >
* <NavigateNext />
* </IconButton>
* </li>
* </ul>
* );
* };
*/
const usePrevNextController = (props) => {
const { linkType = 'edit', storeKey, limit = 1000, sort: initialSort = { field: 'id', order: list_1.SORT_ASC }, filter: permanentFilter = {}, filterDefaultValues = {}, queryOptions = {
staleTime: 5 * 60 * 1000,
}, } = props;
const record = (0, record_1.useRecordContext)(props);
const resource = (0, core_1.useResourceContext)(props);
const createPath = (0, routing_1.useCreatePath)();
if (!resource) {
throw new Error(`useNextPrevController was called outside of a ResourceContext and without a resource prop. You must set the resource prop.`);
}
const [storedParams] = (0, store_1.useStore)(storeKey || `${resource}.listParams`, {
filter: filterDefaultValues,
order: initialSort.order,
sort: initialSort.field,
page: 1,
perPage: 10,
displayedFilters: {},
});
const dataProvider = (0, dataProvider_1.useDataProvider)();
const queryClient = (0, react_query_1.useQueryClient)();
const pagination = { page: 1, perPage: limit };
const sort = {
field: storedParams.sort,
order: storedParams.order,
};
const filter = { ...storedParams.filter, ...permanentFilter };
const { meta, ...otherQueryOptions } = queryOptions;
const params = { pagination, sort, filter, meta };
// try to use data from the cache first
const queryData = queryClient.getQueryData([
resource,
'getList',
{
...params,
pagination: {
page: storedParams.page,
perPage: storedParams.perPage,
},
},
]);
const recordIndexInQueryData = queryData?.data?.findIndex(r => r.id === record?.id);
const isRecordIndexFirstInNonFirstPage = recordIndexInQueryData === 0 && storedParams.page > 1;
const isRecordIndexLastInNonLastPage = queryData?.data && queryData?.total
? recordIndexInQueryData === queryData?.data?.length - 1 &&
storedParams.page < queryData?.total / storedParams.perPage
: undefined;
const canUseCacheData = record &&
queryData?.data &&
recordIndexInQueryData !== -1 &&
!isRecordIndexFirstInNonFirstPage &&
!isRecordIndexLastInNonLastPage;
// If the previous and next ids are not in the cache, fetch the entire list.
// This is necessary e.g. when coming directly to a detail page,
// without displaying the list first
const { data, error, isFetching, isLoading, isPending } = (0, react_query_1.useQuery)({
queryKey: [resource, 'getList', params],
queryFn: queryParams => {
return dataProvider.getList(resource, {
...params,
signal: dataProvider.supportAbortSignal === true
? queryParams.signal
: undefined,
});
},
enabled: !canUseCacheData,
...otherQueryOptions,
});
const finalData = canUseCacheData ? queryData.data : data?.data || [];
if (!record || (isPending && !canUseCacheData))
return {
isFetching: true,
isLoading: true,
isPending: true,
prevPath: undefined,
nextPath: undefined,
index: undefined,
total: undefined,
hasPrev: false,
hasNext: false,
};
const ids = finalData.map(record => record.id);
const index = ids.indexOf(record.id);
const previousId = typeof ids[index - 1] !== 'undefined' ? ids[index - 1] : null; // could be 0
const nextId = index !== -1 && index < ids.length - 1 ? ids[index + 1] : null;
return {
hasPrev: previousId !== null,
hasNext: nextId !== null,
prevPath: previousId !== null
? createPath({
type: linkType,
resource,
id: previousId,
})
: undefined,
nextPath: nextId !== null
? createPath({
type: linkType,
resource,
id: nextId,
})
: undefined,
index: index === -1
? undefined
: index +
(canUseCacheData
? (storedParams.perPage ?? 0) *
((storedParams.page ?? 1) - 1)
: 0),
total: canUseCacheData ? queryData?.total : data?.total,
error,
isFetching: canUseCacheData ? false : isFetching,
isLoading: canUseCacheData ? false : isLoading,
isPending: canUseCacheData ? false : isPending,
};
};
exports.usePrevNextController = usePrevNextController;
//# sourceMappingURL=usePrevNextController.js.map