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

@@ -1,4 +1,4 @@
import React, { useState, useEffect} from 'react'; import React, { useState, useEffect } from 'react';
import { import {
CButton, CButton,
CModal, CModal,
@@ -7,45 +7,51 @@ 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';
const ConfirmModal = ({ show, toggle, action }) => { const ConfirmModal = ({ show, toggle, action }) => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [haveResult, setHaveResult] = useState(false); const [haveResult, setHaveResult] = useState(false);
const [success, setSuccess] = useState(false); const [success, setSuccess] = useState(false);
const getButtonContent = () => { const getButtonContent = () => {
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 ( );
<CBadge color="danger" shape="pill">Error</CBadge> }
); return (
} <CBadge color="danger" shape="pill">
if(loading){ Error
return ( </CBadge>
<div> );
Loading... }
<CSpinner component="span" size="sm" /> if (loading) {
</div> return (
); <div>
} Loading...
return 'Yes'; <CSpinner component="span" size="sm" />
} </div>
);
}
return 'Yes';
};
const doAction = async () => { const doAction = async () => {
setLoading(true); setLoading(true);
const result = await action(); const result = await 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);
@@ -53,8 +59,8 @@ const ConfirmModal = ({ show, toggle, action }) => {
setSuccess(false); setSuccess(false);
}, [show]); }, [show]);
return( return (
<CModal style= {{color: '#3c4b64'}} show={show} onClose={toggle}> <CModal style={{ color: '#3c4b64' }} show={show} onClose={toggle}>
<CModalHeader closeButton> <CModalHeader closeButton>
<CModalTitle>Delete Command</CModalTitle> <CModalTitle>Delete Command</CModalTitle>
</CModalHeader> </CModalHeader>
@@ -62,19 +68,15 @@ 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} {getButtonContent()}
color="primary"
onClick={() => doAction()}
>
{getButtonContent()}
</CButton> </CButton>
<CButton color="secondary" onClick={toggle}> <CButton color="secondary" onClick={toggle}>
Cancel Cancel
</CButton> </CButton>
</CModalFooter> </CModalFooter>
</CModal> </CModal>
); );
}; };
ConfirmModal.propTypes = { ConfirmModal.propTypes = {

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,8 +95,12 @@ const DeviceCommands = ({ selectedDeviceId }) => {
}); });
}; };
const deleteCommand= async () => { eventBus.on("actionCompleted", () =>
if(uuidDelete === ''){ getCommands()
);
const deleteCommand = async () => {
if (uuidDelete === '') {
return false; return false;
} }
const options = { const options = {
@@ -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'}
@@ -283,21 +296,29 @@ const DeviceCommands = ({ selectedDeviceId }) => {
toggleDetails(item, index); toggleDetails(item, index);
}} }}
> >
<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
style={{ height: '26px', width: '25px', color: '#007bff'}} : BlueResultIcon)
alt="AP" }
onBlur={(e) =>
(e.currentTarget.src = details.includes(index)
? WhiteResultIcon
: BlueResultIcon)
}
onFocus={(e) => (e.currentTarget.src = WhiteResultIcon)}
style={{ height: '26px', width: '25px', color: '#007bff' }}
alt="AP"
/> />
</CButton> </CButton>
</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'}
shape="square" shape="square"
@@ -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" />
@@ -365,9 +386,9 @@ const DeviceCommands = ({ selectedDeviceId }) => {
scanResults={chosenWifiScan} scanResults={chosenWifiScan}
date={scanDate} date={scanDate}
/> />
<ConfirmModal <ConfirmModal
show={showConfirmModal} show={showConfirmModal}
toggle={toggleConfirmModal} toggle={toggleConfirmModal}
action={deleteCommand} action={deleteCommand}
/> />
<CIcon name="cilNotes" style={{ color: 'white' }} size="lg" /> <CIcon name="cilNotes" style={{ color: 'white' }} 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;