Skip to content

Commit d6debe2

Browse files
committed
Fix useBulkUpdateController mutationOptions handling
1 parent c6bef1d commit d6debe2

File tree

3 files changed

+82
-39
lines changed

3 files changed

+82
-39
lines changed

packages/ra-core/src/controller/button/useBulkUpdateController.tsx

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ export const useBulkUpdateController = <
2020
onSuccess,
2121
onError,
2222
mutationMode = 'undoable',
23-
mutationOptions = {
24-
onSuccess,
25-
onError,
26-
},
23+
mutationOptions = {},
2724
successMessage,
2825
} = props;
2926
const { meta: mutationMeta, ...otherMutationOptions } = mutationOptions;
@@ -37,40 +34,44 @@ export const useBulkUpdateController = <
3734
resource,
3835
undefined,
3936
{
40-
onSuccess: () => {
41-
notify(
42-
successMessage ??
43-
`resources.${resource}.notifications.updated`,
44-
{
45-
type: 'info',
46-
messageArgs: {
47-
smart_count: selectedIds.length,
48-
_: translate('ra.notification.updated', {
37+
onSuccess:
38+
onSuccess ??
39+
(() => {
40+
notify(
41+
successMessage ??
42+
`resources.${resource}.notifications.updated`,
43+
{
44+
type: 'info',
45+
messageArgs: {
4946
smart_count: selectedIds.length,
50-
}),
51-
},
52-
undoable: mutationMode === 'undoable',
53-
}
54-
);
55-
onUnselectItems();
56-
},
57-
onError: (error: any) => {
58-
notify(
59-
typeof error === 'string'
60-
? error
61-
: error?.message || 'ra.notification.http_error',
62-
{
63-
type: 'error',
64-
messageArgs: {
65-
_:
66-
typeof error === 'string'
67-
? error
68-
: error?.message,
69-
},
70-
}
71-
);
72-
refresh();
73-
},
47+
_: translate('ra.notification.updated', {
48+
smart_count: selectedIds.length,
49+
}),
50+
},
51+
undoable: mutationMode === 'undoable',
52+
}
53+
);
54+
onUnselectItems();
55+
}),
56+
onError:
57+
onError ??
58+
((error: any) => {
59+
notify(
60+
typeof error === 'string'
61+
? error
62+
: error?.message || 'ra.notification.http_error',
63+
{
64+
type: 'error',
65+
messageArgs: {
66+
_:
67+
typeof error === 'string'
68+
? error
69+
: error?.message,
70+
},
71+
}
72+
);
73+
refresh();
74+
}),
7475
...otherMutationOptions,
7576
}
7677
);

packages/ra-ui-materialui/src/button/BulkUpdateButton.stories.tsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import polyglotI18nProvider from 'ra-i18n-polyglot';
33
import englishMessages from 'ra-language-english';
4-
import { Resource, TestMemoryRouter } from 'ra-core';
4+
import { Resource, TestMemoryRouter, useNotify } from 'ra-core';
55
import fakeRestDataProvider from 'ra-data-fakerest';
66
import { BulkUpdateButton } from './BulkUpdateButton';
77
import { AdminContext } from '../AdminContext';
@@ -166,6 +166,47 @@ export const MutationMode = () => (
166166
/>
167167
);
168168

169+
const MutationOptionsButtons = () => {
170+
const notify = useNotify();
171+
return (
172+
<>
173+
<BulkUpdateButton
174+
label="Update Undoable"
175+
data={{ reads: 0 }}
176+
mutationMode="undoable"
177+
mutationOptions={{
178+
onSuccess: () => {
179+
notify('Updated successfully', { undoable: true });
180+
},
181+
}}
182+
/>
183+
<BulkUpdateButton
184+
label="Update Optimistic"
185+
data={{ reads: 0 }}
186+
mutationMode="optimistic"
187+
mutationOptions={{
188+
onSuccess: () => {
189+
notify('Updated successfully');
190+
},
191+
}}
192+
/>
193+
<BulkUpdateButton
194+
label="Update Pessimistic"
195+
data={{ reads: 0 }}
196+
mutationMode="pessimistic"
197+
mutationOptions={{
198+
onSuccess: () => {
199+
notify('Updated successfully');
200+
},
201+
}}
202+
/>
203+
</>
204+
);
205+
};
206+
export const MutationOptions = () => (
207+
<Wrapper bulkActionButtons={<MutationOptionsButtons />} />
208+
);
209+
169210
export const Themed = () => (
170211
<Wrapper
171212
bulkActionButtons={<BulkUpdateButton data={{ reads: 0 }} />}

packages/ra-ui-materialui/src/button/BulkUpdateWithUndoButton.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export const BulkUpdateWithUndoButton = (
3131

3232
const { handleUpdate, isPending } = useBulkUpdateController(rest);
3333

34-
const handleClick = e => {
34+
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
35+
e.stopPropagation();
3536
handleUpdate(data);
3637
if (typeof onClick === 'function') {
3738
onClick(e);

0 commit comments

Comments
 (0)