From da4ca91fc6f0ea250e027622ccf93b3451fc95de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sat, 25 Apr 2020 20:06:38 +0100 Subject: [PATCH] fix(ui): rewrite MainModal with hooks --- ui/src/Components/MainModal/index.js | 112 ++++++++++------------ ui/src/Components/MainModal/index.test.js | 5 +- 2 files changed, 51 insertions(+), 66 deletions(-) diff --git a/ui/src/Components/MainModal/index.js b/ui/src/Components/MainModal/index.js index f359bbccb..807012094 100644 --- a/ui/src/Components/MainModal/index.js +++ b/ui/src/Components/MainModal/index.js @@ -1,8 +1,7 @@ -import React, { Component } from "react"; +import React from "react"; import PropTypes from "prop-types"; -import { observer } from "mobx-react"; -import { observable, action } from "mobx"; +import { observer, useLocalStore } from "mobx-react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faCog } from "@fortawesome/free-solid-svg-icons/faCog"; @@ -20,66 +19,53 @@ const MainModalContent = React.lazy(() => })) ); -const MainModal = observer( - class MainModal extends Component { - static propTypes = { - alertStore: PropTypes.instanceOf(AlertStore).isRequired, - settingsStore: PropTypes.instanceOf(Settings).isRequired, - }; +const MainModal = observer(({ alertStore, settingsStore }) => { + const toggle = useLocalStore(() => ({ + show: false, + toggle() { + this.show = !this.show; + }, + hide() { + this.show = false; + }, + })); - toggle = observable( - { - show: false, - toggle() { - this.show = !this.show; - }, - hide() { - this.show = false; - }, - }, - { toggle: action.bound, hide: action.bound } - ); - - render() { - const { alertStore, settingsStore } = this.props; - - return ( - -
  • - - - - - -
  • - - - - - } - > - - - -
    - ); - } - } -); + return ( + +
  • + + + + + +
  • + + + + + } + > + + + +
    + ); +}); +MainModal.propTypes = { + alertStore: PropTypes.instanceOf(AlertStore).isRequired, + settingsStore: PropTypes.instanceOf(Settings).isRequired, +}; export { MainModal }; diff --git a/ui/src/Components/MainModal/index.test.js b/ui/src/Components/MainModal/index.test.js index fa8e5fa6a..fc4d83f71 100644 --- a/ui/src/Components/MainModal/index.test.js +++ b/ui/src/Components/MainModal/index.test.js @@ -81,15 +81,14 @@ describe("", () => { expect(tree.find("MainModalContent")).toHaveLength(0); }); - it("hides the modal when hide() is called", () => { + it("hides the modal when button.close is clicked", () => { const tree = MountedMainModal(); const toggle = tree.find(".nav-link"); toggle.simulate("click"); expect(tree.find("MainModalContent")).toHaveLength(1); - const instance = tree.instance(); - instance.toggle.hide(); + tree.find("button.close").simulate("click"); jest.runOnlyPendingTimers(); tree.update(); expect(tree.find("MainModalContent")).toHaveLength(0);