Skip to content

Commit 40a075d

Browse files
committed
Improve side effects handling depending on the mutationMode
1 parent ea502e2 commit 40a075d

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,40 @@ export const BulkUpdateWithConfirmButton = (
4444
const { handleUpdate, isPending } = useBulkUpdateController({
4545
...rest,
4646
mutationMode,
47+
mutationOptions: {
48+
...rest.mutationOptions,
49+
onSettled(data, error, variables, context) {
50+
// In pessimistic mode, we wait for the mutation to be completed (either successfully or with an error) before closing
51+
if (mutationMode === 'pessimistic') {
52+
setOpen(false);
53+
}
54+
rest.mutationOptions?.onSettled?.(
55+
data,
56+
error,
57+
variables,
58+
context
59+
);
60+
},
61+
},
4762
});
4863

49-
const handleClick = e => {
50-
setOpen(true);
64+
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
5165
e.stopPropagation();
66+
setOpen(true);
5267
};
5368

54-
const handleDialogClose = () => {
69+
const handleDialogClose = (e: React.MouseEvent) => {
70+
e.stopPropagation();
5571
setOpen(false);
5672
};
5773

58-
const handleConfirm = e => {
59-
setOpen(false);
74+
const handleConfirm = (e: React.MouseEvent<HTMLButtonElement>) => {
75+
e.stopPropagation();
76+
// We close the dialog immediately here for optimistic/undoable modes instead of in onSuccess/onError
77+
// to avoid reimplementing the default side effects
78+
if (mutationMode !== 'pessimistic') {
79+
setOpen(false);
80+
}
6081
handleUpdate(data);
6182

6283
if (typeof onClick === 'function') {

0 commit comments

Comments
 (0)