ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
67 lines • 2.46 kB
JavaScript
import { useCallback, useMemo } from 'react';
import { useDeleteMany, } from "../../dataProvider/useDeleteMany.js";
import { useRefresh } from "../../dataProvider/useRefresh.js";
import { useListContext } from "../list/useListContext.js";
import { useNotify } from "../../notification/useNotify.js";
import { useResourceContext } from "../../core/useResourceContext.js";
import { useTranslate } from "../../i18n/useTranslate.js";
export const useBulkDeleteController = (props) => {
const { mutationMode = 'undoable', mutationOptions = {}, successMessage, } = props;
const { meta: mutationMeta, ...otherMutationOptions } = mutationOptions;
const resource = useResourceContext(props);
const notify = useNotify();
const refresh = useRefresh();
const translate = useTranslate();
const { selectedIds, onUnselectItems } = useListContext();
const [deleteMany, { isPending }] = useDeleteMany(resource, undefined, {
onSuccess: () => {
notify(successMessage ??
`resources.${resource}.notifications.deleted`, {
type: 'info',
messageArgs: {
smart_count: selectedIds.length,
_: translate('ra.notification.deleted', {
smart_count: selectedIds.length,
}),
},
undoable: mutationMode === 'undoable',
});
onUnselectItems(true);
},
onError: (error) => {
notify(typeof error === 'string'
? error
: error?.message || 'ra.notification.http_error', {
type: 'error',
messageArgs: {
_: typeof error === 'string'
? error
: error?.message,
},
});
refresh();
},
});
const handleDelete = useCallback(() => {
deleteMany(resource, {
ids: selectedIds,
meta: mutationMeta,
}, {
mutationMode,
...otherMutationOptions,
});
}, [
deleteMany,
mutationMeta,
mutationMode,
otherMutationOptions,
resource,
selectedIds,
]);
return useMemo(() => ({
isPending,
isLoading: isPending,
handleDelete,
}), [isPending, handleDelete]);
};
//# sourceMappingURL=useBulkDeleteController.js.map