1- import { useEffect , useMemo } from 'react ' ;
1+ import { RaRecord , GetOneParams , GetOneResult } from '../types ' ;
22import {
33 useQuery ,
4- useQueryClient ,
54 UseQueryOptions ,
65 UseQueryResult ,
76} from '@tanstack/react-query' ;
8-
9- import { RaRecord , GetOneParams , GetOneResult } from '../types' ;
107import { useDataProvider } from './useDataProvider' ;
11- import { populateQueryCache } from './populateQueryCache ' ;
8+ import { useEffect } from 'react ' ;
129import { useEvent } from '../util' ;
1310
1411/**
@@ -56,7 +53,6 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
5653 options : UseGetOneOptions < RecordType , ErrorType > = { }
5754) : UseGetOneHookValue < RecordType , ErrorType > => {
5855 const dataProvider = useDataProvider ( ) ;
59- const queryClient = useQueryClient ( ) ;
6056 const {
6157 onError = noop ,
6258 onSuccess = noop ,
@@ -68,7 +64,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
6864 const onErrorEvent = useEvent ( onError ) ;
6965 const onSettledEvent = useEvent ( onSettled ) ;
7066
71- const result = useQuery < GetOneResult < RecordType > , ErrorType > ( {
67+ const result = useQuery < RecordType , ErrorType > ( {
7268 // Sometimes the id comes as a string (e.g. when read from the URL in a Show view).
7369 // Sometimes the id comes as a number (e.g. when read from a Record in useGetList response).
7470 // As the react-query cache is type-sensitive, we always stringify the identifier to get a match
@@ -85,7 +81,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
8581 ? queryParams . signal
8682 : undefined ,
8783 } )
88- . then ( ( { data, meta } ) => ( { data, meta } ) ) ,
84+ . then ( ( { data } ) => data ) ,
8985 enabled : enabled ?? id != null ,
9086 ...queryOptions ,
9187 } ) ;
@@ -97,8 +93,8 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
9793 result . isFetching
9894 )
9995 return ;
100- onSuccessEvent ( result . data . data ) ;
101- } , [ onSuccessEvent , result . data ?. data , result . error , result . isFetching ] ) ;
96+ onSuccessEvent ( result . data ) ;
97+ } , [ onSuccessEvent , result . data , result . error , result . isFetching ] ) ;
10298
10399 useEffect ( ( ) => {
104100 if ( result . error == null || result . isFetching ) return ;
@@ -107,36 +103,16 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
107103
108104 useEffect ( ( ) => {
109105 if ( result . status === 'pending' || result . isFetching ) return ;
110- onSettledEvent ( result . data ?. data , result . error ) ;
106+ onSettledEvent ( result . data , result . error ) ;
111107 } , [
112108 onSettledEvent ,
113- result . data ?. data ,
109+ result . data ,
114110 result . error ,
115111 result . status ,
116112 result . isFetching ,
117113 ] ) ;
118114
119- useEffect ( ( ) => {
120- if ( result . data ?. meta ?. prefetched ) {
121- populateQueryCache ( {
122- data : result . data ?. meta . prefetched ,
123- queryClient,
124- } ) ;
125- }
126- } , [ result . data ?. meta , queryClient ] ) ;
127-
128- return useMemo (
129- ( ) =>
130- result . data
131- ? {
132- ...result ,
133- ...result . data ,
134- }
135- : result ,
136- [ result ]
137- ) as UseQueryResult < RecordType , ErrorType > & {
138- meta ?: any ;
139- } ;
115+ return result ;
140116} ;
141117
142118const noop = ( ) => undefined ;
@@ -145,7 +121,7 @@ export type UseGetOneOptions<
145121 RecordType extends RaRecord = any ,
146122 ErrorType = Error ,
147123> = Omit <
148- UseQueryOptions < GetOneResult < RecordType > , ErrorType > ,
124+ UseQueryOptions < GetOneResult < RecordType > [ 'data' ] , ErrorType > ,
149125 'queryKey' | 'queryFn'
150126> & {
151127 onSuccess ?: ( data : GetOneResult < RecordType > [ 'data' ] ) => void ;
0 commit comments