forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseReferenceOneFieldController.tsx
106 lines (101 loc) · 3.22 KB
/
useReferenceOneFieldController.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import get from 'lodash/get';
import { UseQueryOptions } from 'react-query';
import { useGetManyReference } from '../../dataProvider';
import { useNotify } from '../../notification';
import { RaRecord, SortPayload } from '../../types';
import { UseReferenceResult } from '../useReference';
export interface UseReferenceOneFieldControllerParams<
RecordType extends RaRecord = any
> {
record?: RaRecord;
reference: string;
source?: string;
target: string;
sort?: SortPayload;
filter?: any;
queryOptions?: UseQueryOptions<{
data: RecordType[];
total: number;
}> & { meta?: any };
}
/**
* Fetch a reference record in a one-to-one relationship, and return it when available
*
* The reference prop should be the name of one of the <Resource> components
* added as <Admin> child.
*
* @example
*
* const { data, isLoading, error } = useReferenceOneFieldController({
* record: { id: 7, name: 'James Joyce'}
* reference: 'bios',
* target: 'author_id',
* });
*
* @typedef {Object} UseReferenceOneFieldControllerParams
* @prop {Object} props.record The current resource record
* @prop {string} props.reference The linked resource name
* @prop {string} props.target The target resource key
* @prop {string} props.source The key current record identifier ('id' by default)
* @prop {Object} props.sort The sort to apply to the referenced records
* @prop {Object} props.filter The filter to apply to the referenced records
* @returns {UseReferenceResult} The request state. Destructure as { referenceRecord, isLoading, error }.
*/
export const useReferenceOneFieldController = <
RecordType extends RaRecord = any
>(
props: UseReferenceOneFieldControllerParams<RecordType>
): UseReferenceResult<RecordType> => {
const {
reference,
record,
target,
source = 'id',
sort = { field: 'id', order: 'ASC' },
filter = {},
queryOptions = {},
} = props;
const notify = useNotify();
const { meta, ...otherQueryOptions } = queryOptions;
const { data, error, isFetching, isLoading, refetch } = useGetManyReference<
RecordType
>(
reference,
{
target,
id: get(record, source),
pagination: { page: 1, perPage: 1 },
sort,
filter,
meta,
},
{
enabled: !!record,
onError: error =>
notify(
typeof error === 'string'
? error
: error.message || 'ra.notification.http_error',
{
type: 'error',
messageArgs: {
_:
typeof error === 'string'
? error
: error && error.message
? error.message
: undefined,
},
}
),
...otherQueryOptions,
}
);
return {
referenceRecord: data ? data[0] : undefined,
error,
isFetching,
isLoading,
refetch,
};
};