Skip to content

Commit ebc0f3b

Browse files
committed
Update UpdateWithConfirmButton
1 parent 3623f3f commit ebc0f3b

File tree

1 file changed

+11
-73
lines changed

1 file changed

+11
-73
lines changed

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

Lines changed: 11 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,17 @@ import {
99
} from '@mui/material/styles';
1010
import {
1111
useTranslate,
12-
useNotify,
1312
useResourceContext,
14-
type MutationMode,
1513
type RaRecord,
16-
type UpdateParams,
1714
useRecordContext,
18-
useUpdate,
1915
useGetRecordRepresentation,
2016
useResourceTranslation,
17+
useUpdateController,
18+
UseUpdateControllerParams,
2119
} from 'ra-core';
2220

2321
import { Confirm } from '../layout';
2422
import { Button, type ButtonProps } from './Button';
25-
import type { UseMutationOptions } from '@tanstack/react-query';
2623
import { humanize, singularize } from 'inflection';
2724

2825
export const UpdateWithConfirmButton = <
@@ -35,7 +32,6 @@ export const UpdateWithConfirmButton = <
3532
props: inProps,
3633
name: PREFIX,
3734
});
38-
const notify = useNotify();
3935
const translate = useTranslate();
4036
const resource = useResourceContext(props);
4137
const [isOpen, setOpen] = useState(false);
@@ -49,62 +45,14 @@ export const UpdateWithConfirmButton = <
4945
label: labelProp,
5046
mutationMode = 'pessimistic',
5147
onClick,
52-
mutationOptions = emptyObject as UseMutationOptions<
53-
RecordType,
54-
MutationOptionsError,
55-
UpdateParams<RecordType>
56-
> & { meta?: any },
5748
titleTranslateOptions = emptyObject,
5849
contentTranslateOptions = emptyObject,
5950
...rest
6051
} = props;
61-
const {
62-
meta: mutationMeta,
63-
onSuccess = () => {
64-
notify(`resources.${resource}.notifications.updated`, {
65-
type: 'info',
66-
messageArgs: {
67-
smart_count: 1,
68-
_: translate('ra.notification.updated', { smart_count: 1 }),
69-
},
70-
undoable: mutationMode === 'undoable',
71-
});
72-
},
73-
onError = (error: MutationOptionsError) => {
74-
notify(
75-
typeof error === 'string'
76-
? error
77-
: error.message || 'ra.notification.http_error',
78-
{
79-
type: 'error',
80-
messageArgs: {
81-
_:
82-
typeof error === 'string'
83-
? error
84-
: error && error.message
85-
? error.message
86-
: undefined,
87-
},
88-
}
89-
);
90-
},
91-
onSettled = () => {
92-
setOpen(false);
93-
},
94-
...otherMutationOptions
95-
} = mutationOptions;
96-
97-
const [update, { isPending }] = useUpdate<RecordType, MutationOptionsError>(
98-
resource,
99-
{ id: record?.id, data, meta: mutationMeta, previousData: record },
100-
{
101-
onSuccess,
102-
onError,
103-
onSettled,
104-
mutationMode,
105-
...otherMutationOptions,
106-
}
107-
);
52+
const { handleUpdate, isPending } = useUpdateController({
53+
...rest,
54+
mutationMode,
55+
});
10856

10957
const handleClick = (e: React.MouseEvent) => {
11058
setOpen(true);
@@ -115,13 +63,8 @@ export const UpdateWithConfirmButton = <
11563
setOpen(false);
11664
};
11765

118-
const handleUpdate = e => {
119-
update(resource, {
120-
id: record?.id,
121-
data,
122-
meta: mutationMeta,
123-
previousData: record,
124-
});
66+
const handleConfirm = e => {
67+
handleUpdate();
12568

12669
if (typeof onClick === 'function') {
12770
onClick(e);
@@ -195,7 +138,7 @@ export const UpdateWithConfirmButton = <
195138
loading={isPending}
196139
title={<>{confirmTitle}</>}
197140
content={<>{confirmContent}</>}
198-
onConfirm={handleUpdate}
141+
onConfirm={handleConfirm}
199142
onClose={handleDialogClose}
200143
/>
201144
</Fragment>
@@ -213,17 +156,12 @@ const sanitizeRestProps = ({
213156
export interface UpdateWithConfirmButtonProps<
214157
RecordType extends RaRecord = any,
215158
MutationOptionsError extends Error = Error,
216-
> extends ButtonProps {
159+
> extends ButtonProps,
160+
UseUpdateControllerParams<RecordType, MutationOptionsError> {
217161
confirmContent?: React.ReactNode;
218162
confirmTitle?: React.ReactNode;
219163
icon?: React.ReactNode;
220164
data: any;
221-
mutationMode?: MutationMode;
222-
mutationOptions?: UseMutationOptions<
223-
RecordType,
224-
MutationOptionsError,
225-
UpdateParams<RecordType>
226-
> & { meta?: any };
227165
titleTranslateOptions?: object;
228166
contentTranslateOptions?: object;
229167
}

0 commit comments

Comments
 (0)