From dd18f6264b1a5eb006b4b57951176f7da9cf4fe1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 22 May 2022 15:55:45 -0700 Subject: Reorganize collections --- modern/src/main/StatusCard.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'modern/src/main/StatusCard.js') diff --git a/modern/src/main/StatusCard.js b/modern/src/main/StatusCard.js index b470890e..d23bc8c3 100644 --- a/modern/src/main/StatusCard.js +++ b/modern/src/main/StatusCard.js @@ -1,5 +1,5 @@ import React, { useState } from 'react'; -import { useSelector } from 'react-redux'; +import { useDispatch, useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; import { makeStyles, Card, CardContent, Typography, CardActions, CardHeader, IconButton, Avatar, Table, TableBody, TableRow, TableCell, TableContainer, @@ -19,6 +19,8 @@ import dimensions from '../common/theme/dimensions'; import { useDeviceReadonly, useReadonly } from '../common/util/permissions'; import usePersistedState from '../common/util/usePersistedState'; import usePositionAttributes from '../common/attributes/usePositionAttributes'; +import { devicesActions } from '../store'; +import { useCatch } from '../reactHelper'; const useStyles = makeStyles((theme) => ({ card: { @@ -64,6 +66,7 @@ const StatusRow = ({ name, content }) => { const StatusCard = ({ deviceId, onClose }) => { const classes = useStyles(); const history = useHistory(); + const dispatch = useDispatch(); const t = useTranslation(); const readonly = useReadonly(); @@ -75,7 +78,19 @@ const StatusCard = ({ deviceId, onClose }) => { const positionAttributes = usePositionAttributes(t); const [positionItems] = usePersistedState('positionItems', ['speed', 'address', 'totalDistance', 'course']); - const [removeDialogShown, setRemoveDialogShown] = useState(false); + const [removing, setRemoving] = useState(false); + + const handleRemove = useCatch(async (removed) => { + if (removed) { + const response = await fetch('/api/devices'); + if (response.ok) { + dispatch(devicesActions.refresh(await response.json())); + } else { + throw Error(await response.text()); + } + } + setRemoving(false); + }); return ( <> @@ -131,17 +146,17 @@ const StatusCard = ({ deviceId, onClose }) => { history.push(`/settings/device/${deviceId}`)} disabled={deviceReadonly}> - setRemoveDialogShown(true)} disabled={deviceReadonly} className={classes.negative}> + setRemoving(true)} disabled={deviceReadonly} className={classes.negative}> )} setRemoveDialogShown(false)} + onResult={(removed) => handleRemove(removed)} /> ); -- cgit v1.2.3