ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
99 lines • 4.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGetOne = void 0;
const react_query_1 = require("@tanstack/react-query");
const useDataProvider_1 = require("./useDataProvider.cjs");
const react_1 = require("react");
const util_1 = require("../util/index.cjs");
/**
* Call the dataProvider.getOne() method and return the resolved value
* as well as the loading state.
*
* The return value updates according to the request state:
*
* - start: { isPending: true, isFetching: true, refetch }
* - success: { data: [data from response], isPending: false, refetch }
* - error: { error: [error from response], isPending: false, refetch }
*
* This hook will return the cached result when called a second time
* with the same parameters, until the response arrives.
*
* @param resource The resource name, e.g. 'posts'
* @param {Params} params The getOne parameters { id, meta }, e.g. { id: 123 }
* @param {Options} options Options object to pass to the react-query queryClient.
*
* @typedef Params
* @prop id a resource identifier, e.g. 123
*
* @typedef Options
* @prop enabled Flag to conditionally run the query. If it's false, the query will not run
* @prop onSuccess Side effect function to be executed upon success, e.g. { onSuccess: { refresh: true } }
* @prop onError Side effect function to be executed upon failure, e.g. { onError: error => notify(error.message) }
*
* @returns The current request state. Destructure as { data, error, isPending, refetch }.
*
* @example
*
* import { useGetOne, useRecordContext } from 'react-admin';
*
* const UserProfile = () => {
* const record = useRecordContext();
* const { data, isPending, error } = useGetOne('users', { id: record.id });
* if (isPending) { return <Loading />; }
* if (error) { return <p>ERROR</p>; }
* return <div>User {data.username}</div>;
* };
*/
const useGetOne = (resource, { id, meta }, options = {}) => {
const dataProvider = (0, useDataProvider_1.useDataProvider)();
const { onError = noop, onSuccess = noop, onSettled = noop, enabled, ...queryOptions } = options;
const onSuccessEvent = (0, util_1.useEvent)(onSuccess);
const onErrorEvent = (0, util_1.useEvent)(onError);
const onSettledEvent = (0, util_1.useEvent)(onSettled);
const result = (0, react_query_1.useQuery)({
// Sometimes the id comes as a string (e.g. when read from the URL in a Show view).
// Sometimes the id comes as a number (e.g. when read from a Record in useGetList response).
// As the react-query cache is type-sensitive, we always stringify the identifier to get a match
queryKey: [resource, 'getOne', { id: String(id), meta }],
queryFn: queryParams => id == null
? Promise.reject('useGetOne: id cannot be null')
: dataProvider
.getOne(resource, {
id,
meta,
signal: dataProvider.supportAbortSignal === true
? queryParams.signal
: undefined,
})
.then(({ data }) => data),
enabled: enabled ?? id != null,
...queryOptions,
});
(0, react_1.useEffect)(() => {
if (result.data === undefined ||
result.error != null ||
result.isFetching)
return;
onSuccessEvent(result.data);
}, [onSuccessEvent, result.data, result.error, result.isFetching]);
(0, react_1.useEffect)(() => {
if (result.error == null || result.isFetching)
return;
onErrorEvent(result.error);
}, [onErrorEvent, result.error, result.isFetching]);
(0, react_1.useEffect)(() => {
if (result.status === 'pending' || result.isFetching)
return;
onSettledEvent(result.data, result.error);
}, [
onSettledEvent,
result.data,
result.error,
result.status,
result.isFetching,
]);
return result;
};
exports.useGetOne = useGetOne;
const noop = () => undefined;
//# sourceMappingURL=useGetOne.js.map