11import i18n from '@dhis2/d2-i18n'
22import PropTypes from 'prop-types'
3- import React from 'react'
3+ import React , { useCallback } from 'react'
44import { useDispatch , useSelector } from 'react-redux'
5- import { setCountFeaturesWithoutCoordinates } from '../../../actions/layerEdit.js'
5+ import {
6+ setCountFeaturesWithoutCoordinates ,
7+ setStyle ,
8+ } from '../../../actions/layerEdit.js'
69import { EE_BUFFER } from '../../../constants/layers.js'
10+ import {
11+ getColorPalette ,
12+ defaultColorScaleName ,
13+ defaultClasses ,
14+ } from '../../../util/colors.js'
715import { Checkbox } from '../../core/index.js'
816import BufferRadius from '../shared/BufferRadius.jsx'
917import styles from '../styles/LayerDialog.module.css'
1018import LegendPreview from './LegendPreview.jsx'
1119import StyleSelect from './StyleSelect.jsx'
1220
13- const StyleTab = ( { unit, style, showBelowMin, hasOrgUnitField } ) => {
21+ const StyleTab = ( {
22+ unit,
23+ style,
24+ defaultStyle,
25+ customColorScaleName,
26+ showBelowMin,
27+ hasOrgUnitField,
28+ } ) => {
1429 const dispatch = useDispatch ( )
1530 const countFeaturesWithoutCoordinates = useSelector (
1631 ( state ) => state . layerEdit . countFeaturesWithoutCoordinates
@@ -22,10 +37,47 @@ const StyleTab = ({ unit, style, showBelowMin, hasOrgUnitField }) => {
2237 palette !== undefined &&
2338 palette . length < 10
2439
40+ const hasReferenceScale = ! ! defaultStyle ?. ranges ?. length
41+ const useCustomScale = hasReferenceScale && ! style ?. ranges ?. length
42+
43+ const handleScaleToggle = useCallback (
44+ ( checked ) => {
45+ if ( checked ) {
46+ const { min, max } = defaultStyle ?? { }
47+ dispatch (
48+ setStyle ( {
49+ min,
50+ max,
51+ palette : getColorPalette (
52+ customColorScaleName ?? defaultColorScaleName ,
53+ defaultClasses
54+ ) ,
55+ ranges : null ,
56+ } )
57+ )
58+ } else {
59+ dispatch ( setStyle ( defaultStyle ) )
60+ }
61+ } ,
62+ [ customColorScaleName , defaultStyle , dispatch ]
63+ )
64+
2565 return (
2666 < div className = { styles . flexColumnFlow } >
2767 < div className = { styles . flexColumn } >
28- { isClassStyle && < StyleSelect unit = { unit } style = { style } /> }
68+ { unit && (
69+ < p >
70+ { i18n . t ( 'Unit' ) } : { unit }
71+ </ p >
72+ ) }
73+ { hasReferenceScale && (
74+ < Checkbox
75+ label = { i18n . t ( 'Use custom legend' ) }
76+ checked = { useCustomScale }
77+ onChange = { handleScaleToggle }
78+ />
79+ ) }
80+ { isClassStyle && < StyleSelect style = { style } /> }
2981 < BufferRadius
3082 label = { i18n . t ( 'Facility buffer' ) }
3183 defaultRadius = { EE_BUFFER }
@@ -40,7 +92,7 @@ const StyleTab = ({ unit, style, showBelowMin, hasOrgUnitField }) => {
4092 }
4193 />
4294 </ div >
43- { isClassStyle && (
95+ { ( isClassStyle || ( hasReferenceScale && ! useCustomScale ) ) && (
4496 < LegendPreview style = { style } showBelowMin = { showBelowMin } />
4597 ) }
4698 </ div >
@@ -49,6 +101,13 @@ const StyleTab = ({ unit, style, showBelowMin, hasOrgUnitField }) => {
49101
50102StyleTab . propTypes = {
51103 hasOrgUnitField : PropTypes . bool . isRequired ,
104+ customColorScaleName : PropTypes . string ,
105+ defaultStyle : PropTypes . shape ( {
106+ max : PropTypes . number ,
107+ min : PropTypes . number ,
108+ palette : PropTypes . array ,
109+ ranges : PropTypes . array ,
110+ } ) ,
52111 showBelowMin : PropTypes . bool ,
53112 style : PropTypes . oneOfType ( [
54113 PropTypes . array ,
0 commit comments