diff --git a/src/components/edit/orgUnit/OrgUnitDialog.jsx b/src/components/edit/orgUnit/OrgUnitDialog.jsx index 9ceae1ff5..99b43ff92 100644 --- a/src/components/edit/orgUnit/OrgUnitDialog.jsx +++ b/src/components/edit/orgUnit/OrgUnitDialog.jsx @@ -1,7 +1,7 @@ import i18n from '@dhis2/d2-i18n' import PropTypes from 'prop-types' -import React, { Component } from 'react' -import { connect } from 'react-redux' +import React, { useEffect, useState, useCallback } from 'react' +import { useDispatch } from 'react-redux' import { setRadiusLow, setOrganisationUnitColor, @@ -20,122 +20,110 @@ import OrgUnitSelect from '../../orgunits/OrgUnitSelect.jsx' import Labels from '../shared/Labels.jsx' import styles from '../styles/LayerDialog.module.css' -class OrgUnitDialog extends Component { - static propTypes = { - setOrganisationUnitColor: PropTypes.func.isRequired, - setRadiusLow: PropTypes.func.isRequired, - validateLayer: PropTypes.bool.isRequired, - onLayerValidation: PropTypes.func.isRequired, - organisationUnitColor: PropTypes.string, - radiusLow: PropTypes.number, - rows: PropTypes.array, - } +const OrgUnitDialog = ({ + validateLayer, + onLayerValidation, + organisationUnitColor, + radiusLow, + rows, +}) => { + const dispatch = useDispatch() - state = { - tab: 'orgunits', - } - - componentDidUpdate(prev) { - const { validateLayer, onLayerValidation } = this.props - - if (validateLayer && validateLayer !== prev.validateLayer) { - onLayerValidation(this.validate()) - } - } - - render() { - const { - radiusLow, - organisationUnitColor, - setOrganisationUnitColor, - setRadiusLow, - } = this.props - - const { tab, orgUnitsError } = this.state - - return ( -
- this.setState({ tab })}> - {i18n.t('Organisation Units')} - {i18n.t('Style')} - -
- {tab === 'orgunits' && ( - - )} - {tab === 'style' && ( -
-
- - - -
-
- -
-
- )} -
-
- ) - } - - // TODO: Add to parent class? - setErrorState(key, message, tab) { - this.setState({ - [key]: message, - tab, - }) + const [tab, setTab] = useState('orgunits') + const [orgUnitsError, setOrgUnitsError] = useState(null) + // -------------------------- + // Validation handler + // -------------------------- + const setErrorState = useCallback((message, tabName) => { + setOrgUnitsError(message) + setTab(tabName) return false - } - - validate() { - const { rows } = this.props + }, []) + const validate = useCallback(() => { if (!getOrgUnitsFromRows(rows).length) { - return this.setErrorState( - 'orgUnitsError', + return setErrorState( i18n.t('No organisation units are selected'), 'orgunits' ) } - return true - } + }, [rows, setErrorState]) + + // -------------------------- + // Validation (was componentDidUpdate) + // -------------------------- + useEffect(() => { + if (validateLayer) { + onLayerValidation(validate()) + } + }, [validateLayer, validate, onLayerValidation]) + + // -------------------------- + // Render + // -------------------------- + return ( +
+ + {i18n.t('Organisation Units')} + {i18n.t('Style')} + + +
+ {tab === 'orgunits' && ( + + )} + + {tab === 'style' && ( +
+
+ + + dispatch(setOrganisationUnitColor(color)) + } + className={styles.narrowField} + /> + + dispatch(setRadiusLow(value)) + } + className={styles.narrowFieldIcon} + /> +
+ +
+ +
+
+ )} +
+
+ ) +} + +OrgUnitDialog.propTypes = { + validateLayer: PropTypes.bool.isRequired, + onLayerValidation: PropTypes.func.isRequired, + organisationUnitColor: PropTypes.string, + radiusLow: PropTypes.number, + rows: PropTypes.array, } -export default connect( - null, - { - setRadiusLow, - setOrganisationUnitColor, - }, - null, - { - forwardRef: true, - } -)(OrgUnitDialog) +export default OrgUnitDialog