Refresh commands when wifiscan, delete when delete

This commit is contained in:
bourquecharles
2021-05-24 17:21:14 -04:00
parent 3aeec449dd
commit a77916b058
4 changed files with 107 additions and 69 deletions

View File

@@ -7,7 +7,7 @@ import {
CModalBody, CModalBody,
CModalFooter, CModalFooter,
CSpinner, CSpinner,
CBadge CBadge,
} from '@coreui/react'; } from '@coreui/react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
@@ -20,11 +20,15 @@ const ConfirmModal = ({ show, toggle, action }) => {
if (haveResult) { if (haveResult) {
if (success) { if (success) {
return ( return (
<CBadge color="success" shape="pill">Success</CBadge> <CBadge color="success" shape="pill">
Success
</CBadge>
); );
} }
return ( return (
<CBadge color="danger" shape="pill">Error</CBadge> <CBadge color="danger" shape="pill">
Error
</CBadge>
); );
} }
if (loading) { if (loading) {
@@ -36,7 +40,7 @@ const ConfirmModal = ({ show, toggle, action }) => {
); );
} }
return 'Yes'; return 'Yes';
} };
const doAction = async () => { const doAction = async () => {
setLoading(true); setLoading(true);
@@ -44,8 +48,10 @@ const ConfirmModal = ({ show, toggle, action }) => {
setSuccess(result); setSuccess(result);
setHaveResult(true); setHaveResult(true);
setLoading(false); setLoading(false);
if(result) toggle(); if (result) {
toggle();
} }
};
useEffect(() => { useEffect(() => {
setLoading(false); setLoading(false);
@@ -62,11 +68,7 @@ const ConfirmModal = ({ show, toggle, action }) => {
<h6>Are you sure you want to delete this command? this action is not reversible.</h6> <h6>Are you sure you want to delete this command? this action is not reversible.</h6>
</CModalBody> </CModalBody>
<CModalFooter> <CModalFooter>
<CButton <CButton disabled={loading} color="primary" onClick={() => doAction()}>
disabled={loading}
color="primary"
onClick={() => doAction()}
>
{getButtonContent()} {getButtonContent()}
</CButton> </CButton>
<CButton color="secondary" onClick={toggle}> <CButton color="secondary" onClick={toggle}>

View File

@@ -22,6 +22,7 @@ import WifiScanResultModalWidget from './WifiScanResultModal';
import ConfirmModal from '../../components/ConfirmModal'; import ConfirmModal from '../../components/ConfirmModal';
import BlueResultIcon from '../../assets/icons/BlueResultsIcon.png'; import BlueResultIcon from '../../assets/icons/BlueResultsIcon.png';
import WhiteResultIcon from '../../assets/icons/WhiteResultsIcon.png'; import WhiteResultIcon from '../../assets/icons/WhiteResultsIcon.png';
import eventBus from '../../utils/EventBus';
const DeviceCommands = ({ selectedDeviceId }) => { const DeviceCommands = ({ selectedDeviceId }) => {
const [showScanModal, setShowScanModal] = useState(false); const [showScanModal, setShowScanModal] = useState(false);
@@ -31,6 +32,7 @@ const DeviceCommands = ({ selectedDeviceId }) => {
const [scanDate, setScanDate] = useState(''); const [scanDate, setScanDate] = useState('');
const [collapse, setCollapse] = useState(false); const [collapse, setCollapse] = useState(false);
const [details, setDetails] = useState([]); const [details, setDetails] = useState([]);
const [toDelete, setToDelete] = useState(null);
const [responses, setResponses] = useState([]); const [responses, setResponses] = useState([]);
const [commands, setCommands] = useState([]); const [commands, setCommands] = useState([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
@@ -46,7 +48,8 @@ const DeviceCommands = ({ selectedDeviceId }) => {
setShowScanModal(!showScanModal); setShowScanModal(!showScanModal);
}; };
const toggleConfirmModal = (uuid) => { const toggleConfirmModal = (uuid, index) => {
setToDelete(index);
setUuidDelete(uuid); setUuidDelete(uuid);
setShowConfirmModal(!showConfirmModal); setShowConfirmModal(!showConfirmModal);
}; };
@@ -59,6 +62,12 @@ const DeviceCommands = ({ selectedDeviceId }) => {
setEnd(value); setEnd(value);
}; };
const deleteCommandFromList = (index) => {
const newCommands = commands;
newCommands.splice(index, 1);
setCommands(newCommands);
}
const getCommands = () => { const getCommands = () => {
setLoading(true); setLoading(true);
const utcStart = new Date(start).toISOString(); const utcStart = new Date(start).toISOString();
@@ -86,6 +95,10 @@ const DeviceCommands = ({ selectedDeviceId }) => {
}); });
}; };
eventBus.on("actionCompleted", () =>
getCommands()
);
const deleteCommand = async () => { const deleteCommand = async () => {
if (uuidDelete === '') { if (uuidDelete === '') {
return false; return false;
@@ -96,18 +109,17 @@ const DeviceCommands = ({ selectedDeviceId }) => {
Authorization: `Bearer ${getToken()}`, Authorization: `Bearer ${getToken()}`,
}, },
}; };
return ( return axiosInstance
axiosInstance
.delete(`/command/${uuidDelete}`, options) .delete(`/command/${uuidDelete}`, options)
.then(() => { .then(() => {
deleteCommandFromList(toDelete);
setUuidDelete(''); setUuidDelete('');
return true; return true;
}) })
.catch(() => { .catch(() => {
setUuidDelete(''); setUuidDelete('');
return false; return false;
}) });
);
}; };
const toggleDetails = (item, index) => { const toggleDetails = (item, index) => {
@@ -174,8 +186,8 @@ const DeviceCommands = ({ selectedDeviceId }) => {
label: '', label: '',
sorter: false, sorter: false,
filter: false, filter: false,
_style: { width: '14%' } _style: { width: '14%' },
} },
]; ];
useEffect(() => { useEffect(() => {
@@ -189,6 +201,7 @@ const DeviceCommands = ({ selectedDeviceId }) => {
setStart(addDays(new Date(), -3).toString()); setStart(addDays(new Date(), -3).toString());
setEnd(new Date().toString()); setEnd(new Date().toString());
getCommands(); getCommands();
setToDelete(null);
} }
}, [selectedDeviceId]); }, [selectedDeviceId]);
@@ -272,7 +285,7 @@ const DeviceCommands = ({ selectedDeviceId }) => {
<td> <td>
<CRow> <CRow>
<CCol> <CCol>
<CPopover content='Results'> <CPopover content="Results">
<CButton <CButton
color="primary" color="primary"
variant={details.includes(index) ? '' : 'outline'} variant={details.includes(index) ? '' : 'outline'}
@@ -285,10 +298,18 @@ const DeviceCommands = ({ selectedDeviceId }) => {
> >
<img <img
src={details.includes(index) ? WhiteResultIcon : BlueResultIcon} src={details.includes(index) ? WhiteResultIcon : BlueResultIcon}
onMouseOver={e => (e.currentTarget.src = WhiteResultIcon)} onMouseOver={(e) => (e.currentTarget.src = WhiteResultIcon)}
onMouseOut={e => (e.currentTarget.src = details.includes(index) ? WhiteResultIcon : BlueResultIcon)} onMouseOut={(e) =>
onBlur={e => (e.currentTarget.src = details.includes(index) ? WhiteResultIcon : BlueResultIcon)} (e.currentTarget.src = details.includes(index)
onFocus={e => (e.currentTarget.src = WhiteResultIcon)} ? WhiteResultIcon
: BlueResultIcon)
}
onBlur={(e) =>
(e.currentTarget.src = details.includes(index)
? WhiteResultIcon
: BlueResultIcon)
}
onFocus={(e) => (e.currentTarget.src = WhiteResultIcon)}
style={{ height: '26px', width: '25px', color: '#007bff' }} style={{ height: '26px', width: '25px', color: '#007bff' }}
alt="AP" alt="AP"
/> />
@@ -296,7 +317,7 @@ const DeviceCommands = ({ selectedDeviceId }) => {
</CPopover> </CPopover>
</CCol> </CCol>
<CCol> <CCol>
<CPopover content='Full Details'> <CPopover content="Full Details">
<CButton <CButton
color="primary" color="primary"
variant={responses.includes(index) ? '' : 'outline'} variant={responses.includes(index) ? '' : 'outline'}
@@ -311,14 +332,14 @@ const DeviceCommands = ({ selectedDeviceId }) => {
</CPopover> </CPopover>
</CCol> </CCol>
<CCol> <CCol>
<CPopover content='Delete'> <CPopover content="Delete">
<CButton <CButton
color="primary" color="primary"
variant='outline' variant="outline"
shape="square" shape="square"
size="sm" size="sm"
onClick={() => { onClick={() => {
toggleConfirmModal(item.UUID); toggleConfirmModal(item.UUID, index);
}} }}
> >
<CIcon name="cilTrash" size="lg" /> <CIcon name="cilTrash" size="lg" />

View File

@@ -19,6 +19,7 @@ import WifiChannelTable from './WifiChannelTable';
import 'react-widgets/styles.css'; import 'react-widgets/styles.css';
import { getToken } from '../../utils/authHelper'; import { getToken } from '../../utils/authHelper';
import axiosInstance from '../../utils/axiosInstance'; import axiosInstance from '../../utils/axiosInstance';
import eventBus from '../../utils/EventBus';
const WifiScanModal = ({ show, toggleModal }) => { const WifiScanModal = ({ show, toggleModal }) => {
const [hadSuccess, setHadSuccess] = useState(false); const [hadSuccess, setHadSuccess] = useState(false);
@@ -107,6 +108,7 @@ const WifiScanModal = ({ show, toggleModal }) => {
.finally(() => { .finally(() => {
setCheckingIfSure(false); setCheckingIfSure(false);
setWaiting(false); setWaiting(false);
eventBus.dispatch("actionCompleted", { message: "An action has been completed" });
}); });
}; };

13
src/utils/EventBus.js Normal file
View File

@@ -0,0 +1,13 @@
const eventBus = {
on(event, callback) {
document.addEventListener(event, (e) => callback(e.detail));
},
dispatch(event, data) {
document.dispatchEvent(new CustomEvent(event, { detail: data }));
},
remove(event, callback) {
document.removeEventListener(event, callback);
},
};
export default eventBus;