Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 97 additions & 109 deletions src/components/edit/orgUnit/OrgUnitDialog.jsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 (
<div className={styles.content} data-test="orgunitdialog">
<Tabs value={tab} onChange={(tab) => this.setState({ tab })}>
<Tab value="orgunits">{i18n.t('Organisation Units')}</Tab>
<Tab value="style">{i18n.t('Style')}</Tab>
</Tabs>
<div className={styles.tabContent}>
{tab === 'orgunits' && (
<OrgUnitSelect warning={orgUnitsError} />
)}
{tab === 'style' && (
<div
className={styles.flexColumnFlow}
data-test="orgunitdialog-styletab"
>
<div className={styles.flexColumn}>
<Labels />
<ColorPicker
label={i18n.t('Boundary color')}
color={
organisationUnitColor || ORG_UNIT_COLOR
}
onChange={setOrganisationUnitColor}
className={styles.narrowField}
/>
<NumberField
label={i18n.t('Point radius')}
min={MIN_RADIUS}
max={MAX_RADIUS}
value={
radiusLow !== undefined
? radiusLow
: ORG_UNIT_RADIUS
}
onChange={setRadiusLow}
className={styles.narrowFieldIcon}
/>
</div>
<div className={styles.flexColumn}>
<StyleByGroupSet
defaultStyleType={STYLE_TYPE_COLOR}
/>
</div>
</div>
)}
</div>
</div>
)
}

// 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 (
<div className={styles.content} data-test="orgunitdialog">
<Tabs value={tab} onChange={setTab}>
<Tab value="orgunits">{i18n.t('Organisation Units')}</Tab>
<Tab value="style">{i18n.t('Style')}</Tab>
</Tabs>

<div className={styles.tabContent}>
{tab === 'orgunits' && (
<OrgUnitSelect warning={orgUnitsError} />
)}

{tab === 'style' && (
<div
className={styles.flexColumnFlow}
data-test="orgunitdialog-styletab"
>
<div className={styles.flexColumn}>
<Labels />
<ColorPicker
label={i18n.t('Boundary color')}
color={organisationUnitColor || ORG_UNIT_COLOR}
onChange={(color) =>
dispatch(setOrganisationUnitColor(color))
}
className={styles.narrowField}
/>
<NumberField
label={i18n.t('Point radius')}
min={MIN_RADIUS}
max={MAX_RADIUS}
value={
radiusLow !== undefined
? radiusLow
: ORG_UNIT_RADIUS
}
onChange={(value) =>
dispatch(setRadiusLow(value))
}
className={styles.narrowFieldIcon}
/>
</div>

<div className={styles.flexColumn}>
<StyleByGroupSet
defaultStyleType={STYLE_TYPE_COLOR}
/>
</div>
</div>
)}
</div>
</div>
)
}

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
Loading