diff --git a/ui/src/Components/SilenceModal/DateTimeSelect/index.js b/ui/src/Components/SilenceModal/DateTimeSelect/index.js index eb681d59a..8c0f6ecfb 100644 --- a/ui/src/Components/SilenceModal/DateTimeSelect/index.js +++ b/ui/src/Components/SilenceModal/DateTimeSelect/index.js @@ -1,8 +1,7 @@ -import React, { Component } from "react"; +import React, { useEffect } from "react"; import PropTypes from "prop-types"; -import { observable, action } from "mobx"; -import { observer } from "mobx-react"; +import { useObserver, useLocalStore } from "mobx-react"; import moment from "moment"; @@ -54,8 +53,8 @@ const TabNames = Object.freeze({ Duration: "duration", }); -const TabContentStart = observer(({ silenceFormStore }) => { - return ( +const TabContentStart = ({ silenceFormStore }) => { + return useObserver(() => (
{ onMinuteDec={() => silenceFormStore.data.decStart(1)} />
- ); -}); + )); +}; -const TabContentEnd = observer(({ silenceFormStore }) => { - return ( +const TabContentEnd = ({ silenceFormStore }) => { + return useObserver(() => (
{ onMinuteDec={() => silenceFormStore.data.decEnd(1)} />
- ); -}); + )); +}; // calculate value for duration increase button using a goal step const CalculateChangeValueUp = (currentValue, step) => { @@ -126,8 +125,8 @@ const CalculateChangeValueDown = (currentValue, step) => { return currentValue % step || step; }; -const TabContentDuration = observer(({ silenceFormStore }) => { - return ( +const TabContentDuration = ({ silenceFormStore }) => { + return useObserver(() => (
{ } />
- ); -}); + )); +}; TabContentDuration.propTypes = { silenceFormStore: PropTypes.instanceOf(SilenceFormStore).isRequired, }; -const DateTimeSelect = observer( - class DateTimeSelect extends Component { - static propTypes = { - silenceFormStore: PropTypes.instanceOf(SilenceFormStore).isRequired, - openTab: PropTypes.oneOf(Object.values(TabNames)), - }; - static defaultProps = { - openTab: TabNames.Duration, +const DateTimeSelect = ({ silenceFormStore, openTab }) => { + const tab = useLocalStore(() => ({ + current: openTab, + setStart() { + this.current = TabNames.Start; + }, + setEnd() { + this.current = TabNames.End; + }, + setDuration() { + this.current = TabNames.Duration; + }, + timeNow: moment().seconds(0), + updateTimeNow() { + this.timeNow = moment().seconds(0); + }, + })); + + useEffect(() => { + tab.updateTimeNow(); + const nowUpdateTimer = setInterval(tab.updateTimeNow, 30 * 1000); + return () => { + clearInterval(nowUpdateTimer); }; + }, [tab]); - constructor(props) { - super(props); - - this.tab = observable( - { - current: props.openTab, - setStart() { - this.current = TabNames.Start; - }, - setEnd() { - this.current = TabNames.End; - }, - setDuration() { - this.current = TabNames.Duration; - }, - timeNow: moment().seconds(0), - updateTimeNow() { - this.timeNow = moment().seconds(0); - }, - }, - { - setStart: action.bound, - setEnd: action.bound, - setDuration: action.bound, - updateTimeNow: action.bound, - } - ); - } - - componentDidMount() { - this.tab.updateTimeNow(); - this.nowUpdateTimer = setInterval(this.tab.updateTimeNow, 30 * 1000); - } - componentWillUnmount() { - clearInterval(this.nowUpdateTimer); - this.nowUpdateTimer = null; - } - - render() { - const { silenceFormStore } = this.props; - - return ( - -
    - - Starts - - - } - active={this.tab.current === TabNames.Start} - onClick={this.tab.setStart} - /> - - Ends - - - } - active={this.tab.current === TabNames.End} - onClick={this.tab.setEnd} - /> - - Duration - - - } - active={this.tab.current === TabNames.Duration} - onClick={this.tab.setDuration} - /> -
-
- {this.tab.current === TabNames.Duration ? ( - - ) : null} - {this.tab.current === TabNames.Start ? ( - - ) : null} - {this.tab.current === TabNames.End ? ( - - ) : null} -
-
- ); - } - } -); + return useObserver(() => ( + +
    + + Starts + + + } + active={tab.current === TabNames.Start} + onClick={tab.setStart} + /> + + Ends + + + } + active={tab.current === TabNames.End} + onClick={tab.setEnd} + /> + + Duration + + + } + active={tab.current === TabNames.Duration} + onClick={tab.setDuration} + /> +
+
+ {tab.current === TabNames.Duration ? ( + + ) : null} + {tab.current === TabNames.Start ? ( + + ) : null} + {tab.current === TabNames.End ? ( + + ) : null} +
+
+ )); +}; +DateTimeSelect.propTypes = { + silenceFormStore: PropTypes.instanceOf(SilenceFormStore).isRequired, + openTab: PropTypes.oneOf(Object.values(TabNames)), +}; +DateTimeSelect.defaultProps = { + openTab: TabNames.Duration, +}; export { DateTimeSelect, diff --git a/ui/src/Components/SilenceModal/DateTimeSelect/index.test.js b/ui/src/Components/SilenceModal/DateTimeSelect/index.test.js index a6e019672..e882edf38 100644 --- a/ui/src/Components/SilenceModal/DateTimeSelect/index.test.js +++ b/ui/src/Components/SilenceModal/DateTimeSelect/index.test.js @@ -132,12 +132,9 @@ describe("", () => { expect(tree.find(".nav-link").at(1).text()).toBe("Endsin 59m "); }); - it("nowUpdateTimer is destroyed before unmount", () => { + it("unmounts cleanly", () => { const tree = MountedDateTimeSelect(); - const instance = tree.instance(); - expect(instance.nowUpdateTimer).toBeDefined(); - instance.componentWillUnmount(); - expect(instance.nowUpdateTimer).toBeNull(); + tree.unmount(); }); });