@@ -9,18 +9,14 @@ import {
99import {
1010 useListContext ,
1111 useTranslate ,
12- useUpdateMany ,
13- useNotify ,
14- useUnselectAll ,
1512 useResourceContext ,
16- type MutationMode ,
1713 type RaRecord ,
18- type UpdateManyParams ,
14+ useBulkUpdateController ,
15+ UseBulkUpdateControllerParams ,
1916} from 'ra-core' ;
2017
2118import { Confirm } from '../layout' ;
2219import { Button , type ButtonProps } from './Button' ;
23- import type { UseMutationOptions } from '@tanstack/react-query' ;
2420import { humanize , inflect } from 'inflection' ;
2521
2622export const BulkUpdateWithConfirmButton = (
@@ -30,10 +26,8 @@ export const BulkUpdateWithConfirmButton = (
3026 props : inProps ,
3127 name : PREFIX ,
3228 } ) ;
33- const notify = useNotify ( ) ;
3429 const translate = useTranslate ( ) ;
3530 const resource = useResourceContext ( props ) ;
36- const unselectAll = useUnselectAll ( resource ) ;
3731 const [ isOpen , setOpen ] = useState ( false ) ;
3832 const { selectedIds } = useListContext ( ) ;
3933
@@ -45,54 +39,12 @@ export const BulkUpdateWithConfirmButton = (
4539 label = 'ra.action.update' ,
4640 mutationMode = 'pessimistic' ,
4741 onClick,
48- onSuccess = ( ) => {
49- notify ( `resources.${ resource } .notifications.updated` , {
50- type : 'info' ,
51- messageArgs : {
52- smart_count : selectedIds . length ,
53- _ : translate ( 'ra.notification.updated' , {
54- smart_count : selectedIds . length ,
55- } ) ,
56- } ,
57- undoable : mutationMode === 'undoable' ,
58- } ) ;
59- unselectAll ( ) ;
60- setOpen ( false ) ;
61- } ,
62- onError = ( error : Error | string ) => {
63- notify (
64- typeof error === 'string'
65- ? error
66- : error . message || 'ra.notification.http_error' ,
67- {
68- type : 'error' ,
69- messageArgs : {
70- _ :
71- typeof error === 'string'
72- ? error
73- : error && error . message
74- ? error . message
75- : undefined ,
76- } ,
77- }
78- ) ;
79- setOpen ( false ) ;
80- } ,
81- mutationOptions = { } ,
8242 ...rest
8343 } = props ;
84- const { meta : mutationMeta , ...otherMutationOptions } = mutationOptions ;
85-
86- const [ updateMany , { isPending } ] = useUpdateMany (
87- resource ,
88- { ids : selectedIds , data, meta : mutationMeta } ,
89- {
90- onSuccess,
91- onError,
92- mutationMode,
93- ...otherMutationOptions ,
94- }
95- ) ;
44+ const { handleUpdate, isPending } = useBulkUpdateController ( {
45+ ...rest ,
46+ mutationMode,
47+ } ) ;
9648
9749 const handleClick = e => {
9850 setOpen ( true ) ;
@@ -103,8 +55,9 @@ export const BulkUpdateWithConfirmButton = (
10355 setOpen ( false ) ;
10456 } ;
10557
106- const handleUpdate = e => {
107- updateMany ( ) ;
58+ const handleConfirm = e => {
59+ setOpen ( false ) ;
60+ handleUpdate ( data ) ;
10861
10962 if ( typeof onClick === 'function' ) {
11063 onClick ( e ) ;
@@ -155,7 +108,7 @@ export const BulkUpdateWithConfirmButton = (
155108 ) ,
156109 } ) ,
157110 } }
158- onConfirm = { handleUpdate }
111+ onConfirm = { handleConfirm }
159112 onClose = { handleDialogClose }
160113 />
161114 </ Fragment >
@@ -164,30 +117,20 @@ export const BulkUpdateWithConfirmButton = (
164117
165118const sanitizeRestProps = ( {
166119 label,
167- onSuccess ,
168- onError ,
120+ resource ,
121+ successMessage ,
169122 ...rest
170- } : Omit <
171- BulkUpdateWithConfirmButtonProps ,
172- 'resource' | 'selectedIds' | 'icon' | 'data'
173- > ) => rest ;
123+ } : Omit < BulkUpdateWithConfirmButtonProps , 'icon' | 'data' > ) => rest ;
174124
175125export interface BulkUpdateWithConfirmButtonProps <
176126 RecordType extends RaRecord = any ,
177127 MutationOptionsError = unknown ,
178- > extends ButtonProps {
128+ > extends ButtonProps ,
129+ UseBulkUpdateControllerParams < RecordType , MutationOptionsError > {
179130 confirmContent ?: React . ReactNode ;
180131 confirmTitle ?: React . ReactNode ;
181132 icon ?: React . ReactNode ;
182133 data : any ;
183- onSuccess ?: ( ) => void ;
184- onError ?: ( error : any ) => void ;
185- mutationMode ?: MutationMode ;
186- mutationOptions ?: UseMutationOptions <
187- RecordType ,
188- MutationOptionsError ,
189- UpdateManyParams < RecordType >
190- > & { meta ?: any } ;
191134}
192135
193136const PREFIX = 'RaBulkUpdateWithConfirmButton' ;
0 commit comments