2020-07-08 12:11:24 +03:00
|
|
|
import React, { Fragment, useState } from "react";
|
2023-01-24 18:36:08 +03:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
useDelete,
|
|
|
|
useNotify,
|
|
|
|
Confirm,
|
|
|
|
useRecordContext,
|
|
|
|
useRefresh,
|
|
|
|
} from "react-admin";
|
2023-01-24 23:27:02 +03:00
|
|
|
import ActionDelete from "@mui/icons-material/Delete";
|
2020-07-08 12:11:24 +03:00
|
|
|
import { makeStyles } from "@material-ui/core/styles";
|
2023-01-24 23:27:02 +03:00
|
|
|
import { alpha } from "@mui/material/styles";
|
2020-07-08 12:11:24 +03:00
|
|
|
import classnames from "classnames";
|
|
|
|
|
|
|
|
const useStyles = makeStyles(
|
|
|
|
theme => ({
|
|
|
|
deleteButton: {
|
|
|
|
color: theme.palette.error.main,
|
|
|
|
"&:hover": {
|
2022-02-17 22:49:34 +03:00
|
|
|
backgroundColor: alpha(theme.palette.error.main, 0.12),
|
2020-07-08 12:11:24 +03:00
|
|
|
// Reset on mouse devices
|
|
|
|
"@media (hover: none)": {
|
|
|
|
backgroundColor: "transparent",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
{ name: "RaDeleteDeviceButton" }
|
|
|
|
);
|
|
|
|
|
|
|
|
export const DeviceRemoveButton = props => {
|
2023-01-24 18:36:08 +03:00
|
|
|
const record = useRecordContext();
|
2020-07-08 12:11:24 +03:00
|
|
|
const classes = useStyles(props);
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const refresh = useRefresh();
|
|
|
|
const notify = useNotify();
|
|
|
|
|
2023-01-24 17:24:14 +03:00
|
|
|
const [removeDevice, { isLoading }] = useDelete("devices");
|
2020-07-08 12:11:24 +03:00
|
|
|
|
|
|
|
if (!record) return null;
|
|
|
|
|
|
|
|
const handleClick = () => setOpen(true);
|
|
|
|
const handleDialogClose = () => setOpen(false);
|
|
|
|
|
|
|
|
const handleConfirm = () => {
|
|
|
|
removeDevice(
|
2023-01-24 17:24:14 +03:00
|
|
|
{ payload: { id: record.id, user_id: record.user_id } },
|
2020-07-08 12:11:24 +03:00
|
|
|
{
|
|
|
|
onSuccess: () => {
|
|
|
|
notify("resources.devices.action.erase.success");
|
|
|
|
refresh();
|
|
|
|
},
|
2023-01-24 17:24:14 +03:00
|
|
|
onFailure: () => {
|
|
|
|
notify("resources.devices.action.erase.failure", { type: "error" });
|
|
|
|
},
|
2020-07-08 12:11:24 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
setOpen(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<Button
|
|
|
|
label="ra.action.remove"
|
|
|
|
onClick={handleClick}
|
|
|
|
className={classnames("ra-delete-button", classes.deleteButton)}
|
|
|
|
>
|
|
|
|
<ActionDelete />
|
|
|
|
</Button>
|
|
|
|
<Confirm
|
|
|
|
isOpen={open}
|
2023-01-24 17:24:14 +03:00
|
|
|
loading={isLoading}
|
2020-07-08 12:11:24 +03:00
|
|
|
onConfirm={handleConfirm}
|
|
|
|
onClose={handleDialogClose}
|
|
|
|
title="resources.devices.action.erase.title"
|
|
|
|
content="resources.devices.action.erase.content"
|
|
|
|
translateOptions={{
|
|
|
|
id: record.id,
|
|
|
|
name: record.display_name ? record.display_name : record.id,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
};
|