-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4858 from marmelab/delete-button-hooks
Add ability to create custom DeleteButton views without rewriting the logic
- Loading branch information
Showing
7 changed files
with
282 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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,4 @@ | ||
import useDeleteWithUndoController from './useDeleteWithUndoController'; | ||
import useDeleteWithConfirmController from './useDeleteWithConfirmController'; | ||
|
||
export { useDeleteWithUndoController, useDeleteWithConfirmController }; |
140 changes: 140 additions & 0 deletions
140
packages/ra-core/src/controller/button/useDeleteWithConfirmController.tsx
This file contains 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,140 @@ | ||
import { | ||
useState, | ||
useCallback, | ||
ReactEventHandler, | ||
SyntheticEvent, | ||
} from 'react'; | ||
import { useDelete } from '../../dataProvider'; | ||
import { CRUD_DELETE } from '../../actions'; | ||
import { | ||
useRefresh, | ||
useNotify, | ||
useRedirect, | ||
RedirectionSideEffect, | ||
} from '../../sideEffect'; | ||
import { Record } from '../../types'; | ||
|
||
/** | ||
* Prepare a set of callbacks for a delete button guarded by confirmation dialog | ||
* | ||
* @example | ||
* | ||
* const DeleteButton = ({ | ||
* resource, | ||
* record, | ||
* basePath, | ||
* redirect, | ||
* onClick, | ||
* ...rest | ||
* }) => { | ||
* const { | ||
* open, | ||
* loading, | ||
* handleDialogOpen, | ||
* handleDialogClose, | ||
* handleDelete, | ||
* } = useDeleteWithConfirmController({ | ||
* resource, | ||
* record, | ||
* redirect, | ||
* basePath, | ||
* onClick, | ||
* }); | ||
* | ||
* return ( | ||
* <Fragment> | ||
* <Button | ||
* onClick={handleDialogOpen} | ||
* label="ra.action.delete" | ||
* {...rest} | ||
* > | ||
* {icon} | ||
* </Button> | ||
* <Confirm | ||
* isOpen={open} | ||
* loading={loading} | ||
* title="ra.message.delete_title" | ||
* content="ra.message.delete_content" | ||
* translateOptions={{ | ||
* name: resource, | ||
* id: record.id, | ||
* }} | ||
* onConfirm={handleDelete} | ||
* onClose={handleDialogClose} | ||
* /> | ||
* </Fragment> | ||
* ); | ||
* }; | ||
*/ | ||
const useDeleteWithConfirmController = ({ | ||
resource, | ||
record, | ||
redirect: redirectTo, | ||
basePath, | ||
onClick, | ||
}: UseDeleteWithConfirmControllerParams): UseDeleteWithConfirmControllerReturn => { | ||
const [open, setOpen] = useState(false); | ||
const notify = useNotify(); | ||
const redirect = useRedirect(); | ||
const refresh = useRefresh(); | ||
const [deleteOne, { loading }] = useDelete(resource, null, null, { | ||
action: CRUD_DELETE, | ||
onSuccess: () => { | ||
notify('ra.notification.deleted', 'info', { smart_count: 1 }); | ||
redirect(redirectTo, basePath); | ||
refresh(); | ||
}, | ||
onFailure: error => { | ||
notify( | ||
typeof error === 'string' | ||
? error | ||
: error.message || 'ra.notification.http_error', | ||
'warning' | ||
); | ||
setOpen(false); | ||
}, | ||
undoable: false, | ||
}); | ||
|
||
const handleDialogOpen = e => { | ||
setOpen(true); | ||
e.stopPropagation(); | ||
}; | ||
|
||
const handleDialogClose = e => { | ||
setOpen(false); | ||
e.stopPropagation(); | ||
}; | ||
|
||
const handleDelete = useCallback( | ||
event => { | ||
deleteOne({ | ||
payload: { id: record.id, previousData: record }, | ||
}); | ||
if (typeof onClick === 'function') { | ||
onClick(event); | ||
} | ||
}, | ||
[deleteOne, onClick, record] | ||
); | ||
|
||
return { open, loading, handleDialogOpen, handleDialogClose, handleDelete }; | ||
}; | ||
|
||
export interface UseDeleteWithConfirmControllerParams { | ||
basePath?: string; | ||
record?: Record; | ||
redirect?: RedirectionSideEffect; | ||
resource: string; | ||
onClick?: ReactEventHandler<any>; | ||
} | ||
|
||
export interface UseDeleteWithConfirmControllerReturn { | ||
open: boolean; | ||
loading: boolean; | ||
handleDialogOpen: (e: SyntheticEvent) => void; | ||
handleDialogClose: (e: SyntheticEvent) => void; | ||
handleDelete: ReactEventHandler<any>; | ||
} | ||
|
||
export default useDeleteWithConfirmController; |
105 changes: 105 additions & 0 deletions
105
packages/ra-core/src/controller/button/useDeleteWithUndoController.tsx
This file contains 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,105 @@ | ||
import { useCallback, ReactEventHandler } from 'react'; | ||
import { useDelete } from '../../dataProvider'; | ||
import { CRUD_DELETE } from '../../actions'; | ||
import { | ||
useRefresh, | ||
useNotify, | ||
useRedirect, | ||
RedirectionSideEffect, | ||
} from '../../sideEffect'; | ||
import { Record } from '../../types'; | ||
|
||
/** | ||
* Prepare callback for a Delete button with undo support | ||
* | ||
* @example | ||
* | ||
* import React from 'react'; | ||
* import ActionDelete from '@material-ui/icons/Delete'; | ||
* import { Button, useDeleteWithUndoController } from 'react-admin'; | ||
* | ||
* const DeleteButton = ({ | ||
* resource, | ||
* record, | ||
* basePath, | ||
* redirect, | ||
* onClick, | ||
* ...rest | ||
* }) => { | ||
* const { loading, handleDelete } = useDeleteWithUndoController({ | ||
* resource, | ||
* record, | ||
* basePath, | ||
* redirect, | ||
* onClick, | ||
* }); | ||
* | ||
* return ( | ||
* <Button | ||
* onClick={handleDelete} | ||
* disabled={loading} | ||
* label="ra.action.delete" | ||
* {...rest} | ||
* > | ||
* <ActionDelete /> | ||
* </Button> | ||
* ); | ||
* }; | ||
*/ | ||
const useDeleteWithUndoController = ({ | ||
resource, | ||
record, | ||
basePath, | ||
redirect: redirectTo = 'list', | ||
onClick, | ||
}: UseDeleteWithUndoControllerParams): UseDeleteWithUndoControllerReturn => { | ||
const notify = useNotify(); | ||
const redirect = useRedirect(); | ||
const refresh = useRefresh(); | ||
|
||
const [deleteOne, { loading }] = useDelete(resource, null, null, { | ||
action: CRUD_DELETE, | ||
onSuccess: () => { | ||
notify('ra.notification.deleted', 'info', { smart_count: 1 }, true); | ||
redirect(redirectTo, basePath); | ||
refresh(); | ||
}, | ||
onFailure: error => | ||
notify( | ||
typeof error === 'string' | ||
? error | ||
: error.message || 'ra.notification.http_error', | ||
'warning' | ||
), | ||
undoable: true, | ||
}); | ||
const handleDelete = useCallback( | ||
event => { | ||
event.stopPropagation(); | ||
deleteOne({ | ||
payload: { id: record.id, previousData: record }, | ||
}); | ||
if (typeof onClick === 'function') { | ||
onClick(event); | ||
} | ||
}, | ||
[deleteOne, onClick, record] | ||
); | ||
|
||
return { loading, handleDelete }; | ||
}; | ||
|
||
export interface UseDeleteWithUndoControllerParams { | ||
basePath?: string; | ||
record?: Record; | ||
redirect?: RedirectionSideEffect; | ||
resource: string; | ||
onClick?: ReactEventHandler<any>; | ||
} | ||
|
||
export interface UseDeleteWithUndoControllerReturn { | ||
loading: boolean; | ||
handleDelete: ReactEventHandler<any>; | ||
} | ||
|
||
export default useDeleteWithUndoController; |
This file contains 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 |
---|---|---|
|
@@ -52,3 +52,4 @@ export { | |
|
||
export * from './field'; | ||
export * from './input'; | ||
export * from './button'; |
This file contains 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
This file contains 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
Oops, something went wrong.