Skip to content

Commit c1e4bea

Browse files
committed
Apply code review
1 parent ecfc59c commit c1e4bea

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback } from 'react';
1+
import { useCallback, useMemo } from 'react';
22
import { UseMutationOptions } from '@tanstack/react-query';
33

44
import { useDelete } from '../../dataProvider';
@@ -10,34 +10,31 @@ import { useResourceContext } from '../../core';
1010
import { useTranslate } from '../../i18n';
1111

1212
/**
13-
* Prepare a set of callbacks for a delete button guarded by confirmation dialog
13+
* Prepare a set of callbacks for a delete button
1414
*
1515
* @example
16-
*
1716
* const DeleteButton = ({
1817
* resource,
1918
* record,
2019
* redirect,
21-
* onClick,
2220
* ...rest
2321
* }) => {
2422
* const {
25-
* open,
2623
* isPending,
27-
* handleDialogOpen,
28-
* handleDialogClose,
2924
* handleDelete,
30-
* } = useDeleteWithConfirmController({
25+
* } = useDeleteController({
26+
* mutationMode: 'pessimistic',
3127
* resource,
3228
* record,
3329
* redirect,
34-
* onClick,
3530
* });
3631
*
32+
* const [open, setOpen] = useState(false);
33+
*
3734
* return (
3835
* <Fragment>
3936
* <Button
40-
* onClick={handleDialogOpen}
37+
* onClick={() => setOpen(true)}
4138
* label="ra.action.delete"
4239
* {...rest}
4340
* >
@@ -56,8 +53,8 @@ import { useTranslate } from '../../i18n';
5653
* name: resource,
5754
* id: record.id,
5855
* }}
59-
* onConfirm={handleDelete}
60-
* onClose={handleDialogClose}
56+
* onConfirm={() => handleDelete()}
57+
* onClose={() => setOpen(false)}
6158
* />
6259
* </Fragment>
6360
* );
@@ -69,13 +66,13 @@ export const useDeleteController = <
6966
>(
7067
props: UseDeleteControllerParams<RecordType, ErrorType>
7168
): UseDeleteControllerReturn => {
72-
const { mutationMode } = props;
7369
const {
7470
record,
7571
redirect: redirectTo = 'list',
72+
mutationMode,
7673
mutationOptions = {},
7774
successMessage,
78-
} = props as UseDeleteControllerParams<RecordType, ErrorType>;
75+
} = props;
7976
const { meta: mutationMeta, ...otherMutationOptions } = mutationOptions;
8077
const resource = useResourceContext(props);
8178
const notify = useNotify();
@@ -154,11 +151,14 @@ export const useDeleteController = <
154151
resource,
155152
]);
156153

157-
return {
158-
isPending,
159-
isLoading: isPending,
160-
handleDelete,
161-
};
154+
return useMemo(
155+
() => ({
156+
isPending,
157+
isLoading: isPending,
158+
handleDelete,
159+
}),
160+
[isPending, handleDelete]
161+
);
162162
};
163163

164164
export interface UseDeleteControllerParams<

0 commit comments

Comments
 (0)