|
| 1 | +import { Button, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle } from "@mui/material"; |
| 2 | +import { Fragment, useState } from "react"; |
| 3 | +import { SimpleForm, BooleanInput, useTranslate, RaRecord, useNotify, useRedirect, useDelete, NotificationType, useDeleteMany, Identifier, useUnselectAll } from "react-admin"; |
| 4 | +import ActionDelete from "@mui/icons-material/Delete"; |
| 5 | +import ActionCheck from "@mui/icons-material/CheckCircle"; |
| 6 | +import AlertError from "@mui/icons-material/ErrorOutline"; |
| 7 | + |
| 8 | +interface DeleteUserButtonProps { |
| 9 | + selectedIds: Identifier[]; |
| 10 | + confirmTitle: string; |
| 11 | + confirmContent: string; |
| 12 | +} |
| 13 | + |
| 14 | +const resourceName = "users"; |
| 15 | + |
| 16 | +const DeleteUserButton: React.FC<DeleteUserButtonProps> = (props) => { |
| 17 | + const translate = useTranslate(); |
| 18 | + const [open, setOpen] = useState(false); |
| 19 | + const [deleteMedia, setDeleteMedia] = useState(false); |
| 20 | + const [redactEvents, setRedactEvents] = useState(false); |
| 21 | + |
| 22 | + const notify = useNotify(); |
| 23 | + const redirect = useRedirect(); |
| 24 | + |
| 25 | + const [deleteMany, { isLoading }] = useDeleteMany(); |
| 26 | + const unselectAll = useUnselectAll(resourceName); |
| 27 | + const recordIds = props.selectedIds; |
| 28 | + |
| 29 | + const handleDialogOpen = () => setOpen(true); |
| 30 | + const handleDialogClose = () => setOpen(false); |
| 31 | + |
| 32 | + const handleDelete = (values: {deleteMedia: boolean, redactEvents: boolean}) => { |
| 33 | + deleteMany( |
| 34 | + resourceName, |
| 35 | + { ids: recordIds, meta: values }, |
| 36 | + { |
| 37 | + onSuccess: () => { |
| 38 | + handleDialogClose(); |
| 39 | + unselectAll(); |
| 40 | + redirect("/users"); |
| 41 | + }, |
| 42 | + onError: (error) => |
| 43 | + notify("ra.notification.data_provider_error", { type: 'error' as NotificationType }), |
| 44 | + } |
| 45 | + ); |
| 46 | + }; |
| 47 | + |
| 48 | + const handleConfirm = () => { |
| 49 | + setOpen(false); |
| 50 | + handleDelete({ deleteMedia: deleteMedia, redactEvents: redactEvents }); |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <Fragment> |
| 55 | + <Button |
| 56 | + onClick={handleDialogOpen} |
| 57 | + disabled={isLoading} |
| 58 | + className={"ra-delete-button"} |
| 59 | + key="button" |
| 60 | + size="small" |
| 61 | + sx={{ |
| 62 | + "&.MuiButton-sizeSmall": { |
| 63 | + lineHeight: 1.5, |
| 64 | + }, |
| 65 | + }} |
| 66 | + color={"error"} |
| 67 | + startIcon={<ActionDelete />} |
| 68 | + > |
| 69 | + {translate("ra.action.delete")} |
| 70 | + </Button> |
| 71 | + <Dialog open={open} onClose={handleDialogClose}> |
| 72 | + <DialogTitle>{translate(props.confirmTitle)}</DialogTitle> |
| 73 | + <DialogContent> |
| 74 | + <DialogContentText>{translate(props.confirmContent)}</DialogContentText> |
| 75 | + <SimpleForm toolbar={false}> |
| 76 | + <BooleanInput |
| 77 | + source="deleteMedia" |
| 78 | + value={deleteMedia} |
| 79 | + onChange={(event: React.ChangeEvent<HTMLInputElement>) => setDeleteMedia(event.target.checked)} |
| 80 | + label="resources.users.action.delete_media" |
| 81 | + defaultValue={false} |
| 82 | + /> |
| 83 | + <BooleanInput |
| 84 | + source="redactEvents" |
| 85 | + value={redactEvents} |
| 86 | + onChange={(event: React.ChangeEvent<HTMLInputElement>) => setRedactEvents(event.target.checked)} |
| 87 | + label="resources.users.action.redact_events" |
| 88 | + defaultValue={false} |
| 89 | + /> |
| 90 | + </SimpleForm> |
| 91 | + </DialogContent> |
| 92 | + <DialogActions> |
| 93 | + <Button disabled={false} onClick={handleDialogClose} startIcon={<AlertError />}> |
| 94 | + {translate("ra.action.cancel")} |
| 95 | + </Button> |
| 96 | + <Button |
| 97 | + disabled={false} |
| 98 | + onClick={handleConfirm} |
| 99 | + className={"ra-confirm RaConfirm-confirmPrimary"} |
| 100 | + autoFocus |
| 101 | + startIcon={<ActionCheck />} |
| 102 | + > |
| 103 | + {translate("ra.action.confirm")} |
| 104 | + </Button> |
| 105 | + </DialogActions> |
| 106 | + </Dialog> |
| 107 | + </Fragment> |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +export default DeleteUserButton; |
0 commit comments