ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
71 lines • 2.68 kB
JavaScript
import { useCallback, useMemo } from 'react';
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";
import { useUpdateMany, } from "../../dataProvider/useUpdateMany.js";
export const useBulkUpdateController = (props) => {
const { onSuccess, onError, 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 [updateMany, { isPending }] = useUpdateMany(resource, undefined, {
onSuccess: onSuccess ??
(() => {
notify(successMessage ??
`resources.${resource}.notifications.updated`, {
type: 'info',
messageArgs: {
smart_count: selectedIds.length,
_: translate('ra.notification.updated', {
smart_count: selectedIds.length,
}),
},
undoable: mutationMode === 'undoable',
});
onUnselectItems();
}),
onError: onError ??
((error) => {
notify(typeof error === 'string'
? error
: error?.message || 'ra.notification.http_error', {
type: 'error',
messageArgs: {
_: typeof error === 'string'
? error
: error?.message,
},
});
refresh();
}),
...otherMutationOptions,
});
const handleUpdate = useCallback((data) => {
updateMany(resource, {
data,
ids: selectedIds,
meta: mutationMeta,
}, {
mutationMode,
...otherMutationOptions,
});
}, [
updateMany,
mutationMeta,
mutationMode,
otherMutationOptions,
resource,
selectedIds,
]);
return useMemo(() => ({
isPending,
isLoading: isPending,
handleUpdate,
}), [isPending, handleUpdate]);
};
//# sourceMappingURL=useBulkUpdateController.js.map