-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Introduce useDeleteController
#10876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
357b557
Introduce `useDeleteController`
djhi ecfc59c
Modify DeleteWithConfirmButton tests to check dialog is closed
djhi c1e4bea
Apply code review
djhi 3ddcc80
Mark useDeleteWithUndoController and useDeleteWithConfirmController a…
djhi 59c6dca
Make sure to stop event propagation
djhi ca228a4
Ensure users can still override only parts of the mutationOptions
djhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
packages/ra-core/src/controller/button/useDeleteController.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { useCallback } from 'react'; | ||
| import { UseMutationOptions } from '@tanstack/react-query'; | ||
|
|
||
| import { useDelete } from '../../dataProvider'; | ||
| import { useUnselect } from '../'; | ||
| import { useRedirect, RedirectionSideEffect } from '../../routing'; | ||
| import { useNotify } from '../../notification'; | ||
| import { RaRecord, MutationMode, DeleteParams } from '../../types'; | ||
| import { useResourceContext } from '../../core'; | ||
| import { useTranslate } from '../../i18n'; | ||
|
|
||
| /** | ||
| * Prepare a set of callbacks for a delete button guarded by confirmation dialog | ||
| * | ||
| * @example | ||
| * | ||
| * const DeleteButton = ({ | ||
| * resource, | ||
| * record, | ||
| * redirect, | ||
| * onClick, | ||
| * ...rest | ||
| * }) => { | ||
| * const { | ||
| * open, | ||
| * isPending, | ||
| * handleDialogOpen, | ||
| * handleDialogClose, | ||
| * handleDelete, | ||
| * } = useDeleteWithConfirmController({ | ||
| * resource, | ||
| * record, | ||
| * redirect, | ||
| * onClick, | ||
| * }); | ||
| * | ||
| * return ( | ||
| * <Fragment> | ||
| * <Button | ||
| * onClick={handleDialogOpen} | ||
| * label="ra.action.delete" | ||
| * {...rest} | ||
| * > | ||
| * {icon} | ||
| * </Button> | ||
| * <Confirm | ||
| * isOpen={open} | ||
| * loading={isPending} | ||
| * title="ra.message.delete_title" | ||
| * content="ra.message.delete_content" | ||
| * titleTranslateOptions={{ | ||
| * name: resource, | ||
| * id: record.id, | ||
| * }} | ||
| * contentTranslateOptions={{ | ||
| * name: resource, | ||
| * id: record.id, | ||
| * }} | ||
| * onConfirm={handleDelete} | ||
| * onClose={handleDialogClose} | ||
| * /> | ||
| * </Fragment> | ||
| * ); | ||
| * }; | ||
| */ | ||
| export const useDeleteController = < | ||
| RecordType extends RaRecord = any, | ||
| ErrorType = Error, | ||
| >( | ||
| props: UseDeleteControllerParams<RecordType, ErrorType> | ||
| ): UseDeleteControllerReturn => { | ||
| const { mutationMode } = props; | ||
| const { | ||
| record, | ||
| redirect: redirectTo = 'list', | ||
| mutationOptions = {}, | ||
| successMessage, | ||
| } = props as UseDeleteControllerParams<RecordType, ErrorType>; | ||
slax57 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
slax57 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const { meta: mutationMeta, ...otherMutationOptions } = mutationOptions; | ||
| const resource = useResourceContext(props); | ||
| const notify = useNotify(); | ||
| const unselect = useUnselect(resource); | ||
| const redirect = useRedirect(); | ||
| const translate = useTranslate(); | ||
|
|
||
| const [deleteOne, { isPending }] = useDelete<RecordType, ErrorType>( | ||
| resource, | ||
| undefined, | ||
| { | ||
| onSuccess: () => { | ||
| notify( | ||
| successMessage ?? | ||
| `resources.${resource}.notifications.deleted`, | ||
| { | ||
| type: 'info', | ||
| messageArgs: { | ||
| smart_count: 1, | ||
| _: translate('ra.notification.deleted', { | ||
| smart_count: 1, | ||
| }), | ||
| }, | ||
| undoable: mutationMode === 'undoable', | ||
| } | ||
| ); | ||
| record && unselect([record.id]); | ||
| redirect(redirectTo, resource); | ||
| }, | ||
| onError: error => { | ||
| notify( | ||
| typeof error === 'string' | ||
| ? error | ||
| : (error as Error)?.message || | ||
| 'ra.notification.http_error', | ||
| { | ||
| type: 'error', | ||
| messageArgs: { | ||
| _: | ||
| typeof error === 'string' | ||
| ? error | ||
| : (error as Error)?.message | ||
| ? (error as Error).message | ||
| : undefined, | ||
| }, | ||
| } | ||
| ); | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| const handleDelete = useCallback(() => { | ||
| if (!record) { | ||
| throw new Error( | ||
| 'The record cannot be deleted because no record has been passed' | ||
| ); | ||
| } | ||
| deleteOne( | ||
| resource, | ||
| { | ||
| id: record.id, | ||
| previousData: record, | ||
| meta: mutationMeta, | ||
| }, | ||
| { | ||
| mutationMode, | ||
| ...otherMutationOptions, | ||
| } | ||
| ); | ||
| }, [ | ||
| deleteOne, | ||
| mutationMeta, | ||
| mutationMode, | ||
| otherMutationOptions, | ||
| record, | ||
| resource, | ||
| ]); | ||
|
|
||
| return { | ||
| isPending, | ||
| isLoading: isPending, | ||
| handleDelete, | ||
| }; | ||
| }; | ||
|
|
||
| export interface UseDeleteControllerParams< | ||
| RecordType extends RaRecord = any, | ||
| MutationOptionsError = unknown, | ||
| > { | ||
| mutationMode?: MutationMode; | ||
| mutationOptions?: UseMutationOptions< | ||
| RecordType, | ||
| MutationOptionsError, | ||
| DeleteParams<RecordType> | ||
| >; | ||
| record?: RecordType; | ||
| redirect?: RedirectionSideEffect; | ||
| resource?: string; | ||
| successMessage?: string; | ||
| } | ||
|
|
||
| export interface UseDeleteControllerReturn { | ||
| isLoading: boolean; | ||
| isPending: boolean; | ||
| handleDelete: () => void; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.