1- import { RaRecord , GetOneParams , GetOneResult } from '../types ' ;
1+ import { useEffect , useMemo } from 'react ' ;
22import {
33 useQuery ,
4+ useQueryClient ,
45 UseQueryOptions ,
56 UseQueryResult ,
67} from '@tanstack/react-query' ;
8+
9+ import { RaRecord , GetOneParams , GetOneResult } from '../types' ;
710import { useDataProvider } from './useDataProvider' ;
8- import { useEffect } from 'react ' ;
11+ import { populateQueryCache } from './populateQueryCache ' ;
912import { useEvent } from '../util' ;
1013
1114/**
@@ -53,6 +56,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
5356 options : UseGetOneOptions < RecordType , ErrorType > = { }
5457) : UseGetOneHookValue < RecordType , ErrorType > => {
5558 const dataProvider = useDataProvider ( ) ;
59+ const queryClient = useQueryClient ( ) ;
5660 const {
5761 onError = noop ,
5862 onSuccess = noop ,
@@ -64,7 +68,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
6468 const onErrorEvent = useEvent ( onError ) ;
6569 const onSettledEvent = useEvent ( onSettled ) ;
6670
67- const result = useQuery < RecordType , ErrorType > ( {
71+ const result = useQuery < GetOneResult < RecordType > , ErrorType > ( {
6872 // Sometimes the id comes as a string (e.g. when read from the URL in a Show view).
6973 // Sometimes the id comes as a number (e.g. when read from a Record in useGetList response).
7074 // As the react-query cache is type-sensitive, we always stringify the identifier to get a match
@@ -81,7 +85,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
8185 ? queryParams . signal
8286 : undefined ,
8387 } )
84- . then ( ( { data } ) => data ) ,
88+ . then ( ( { data, meta } ) => ( { data, meta } ) ) ,
8589 enabled : enabled ?? id != null ,
8690 ...queryOptions ,
8791 } ) ;
@@ -93,8 +97,8 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
9397 result . isFetching
9498 )
9599 return ;
96- onSuccessEvent ( result . data ) ;
97- } , [ onSuccessEvent , result . data , result . error , result . isFetching ] ) ;
100+ onSuccessEvent ( result . data . data ) ;
101+ } , [ onSuccessEvent , result . data ?. data , result . error , result . isFetching ] ) ;
98102
99103 useEffect ( ( ) => {
100104 if ( result . error == null || result . isFetching ) return ;
@@ -103,16 +107,36 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
103107
104108 useEffect ( ( ) => {
105109 if ( result . status === 'pending' || result . isFetching ) return ;
106- onSettledEvent ( result . data , result . error ) ;
110+ onSettledEvent ( result . data ?. data , result . error ) ;
107111 } , [
108112 onSettledEvent ,
109- result . data ,
113+ result . data ?. data ,
110114 result . error ,
111115 result . status ,
112116 result . isFetching ,
113117 ] ) ;
114118
115- return result ;
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+ } ;
116140} ;
117141
118142const noop = ( ) => undefined ;
@@ -121,7 +145,7 @@ export type UseGetOneOptions<
121145 RecordType extends RaRecord = any ,
122146 ErrorType = Error ,
123147> = Omit <
124- UseQueryOptions < GetOneResult < RecordType > [ 'data' ] , ErrorType > ,
148+ UseQueryOptions < GetOneResult < RecordType > , ErrorType > ,
125149 'queryKey' | 'queryFn'
126150> & {
127151 onSuccess ?: ( data : GetOneResult < RecordType > [ 'data' ] ) => void ;
0 commit comments