forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuery.tsx
85 lines (81 loc) · 2.83 KB
/
Query.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
import { FunctionComponent } from 'react';
import { useQuery } from './useQuery';
interface ChildrenFuncParams {
data?: any;
total?: number;
loading: boolean;
loaded: boolean;
error?: any;
}
export interface QueryProps {
children: (params: ChildrenFuncParams) => JSX.Element;
type: string;
resource?: string;
payload?: any;
options?: any;
}
/**
* Fetch the data provider and pass the result to a child function
*
* @param {Function} children Must be a function which will be called with an object containing the following keys: data, loading and error
* @param {string} type The method called on the data provider, e.g. 'getList', 'getOne'. Can also be a custom method if the dataProvider supports is.
* @param {string} resource A resource name, e.g. 'posts', 'comments'
* @param {Object} payload The payload object, e.g; { post_id: 12 }
* @param {Object} options
* @param {string} options.action Redux action type
* @param {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() }
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) }
*
* This component also supports legacy side effects (e.g. { onSuccess: { refresh: true } })
*
* @example
*
* const UserProfile = ({ record }) => (
* <Query type="getOne" resource="users" payload={{ id: record.id }}>
* {({ data, loading, error }) => {
* if (loading) { return <Loading />; }
* if (error) { return <p>ERROR</p>; }
* return <div>User {data.username}</div>;
* }}
* </Query>
* );
*
* @example
*
* const payload = {
* pagination: { page: 1, perPage: 10 },
* sort: { field: 'username', order: 'ASC' },
* };
* const UserList = () => (
* <Query type="getList" resource="users" payload={payload}>
* {({ data, total, loading, error }) => {
* if (loading) { return <Loading />; }
* if (error) { return <p>ERROR</p>; }
* return (
* <div>
* <p>Total users: {total}</p>
* <ul>
* {data.map(user => <li key={user.username}>{user.username}</li>)}
* </ul>
* </div>
* );
* }}
* </Query>
* );
*/
const Query: FunctionComponent<QueryProps> = ({
children,
type,
resource,
payload,
// Provides an undefined onSuccess just so the key `onSuccess` is defined
// This is used to detect options in useDataProvider
options = { onSuccess: undefined },
}) =>
children(
useQuery(
{ type, resource, payload },
{ ...options, withDeclarativeSideEffectsSupport: true }
)
);
export default Query;