| 
									
										
										
										
											2024-02-03 15:56:33 +03:00
										 |  |  | import React, { 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"; | 
					
						
							| 
									
										
										
										
											2023-02-04 18:57:37 +03:00
										 |  |  | import { alpha, useTheme } from "@mui/material/styles"; | 
					
						
							| 
									
										
										
										
											2020-07-08 12:11:24 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const DeviceRemoveButton = props => { | 
					
						
							| 
									
										
										
										
											2023-02-04 18:57:37 +03:00
										 |  |  |   const theme = useTheme(); | 
					
						
							| 
									
										
										
										
											2023-01-24 18:36:08 +03:00
										 |  |  |   const record = useRecordContext(); | 
					
						
							| 
									
										
										
										
											2020-07-08 12:11:24 +03:00
										 |  |  |   const [open, setOpen] = useState(false); | 
					
						
							|  |  |  |   const refresh = useRefresh(); | 
					
						
							|  |  |  |   const notify = useNotify(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-08 11:24:02 +03:00
										 |  |  |   const [removeDevice, { isLoading }] = useDelete(); | 
					
						
							| 
									
										
										
										
											2020-07-08 12:11:24 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!record) return null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleClick = () => setOpen(true); | 
					
						
							|  |  |  |   const handleDialogClose = () => setOpen(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleConfirm = () => { | 
					
						
							|  |  |  |     removeDevice( | 
					
						
							| 
									
										
										
										
											2023-02-08 11:24:02 +03:00
										 |  |  |       "devices", | 
					
						
							|  |  |  |       { id: record.id, meta: { 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 ( | 
					
						
							| 
									
										
										
										
											2024-02-03 15:56:33 +03:00
										 |  |  |     <> | 
					
						
							| 
									
										
										
										
											2020-07-08 12:11:24 +03:00
										 |  |  |       <Button | 
					
						
							|  |  |  |         label="ra.action.remove" | 
					
						
							|  |  |  |         onClick={handleClick} | 
					
						
							| 
									
										
										
										
											2023-02-04 18:57:37 +03:00
										 |  |  |         sx={{ | 
					
						
							|  |  |  |           color: theme.palette.error.main, | 
					
						
							|  |  |  |           "&:hover": { | 
					
						
							|  |  |  |             backgroundColor: alpha(theme.palette.error.main, 0.12), | 
					
						
							|  |  |  |             // Reset on mouse devices
 | 
					
						
							|  |  |  |             "@media (hover: none)": { | 
					
						
							|  |  |  |               backgroundColor: "transparent", | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }} | 
					
						
							| 
									
										
										
										
											2020-07-08 12:11:24 +03:00
										 |  |  |       > | 
					
						
							|  |  |  |         <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, | 
					
						
							|  |  |  |         }} | 
					
						
							|  |  |  |       /> | 
					
						
							| 
									
										
										
										
											2024-02-03 15:56:33 +03:00
										 |  |  |     </> | 
					
						
							| 
									
										
										
										
											2020-07-08 12:11:24 +03:00
										 |  |  |   ); | 
					
						
							|  |  |  | }; |