ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
35 lines • 1.61 kB
JavaScript
import { useCallback } from 'react';
import { useResourceContext } from '../core/useResourceContext';
import { useListContext } from '../controller/list/useListContext';
import { useDataProvider } from '../dataProvider/useDataProvider';
import { useNotify } from '../notification/useNotify';
import { fetchRelatedRecords } from './fetchRelatedRecords';
/**
* A hook that provides a callback to export the selected records from the nearest ListContext and call the exporter function for them.
*/
export function useBulkExport(options) {
if (options === void 0) { options = {}; }
var customExporter = options.exporter, meta = options.meta;
var resource = useResourceContext(options);
var _a = useListContext(), exporterFromContext = _a.exporter, selectedIds = _a.selectedIds;
var exporter = customExporter || exporterFromContext;
var dataProvider = useDataProvider();
var notify = useNotify();
return useCallback(function () {
if (exporter && resource) {
dataProvider
.getMany(resource, { ids: selectedIds, meta: meta })
.then(function (_a) {
var data = _a.data;
return exporter(data, fetchRelatedRecords(dataProvider), dataProvider, resource);
})
.catch(function (error) {
console.error(error);
notify('ra.notification.http_error', {
type: 'error',
});
});
}
}, [dataProvider, exporter, notify, resource, selectedIds, meta]);
}
//# sourceMappingURL=useBulkExport.js.map