diff --git a/ui/src/Components/Grid/ReloadNeeded/index.js b/ui/src/Components/Grid/ReloadNeeded/index.js index 0c63b45eb..45e631179 100644 --- a/ui/src/Components/Grid/ReloadNeeded/index.js +++ b/ui/src/Components/Grid/ReloadNeeded/index.js @@ -1,4 +1,4 @@ -import React, { Component } from "react"; +import React, { useEffect } from "react"; import PropTypes from "prop-types"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; @@ -7,42 +7,30 @@ import { faSpinner } from "@fortawesome/free-solid-svg-icons/faSpinner"; import { CenteredMessage } from "Components/CenteredMessage"; -class ReloadNeeded extends Component { - static propTypes = { - reloadAfter: PropTypes.number.isRequired, - }; +const ReloadNeeded = ({ reloadAfter }) => { + useEffect(() => { + const timer = setTimeout(() => window.location.reload(), reloadAfter); + return () => clearTimeout(timer); + }, [reloadAfter]); - reloadApp = () => { - window.location.reload(); - }; - - componentDidMount() { - const { reloadAfter } = this.props; - this.timer = setTimeout(this.reloadApp, reloadAfter); - } - - componentWillUnmount() { - clearTimeout(this.timer); - this.timer = null; - } - - render() { - return ( - - - - - - All API connection attempts failed. This migth be caused by - authentication middleware, will try to reload. - - - - ); - } -} + return ( + + + + + + All API connection attempts failed. This migth be caused by + authentication middleware, will try to reload. + + + + ); +}; +ReloadNeeded.propTypes = { + reloadAfter: PropTypes.number.isRequired, +}; export { ReloadNeeded }; diff --git a/ui/src/Components/Grid/ReloadNeeded/index.test.js b/ui/src/Components/Grid/ReloadNeeded/index.test.js index 61deee929..20d673ba6 100644 --- a/ui/src/Components/Grid/ReloadNeeded/index.test.js +++ b/ui/src/Components/Grid/ReloadNeeded/index.test.js @@ -35,9 +35,13 @@ describe("", () => { expect(reloadSpy).toBeCalled(); }); - it("unmounts cleanly", () => { + it("stops calling window.location.reload after unmount", () => { + const reloadSpy = jest + .spyOn(global.window.location, "reload") + .mockImplementation(() => {}); const tree = mount(); tree.unmount(); jest.runOnlyPendingTimers(); + expect(reloadSpy).not.toBeCalled(); }); });
- - All API connection attempts failed. This migth be caused by - authentication middleware, will try to reload. -
+ + All API connection attempts failed. This migth be caused by + authentication middleware, will try to reload. +