import { MutationMode, OnSuccess, OnFailure } from '../types'; import { DeclarativeSideEffect } from './useDeclarativeSideEffects'; /** * Get a callback to fetch the data provider through Redux, usually for mutations. * * The request starts when the callback is called. * * useMutation() parameters can be passed: * * - at definition time * * const [mutate] = useMutation(query, options); mutate(); * * - at call time * * const [mutate] = useMutation(); mutate(query, options); * * - both, in which case the definition and call time parameters are merged * * const [mutate] = useMutation(query1, options1); mutate(query2, options2); * * @param {Object} query * @param {string} query.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} 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.undoable Set to true to run the mutation locally before calling the dataProvider * @param {boolean} options.returnPromise Set to true to return the result promise of the mutation * @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 {boolean} options.withDeclarativeSideEffectsSupport Set to true to support legacy side effects e.g. { onSuccess: { refresh: true } } * * @returns A tuple with the mutation callback and the request state. Destructure as [mutate, { data, total, error, loading, loaded }]. * * The return value updates according to the request state: * * - mount: [mutate, { loading: false, loaded: false }] * - mutate called: [mutate, { loading: true, loaded: false }] * - success: [mutate, { data: [data from response], total: [total from response], loading: false, loaded: true }] * - error: [mutate, { error: [error from response], loading: false, loaded: false }] * * The mutate function accepts the following arguments * - {Object} query * - {string} query.type The method called on the data provider, e.g. 'update' * - {string} query.resource A resource name, e.g. 'posts', 'comments' * - {Object} query.payload The payload object, e.g. { id: 123, data: { isApproved: true } } * - {Object} options * - {string} options.action Redux action type * - {boolean} options.undoable Set to true to run the mutation locally before calling the dataProvider * - {boolean} options.returnPromise Set to true to return the result promise of the mutation * - {Function} options.onSuccess Side effect function to be executed upon success or failure, e.g. { onSuccess: response => refresh() } * - {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) } * - {boolean} withDeclarativeSideEffectsSupport Set to true to support legacy side effects e.g. { onSuccess: { refresh: true } } * * @example * * // pass parameters at definition time * // use when all parameters are determined at definition time * // the mutation callback can be used as an even handler * // because Event parameters are ignored * import { useMutation } from '../app'; * * const ApproveButton = ({ record }) => { * const [approve, { loading }] = useMutation({ * type: 'update', * resource: 'comments', * payload: { id: record.id, data: { isApproved: true } } * }); * return