import { ReduxState, OnSuccess, OnFailure } from '../types';
export interface DataProviderQuery {
type: string;
resource: string;
payload: object;
}
export declare type Refetch = () => void;
export interface UseQueryWithStoreValue {
data?: any;
total?: number;
error?: any;
loading: boolean;
loaded: boolean;
refetch: Refetch;
}
export interface QueryOptions {
onSuccess?: OnSuccess;
onFailure?: OnFailure;
action?: string;
enabled?: boolean;
[key: string]: any;
}
/**
* Fetch the data provider through Redux, return the value from the store.
*
* The return value updates according to the request state:
*
* - start: { loading: true, loaded: false, refetch }
* - success: { data: [data from response], total: [total from response], loading: false, loaded: true, refetch }
* - error: { error: [error from response], loading: false, loaded: false, refetch }
*
* This hook will return the cached result when called a second time
* with the same parameters, until the response arrives.
*
* @param {Object} query
* @param {string} query.type The verb passed to th data provider, e.g. 'getList', 'getOne'
* @param {string} query.resource A resource name, e.g. 'posts', 'comments'
* @param {Object} query.payload The payload object, e.g; { post_id: 12 }
* @param {Object} options
* @param {string} options.action Redux action type
* @param {boolean} options.enabled Flag to conditionally run the query. If it's false, the query will not run
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. () => refresh()
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. (error) => notify(error.message)
* @param {Function} dataSelector Redux selector to get the result. Required.
* @param {Function} totalSelector Redux selector to get the total (optional, only for LIST queries)
* @param {Function} isDataLoaded
*
* @returns The current request state. Destructure as { data, total, error, loading, loaded, refetch }.
*
* @example
*
* import { useQueryWithStore } from '../app';
*
* const UserProfile = ({ record }) => {
* const { data, loading, error } = useQueryWithStore(
* {
* type: 'getOne',
* resource: 'users',
* payload: { id: record.id }
* },
* {},
* state => state.admin.resources.users.data[record.id]
* );
* if (loading) { return
ERROR
; } * return