From 4fb32395392c3c65ad8dcadc42d7804d2e656c63 Mon Sep 17 00:00:00 2001 From: Ted Humphrey Date: Mon, 6 Jul 2020 16:59:47 -0400 Subject: add framework for blocking user interaction this adds two new methods to LoopUI.js, setBusyLoading and clearBusyLoading, and one new state variable, busyLoadingCount, for supporting the blocking of user clicking during async back end calls that might take a bit of time to return. Blocking the user from clicking on a component box is implemented as an important first case use, as well as all PerformAction calls. Issue-ID: CLAMP-894 Change-Id: I28660afe26b6cc8184b9392aee42157f44601bf6 Signed-off-by: Ted Humphrey --- ui-react/src/LoopUI.js | 155 ++++++++++++++++++--- ui-react/src/__snapshots__/LoopUI.test.js.snap | 4 +- ui-react/src/__snapshots__/OnapClamp.test.js.snap | 4 +- ui-react/src/components/dialogs/PerformActions.js | 52 ++++--- .../src/components/dialogs/PerformActions.test.js | 8 +- .../src/components/loop_viewer/svg/SvgGenerator.js | 16 ++- 6 files changed, 188 insertions(+), 51 deletions(-) (limited to 'ui-react') diff --git a/ui-react/src/LoopUI.js b/ui-react/src/LoopUI.js index 8624726be..0ee6e6e24 100644 --- a/ui-react/src/LoopUI.js +++ b/ui-react/src/LoopUI.js @@ -52,6 +52,7 @@ import PerformAction from './components/dialogs/PerformActions'; import RefreshStatus from './components/dialogs/RefreshStatus'; import DeployLoopModal from './components/dialogs/Loop/DeployLoopModal'; import Alert from 'react-bootstrap/Alert'; +import Spinner from 'react-bootstrap/Spinner'; import { Link } from 'react-router-dom'; @@ -59,6 +60,11 @@ const StyledMainDiv = styled.div` background-color: ${props => props.theme.backgroundColor}; ` +const StyledSpinnerDiv = styled.div` + justify-content: center !important; + display: flex !important; +`; + const ProjectNameStyled = styled.a` vertical-align: middle; padding-left: 30px; @@ -108,7 +114,8 @@ export default class LoopUI extends React.Component { loopName: OnapConstants.defaultLoopName, loopCache: new LoopCache({}), showSucAlert: false, - showFailAlert: false + showFailAlert: false, + busyLoadingCount: 0 }; constructor() { @@ -120,6 +127,9 @@ export default class LoopUI extends React.Component { this.showSucAlert = this.showSucAlert.bind(this); this.showFailAlert = this.showFailAlert.bind(this); this.disableAlert = this.disableAlert.bind(this); + this.setBusyLoading = this.setBusyLoading.bind(this); + this.clearBusyLoading = this.clearBusyLoading.bind(this); + this.isBusyLoading = this.isBusyLoading.bind(this); } componentWillMount() { @@ -191,7 +201,7 @@ export default class LoopUI extends React.Component { renderLoopViewBody() { return ( - + @@ -225,53 +235,160 @@ export default class LoopUI extends React.Component { showFailAlert(message) { this.setState ({ showFailAlert: true, showMessage:message }); } - + disableAlert() { this.setState ({ showSucAlert: false, showFailAlert: false }); } loadLoop(loopName) { + this.setBusyLoading(); LoopService.getLoop(loopName).then(loop => { console.debug("Updating loopCache"); LoopActionService.refreshStatus(loopName).then(data => { this.updateLoopCache(data); + this.clearBusyLoading(); this.props.history.push('/'); }) .catch(error => { this.updateLoopCache(loop); + this.clearBusyLoading(); this.props.history.push('/'); }); }); } + setBusyLoading() { + this.setState((state,props) => ({ busyLoadingCount: ++state.busyLoadingCount })); + } + + clearBusyLoading() { + this.setState((state,props) => ({ busyLoadingCount: --state.busyLoadingCount })); + } + + isBusyLoading() { + if (this.state.busyLoadingCount === 0) { + return false; + } else { + return true; + } + } + closeLoop() { this.setState({ loopCache: new LoopCache({}), loopName: OnapConstants.defaultLoopName }); this.props.history.push('/'); } - render() { - return ( - + renderRoutes() { + return( + ()} /> ()} /> ()} /> ()} /> - ()} /> - ()} /> - ()} /> - ()} /> - ()} /> + + ()} + /> + ()} + /> + ()} + /> + ()} + /> + ()} + /> ()} /> - ()} /> - ()} /> - ()} /> - ()} /> - ()} /> - ()} /> - ()} /> - + + ()} + /> + ()} + /> + ()} + /> + ()} + /> + ()} + /> + ()} + /> + ()} + /> + + ); + } + + renderSpinner() { + if (this.isBusyLoading()) { + return ( + + + Loading... + + + ); + } else { + return (
); + } + } + + render() { + return ( + + + {this.renderRoutes()} + {this.renderSpinner()} {this.renderAlertBar()} {this.renderNavBar()} {this.renderLoopViewer()} diff --git a/ui-react/src/__snapshots__/LoopUI.test.js.snap b/ui-react/src/__snapshots__/LoopUI.test.js.snap index 2dfa48091..cae9182ff 100644 --- a/ui-react/src/__snapshots__/LoopUI.test.js.snap +++ b/ui-react/src/__snapshots__/LoopUI.test.js.snap @@ -4,6 +4,7 @@ exports[`Verify LoopUI Test the render method 1`] = ` + - +
+ - +
{ + if (action === 'delete') { + if (window.confirm('You are about to remove Control Loop Model "' + loopName + + '". Select OK to continue with deletion or Cancel to keep the model.') === false) { + return; + } + } + + this.props.setBusyLoading(); // Alert top level to start block user clicks + + LoopActionService.performAction(loopName, action) + .then(pars => { this.props.showSucAlert("Action " + action + " successfully performed"); - // refresh status and update loop logs - this.refreshStatus(loopName); + if (action === 'delete') { + this.props.updateLoopFunction(null); + this.props.history.push('/'); + } else { + // refresh status and update loop logs + this.refreshStatus(loopName); + } }) .catch(error => { this.props.showFailAlert("Action " + action + " failed"); // refresh status and update loop logs this.refreshStatus(loopName); - }); - + }) + .finally(() => this.props.clearBusyLoading()); } refreshStatus(loopName) { - LoopActionService.refreshStatus(loopName).then(data => { + + this.props.setBusyLoading(); + + LoopActionService.refreshStatus(loopName) + .then(data => { this.props.updateLoopFunction(data); this.props.history.push('/'); }) - .catch(error => { + .catch(error => { this.props.history.push('/'); - }); + }) + .finally(() => this.props.clearBusyLoading()); } render() { - return ( - - - - - ); + return null; } } diff --git a/ui-react/src/components/dialogs/PerformActions.test.js b/ui-react/src/components/dialogs/PerformActions.test.js index b833a929d..c91c2f675 100644 --- a/ui-react/src/components/dialogs/PerformActions.test.js +++ b/ui-react/src/components/dialogs/PerformActions.test.js @@ -38,6 +38,8 @@ describe('Verify PerformActions', () => { const updateLoopFunction = jest.fn(); const showSucAlert = jest.fn(); const showFailAlert = jest.fn(); + const setBusyLoading = jest.fn(); + const clearBusyLoading = jest.fn(); LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { return Promise.resolve({ @@ -47,7 +49,7 @@ describe('Verify PerformActions', () => { }); }); const component = shallow() + loopAction="submit" history={historyMock} updateLoopFunction={updateLoopFunction} showSucAlert={showSucAlert} showFailAlert={showFailAlert} setBusyLoading={setBusyLoading} clearBusyLoading={clearBusyLoading}/>) await flushPromises(); component.update(); @@ -60,6 +62,8 @@ describe('Verify PerformActions', () => { const updateLoopFunction = jest.fn(); const showSucAlert = jest.fn(); const showFailAlert = jest.fn(); + const setBusyLoading = jest.fn(); + const clearBusyLoading = jest.fn(); LoopActionService.performAction = jest.fn().mockImplementation(() => { return Promise.resolve({ @@ -76,7 +80,7 @@ describe('Verify PerformActions', () => { }); }); const component = shallow() + loopAction="submit" history={historyMock} updateLoopFunction={updateLoopFunction} showSucAlert={showSucAlert} showFailAlert={showFailAlert} setBusyLoading={setBusyLoading} clearBusyLoading={clearBusyLoading}/>) await flushPromises(); component.update(); diff --git a/ui-react/src/components/loop_viewer/svg/SvgGenerator.js b/ui-react/src/components/loop_viewer/svg/SvgGenerator.js index d718c2e44..7070455e7 100644 --- a/ui-react/src/components/loop_viewer/svg/SvgGenerator.js +++ b/ui-react/src/components/loop_viewer/svg/SvgGenerator.js @@ -70,13 +70,15 @@ class SvgGenerator extends React.Component { } handleSvgClick(event) { - if (this.state.clickable) { - console.debug("svg click event received"); - var elementName = event.target.parentNode.getAttribute('policyId'); - console.info("SVG element clicked", elementName); - if (elementName !== null) { - this.props.history.push("/policyModal/"+event.target.parentNode.getAttribute('policyType')+"/"+elementName); - } + console.debug("svg click event received"); + if (this.state.clickable) { + var elementName = event.target.parentNode.getAttribute('policyId'); + console.info("SVG element clicked", elementName); + // Only allow movement to policy editing IF there busyLoadingCOunt is 0, + // meaning we are not waiting for refreshStatus to complete, for example + if (elementName !== null && !this.props.isBusyLoading()) { + this.props.history.push("/policyModal/"+event.target.parentNode.getAttribute('policyType')+"/"+elementName); + } } } -- cgit 1.2.3-korg