From eae920b9226ffd9c43699c7f555000d5fc1c623f Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Thu, 29 Oct 2020 21:39:05 +0100 Subject: Reimplement modals for stability --- .../NewPortfolioModalComponent.js | 145 +++++++++------------ .../custom-components/NewTopologyModalComponent.js | 121 +++++++---------- 2 files changed, 106 insertions(+), 160 deletions(-) (limited to 'frontend/src') diff --git a/frontend/src/components/modals/custom-components/NewPortfolioModalComponent.js b/frontend/src/components/modals/custom-components/NewPortfolioModalComponent.js index 19049931..978ca11d 100644 --- a/frontend/src/components/modals/custom-components/NewPortfolioModalComponent.js +++ b/frontend/src/components/modals/custom-components/NewPortfolioModalComponent.js @@ -1,98 +1,69 @@ import PropTypes from 'prop-types' -import React from 'react' +import React, { useRef } from 'react' +import { Form, FormGroup, Input, Label } from 'reactstrap' import Modal from '../Modal' import { AVAILABLE_METRICS } from '../../../util/available-metrics' -class NewPortfolioModalComponent extends React.Component { - static propTypes = { - show: PropTypes.bool.isRequired, - callback: PropTypes.func.isRequired, - } +const NewPortfolioModalComponent = ({ show, callback }) => { + const textInput = useRef(null) + const repeatsInput = useRef(null) + const metricCheckboxes = useRef({}) - constructor(props) { - super(props) - this.metricCheckboxes = {} - } - - componentDidMount() { - this.reset() - } - - reset() { - if (this.textInput) { - this.textInput.value = '' - AVAILABLE_METRICS.forEach((metric) => { - this.metricCheckboxes[metric].checked = true - }) - this.repeatsInput.value = 1 - } - } - - onSubmit() { - this.props.callback(this.textInput.value, { - enabledMetrics: AVAILABLE_METRICS.filter((metric) => this.metricCheckboxes[metric].checked), - repeatsPerScenario: parseInt(this.repeatsInput.value), + const onSubmit = () => + callback(textInput.current.value, { + enabledMetrics: AVAILABLE_METRICS.filter((metric) => metricCheckboxes.current[metric].checked), + repeatsPerScenario: parseInt(repeatsInput.current.value), }) - this.reset() - } + const onCancel = () => callback(undefined) - onCancel() { - this.props.callback(undefined) - this.reset() - } - - render() { - return ( - +
{ + e.preventDefault() + this.onSubmit() + }} > - { - e.preventDefault() - this.onSubmit() - }} - > -
- - (this.textInput = textInput)} - /> -
-

Targets

-
Metrics
-
- {AVAILABLE_METRICS.map((metric) => ( -
- -
- ))} -
-
- - (this.repeatsInput = repeatsInput)} - /> -
-
-
- ) - } + + + + +

Targets

+
Metrics
+ + {AVAILABLE_METRICS.map((metric) => ( + + + + ))} + + + + + + + + ) +} + +NewPortfolioModalComponent.propTypes = { + show: PropTypes.bool.isRequired, + callback: PropTypes.func.isRequired, } export default NewPortfolioModalComponent diff --git a/frontend/src/components/modals/custom-components/NewTopologyModalComponent.js b/frontend/src/components/modals/custom-components/NewTopologyModalComponent.js index 8a625b13..b20ec13b 100644 --- a/frontend/src/components/modals/custom-components/NewTopologyModalComponent.js +++ b/frontend/src/components/modals/custom-components/NewTopologyModalComponent.js @@ -1,90 +1,65 @@ import PropTypes from 'prop-types' -import React from 'react' +import { Form, FormGroup, Input, Label } from 'reactstrap' +import React, { useRef } from 'react' import Shapes from '../../../shapes' import Modal from '../Modal' -class NewTopologyModalComponent extends React.Component { - static propTypes = { - show: PropTypes.bool.isRequired, - topologies: PropTypes.arrayOf(Shapes.Topology), - onCreateTopology: PropTypes.func.isRequired, - onDuplicateTopology: PropTypes.func.isRequired, - onCancel: PropTypes.func.isRequired, +const NewTopologyModalComponent = ({ show, onCreateTopology, onDuplicateTopology, onCancel, topologies }) => { + const textInput = useRef(null) + const originTopology = useRef(null) + + const onCreate = () => { + onCreateTopology(textInput.current.value) } - reset() { - if (this.textInput) { - this.textInput.value = '' - this.originTopology.selectedIndex = 0 - } + const onDuplicate = () => { + onDuplicateTopology(textInput.current.value, originTopology.current.value) } - onSubmit() { - if (this.originTopology.selectedIndex === 0) { - this.onCreate() + const onSubmit = () => { + if (originTopology.current.selectedIndex === 0) { + onCreate() } else { - this.onDuplicate() + onDuplicate() } } - onCreate() { - this.props.onCreateTopology(this.textInput.value) - this.reset() - } - - onDuplicate() { - this.props.onDuplicateTopology(this.textInput.value, this.originTopology.value) - this.reset() - } - - onCancel() { - this.props.onCancel() - this.reset() - } - - render() { - return ( - +
{ + e.preventDefault() + onSubmit() + }} > - { - e.preventDefault() - this.onSubmit() - }} - > -
- - (this.textInput = textInput)} - /> -
-
- - + + + + + + {topologies.map((topology) => ( + - {this.props.topologies.map((topology) => ( - - ))} - -
-
-
- ) - } + ))} + + + + + ) +} + +NewTopologyModalComponent.propTypes = { + show: PropTypes.bool.isRequired, + topologies: PropTypes.arrayOf(Shapes.Topology), + onCreateTopology: PropTypes.func.isRequired, + onDuplicateTopology: PropTypes.func.isRequired, + onCancel: PropTypes.func.isRequired, } export default NewTopologyModalComponent -- cgit v1.2.3