Skip to content

Commit 9c63204

Browse files
authored
Merge pull request #9196 from marmelab/fix-updatebutton-mutationoptions
Fix UpdateButton with custom notification doesn't close the confirmation dialog
2 parents 8857e55 + 18f5843 commit 9c63204

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

docs/UpdateButton.md

-5
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,11 @@ import { useNotify, useRefresh, useRedirect, TopToolbar, UpdateButton } from 're
182182

183183
const PostEditActions = () => {
184184
const notify = useNotify();
185-
const refresh = useRefresh();
186185
const redirect = useRedirect();
187186

188187
const onSuccess = () => {
189188
notify(`Changes saved`);
190189
redirect('/posts');
191-
refresh();
192190
};
193191

194192
return (
@@ -212,7 +210,6 @@ The default `onSuccess` function is:
212210
messageArgs: { smart_count: 1 },
213211
undoable: mutationMode === 'undoable'
214212
});
215-
refresh();
216213
}
217214
```
218215

@@ -225,13 +222,11 @@ import { useNotify, useRefresh, useRedirect, TopToolbar, UpdateButton } from 're
225222

226223
const PostEditActions = () => {
227224
const notify = useNotify();
228-
const refresh = useRefresh();
229225
const redirect = useRedirect();
230226

231227
const onSuccess = (data) => {
232228
notify(`Changes to post "${data.title}" saved`);
233229
redirect('/posts');
234-
refresh();
235230
};
236231

237232
return (

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

+42-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import polyglotI18nProvider from 'ra-i18n-polyglot';
33
import englishMessages from 'ra-language-english';
44
import frenchMessages from 'ra-language-french';
5-
import { Resource, withLifecycleCallbacks } from 'ra-core';
5+
import { Resource, useNotify, withLifecycleCallbacks } from 'ra-core';
66
import fakeRestDataProvider from 'ra-data-fakerest';
77
import { createMemoryHistory } from 'history';
88

@@ -161,6 +161,47 @@ export const Optimistic = () => (
161161
</AdminContext>
162162
);
163163

164+
const PostShowMutationOptions = () => {
165+
const notify = useNotify();
166+
return (
167+
<Show
168+
actions={
169+
<TopToolbar>
170+
<UpdateButton
171+
mutationMode="pessimistic"
172+
label="Reset views"
173+
data={{ views: 0 }}
174+
mutationOptions={{
175+
onSuccess: () => {
176+
notify('Reset views success');
177+
},
178+
}}
179+
/>
180+
</TopToolbar>
181+
}
182+
>
183+
<SimpleShowLayout>
184+
<TextField source="id" />
185+
<TextField source="title" />
186+
<TextField source="body" />
187+
<NumberField source="views" />
188+
</SimpleShowLayout>
189+
</Show>
190+
);
191+
};
192+
193+
export const MutationOptions = () => (
194+
<AdminContext
195+
dataProvider={getDataProvider()}
196+
i18nProvider={i18nProvider}
197+
history={createMemoryHistory({ initialEntries: ['/posts/1/show'] })}
198+
>
199+
<AdminUI>
200+
<Resource name="posts" show={<PostShowMutationOptions />} />
201+
</AdminUI>
202+
</AdminContext>
203+
);
204+
164205
const PostShowSx = () => {
165206
return (
166207
<Show

packages/ra-ui-materialui/src/button/UpdateWithConfirmButton.spec.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Toolbar, SimpleForm } from '../form';
99
import { Edit } from '../detail';
1010
import { TextInput } from '../input';
1111
import { Notification } from '../layout';
12+
import { MutationOptions } from './UpdateButton.stories';
1213

1314
const theme = createTheme();
1415

@@ -242,4 +243,19 @@ describe('<UpdateWithConfirmButton />', () => {
242243
);
243244
});
244245
});
246+
247+
it('should close the dialog even with custom success side effect', async () => {
248+
render(<MutationOptions />);
249+
fireEvent.click(await screen.findByLabelText('Reset views'));
250+
await screen.findByText('Are you sure you want to update this post?');
251+
fireEvent.click(screen.getByText('Confirm'));
252+
await screen.findByText('Reset views success', undefined, {
253+
timeout: 2000,
254+
});
255+
// wait until next tick, as the settled side effect is called after the success side effect
256+
await waitFor(() => new Promise(resolve => setTimeout(resolve, 300)));
257+
expect(
258+
screen.queryByText('Are you sure you want to update this post?')
259+
).toBeNull();
260+
});
245261
});

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export const UpdateWithConfirmButton = (
4848
messageArgs: { smart_count: 1 },
4949
undoable: mutationMode === 'undoable',
5050
});
51-
setOpen(false);
5251
},
5352
onError = (error: Error | string) => {
5453
notify(
@@ -67,6 +66,8 @@ export const UpdateWithConfirmButton = (
6766
},
6867
}
6968
);
69+
},
70+
onSettled = () => {
7071
setOpen(false);
7172
},
7273
...otherMutationOptions
@@ -90,6 +91,7 @@ export const UpdateWithConfirmButton = (
9091
{
9192
onSuccess,
9293
onError,
94+
onSettled,
9395
mutationMode,
9496
...otherMutationOptions,
9597
}

0 commit comments

Comments
 (0)