Skip to content

Commit 1819521

Browse files
committed
Improve side effects handling depending on the mutationMode
1 parent 487a25c commit 1819521

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CoreAdminContext,
44
testDataProvider,
55
ListContextProvider,
6+
MutationMode,
67
} from 'ra-core';
78
import { createTheme, ThemeProvider } from '@mui/material/styles';
89

@@ -13,7 +14,11 @@ export default { title: 'ra-ui-materialui/button/BulkDeleteWithConfirmButton' };
1314

1415
const theme = createTheme();
1516

16-
export const Default = () => {
17+
export const Default = ({
18+
mutationMode = 'pessimistic',
19+
}: {
20+
mutationMode?: MutationMode;
21+
}) => {
1722
const dataProvider = testDataProvider({
1823
deleteMany: async () => ({ data: [{ id: 123 }] as any }),
1924
});
@@ -31,11 +36,24 @@ export const Default = () => {
3136
>
3237
<BulkDeleteWithConfirmButton
3338
resource="books"
34-
mutationMode="pessimistic"
39+
mutationMode={mutationMode}
3540
/>
3641
<Notification />
3742
</ListContextProvider>
3843
</ThemeProvider>
3944
</CoreAdminContext>
4045
);
4146
};
47+
48+
Default.args = {
49+
mutationMode: 'pessimistic',
50+
};
51+
52+
Default.argTypes = {
53+
mutationMode: {
54+
options: ['pessimistic', 'optimistic', 'undoable'],
55+
control: {
56+
type: 'select',
57+
},
58+
},
59+
};

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,44 @@ export const BulkDeleteWithConfirmButton = (
4141
const { handleDelete, isPending } = useBulkDeleteController({
4242
mutationMode,
4343
...rest,
44+
mutationOptions: {
45+
...rest.mutationOptions,
46+
onSettled(data, error, variables, context) {
47+
// In pessimistic mode, we wait for the mutation to be completed (either successfully or with an error) before closing
48+
if (mutationMode === 'pessimistic') {
49+
setOpen(false);
50+
}
51+
rest.mutationOptions?.onSettled?.(
52+
data,
53+
error,
54+
variables,
55+
context
56+
);
57+
},
58+
},
4459
});
4560

4661
const [isOpen, setOpen] = useState(false);
4762
const resource = useResourceContext(props);
4863
const translate = useTranslate();
4964

50-
const handleClick = e => {
51-
setOpen(true);
65+
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
5266
e.stopPropagation();
67+
setOpen(true);
5368
};
5469

55-
const handleDialogClose = () => {
70+
const handleDialogClose = (e: React.MouseEvent) => {
71+
e.stopPropagation();
5672
setOpen(false);
5773
};
5874

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

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

0 commit comments

Comments
 (0)