Can delete a device relay from UI
This commit is contained in:
		| @@ -1,14 +1,32 @@ | ||||
| import AddIcon from "@mui/icons-material/Add"; | ||||
| import { IconButton, Tooltip } from "@mui/material"; | ||||
| import DeleteIcon from "@mui/icons-material/Delete"; | ||||
| import EditIcon from "@mui/icons-material/Edit"; | ||||
| import { | ||||
|   IconButton, | ||||
|   ListItem, | ||||
|   ListItemText, | ||||
|   Tooltip, | ||||
|   Typography, | ||||
| } from "@mui/material"; | ||||
| import React from "react"; | ||||
| import { Device, DeviceRelay } from "../../api/DeviceApi"; | ||||
| import { DeviceRouteCard } from "./DeviceRouteCard"; | ||||
| import { EditDeviceRelaysDialog } from "../../dialogs/EditDeviceRelaysDialog"; | ||||
| import { DeviceRouteCard } from "./DeviceRouteCard"; | ||||
| import { useConfirm } from "../../hooks/context_providers/ConfirmDialogProvider"; | ||||
| import { useLoadingMessage } from "../../hooks/context_providers/LoadingMessageProvider"; | ||||
| import { RelayApi } from "../../api/RelayApi"; | ||||
| import { useSnackbar } from "../../hooks/context_providers/SnackbarProvider"; | ||||
| import { useAlert } from "../../hooks/context_providers/AlertDialogProvider"; | ||||
|  | ||||
| export function DeviceRelays(p: { | ||||
|   device: Device; | ||||
|   onReload: () => void; | ||||
| }): React.ReactElement { | ||||
|   const confirm = useConfirm(); | ||||
|   const loadingMessage = useLoadingMessage(); | ||||
|   const snackbar = useSnackbar(); | ||||
|   const alert = useAlert(); | ||||
|  | ||||
|   const [dialogOpen, setDialogOpen] = React.useState(false); | ||||
|   const [currRelay, setCurrRelay] = React.useState<DeviceRelay | undefined>(); | ||||
|  | ||||
| @@ -17,6 +35,25 @@ export function DeviceRelays(p: { | ||||
|     setCurrRelay(undefined); | ||||
|   }; | ||||
|  | ||||
|   const deleteRelay = async (r: DeviceRelay) => { | ||||
|     if ( | ||||
|       !(await confirm("Do you really want to delete this relay configuration?")) | ||||
|     ) | ||||
|       return; | ||||
|  | ||||
|     try { | ||||
|       await RelayApi.Delete(r); | ||||
|  | ||||
|       p.onReload(); | ||||
|       snackbar("The relay configuration was successfully deleted!"); | ||||
|     } catch (e) { | ||||
|       console.error("Failed to delete relay!", e); | ||||
|       alert(`Failed to delete device relay configuration! ${e}`); | ||||
|     } finally { | ||||
|       loadingMessage.hide(); | ||||
|     } | ||||
|   }; | ||||
|  | ||||
|   return ( | ||||
|     <> | ||||
|       {dialogOpen && ( | ||||
| @@ -43,7 +80,39 @@ export function DeviceRelays(p: { | ||||
|           </Tooltip> | ||||
|         } | ||||
|       > | ||||
|         TODO : relays list ({p.device.relays.length}) relays now) | ||||
|         {p.device.relays.length === 0 ? ( | ||||
|           <Typography style={{ textAlign: "center" }}> | ||||
|             No relay configured yet. | ||||
|           </Typography> | ||||
|         ) : ( | ||||
|           <></> | ||||
|         )} | ||||
|  | ||||
|         {p.device.relays.map((r, i) => ( | ||||
|           <ListItem | ||||
|             alignItems="flex-start" | ||||
|             key={r.id} | ||||
|             secondaryAction={ | ||||
|               <> | ||||
|                 <Tooltip title="Edit the relay configuration"> | ||||
|                   <IconButton> | ||||
|                     <EditIcon /> | ||||
|                   </IconButton> | ||||
|                 </Tooltip> | ||||
|  | ||||
|                 {i === p.device.relays.length - 1 && ( | ||||
|                   <Tooltip title="Delete the relay configuration"> | ||||
|                     <IconButton onClick={() => deleteRelay(r)}> | ||||
|                       <DeleteIcon /> | ||||
|                     </IconButton> | ||||
|                   </Tooltip> | ||||
|                 )} | ||||
|               </> | ||||
|             } | ||||
|           > | ||||
|             <ListItemText primary={r.name} secondary={"TODO: status"} /> | ||||
|           </ListItem> | ||||
|         ))} | ||||
|       </DeviceRouteCard> | ||||
|     </> | ||||
|   ); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user