From 1f569e45dcdacb7eb7c209206a4dda16f7220257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Sandvik?= Date: Fri, 6 Oct 2023 10:30:31 +0200 Subject: [PATCH] chore: code cleaning --- src/components/edit/LayerEdit.js | 2 +- .../edit/earthEngine/EarthEngineDialog.js | 53 ++++--------------- .../edit/earthEngine/PeriodSelect.js | 21 ++++---- src/components/edit/earthEngine/PeriodTab.js | 3 ++ .../layers/download/DataDownloadDialog.js | 4 +- .../layers/overlays/AddLayerPopover.js | 5 +- .../layers/toolbar/LayerToolbarMoreMenu.js | 3 +- src/components/map/MapApi.js | 2 - src/constants/earthEngine.js | 19 ++++--- src/util/earthEngine.js | 9 ---- 10 files changed, 44 insertions(+), 77 deletions(-) diff --git a/src/components/edit/LayerEdit.js b/src/components/edit/LayerEdit.js index 29b71e2e6..4cc84a13c 100644 --- a/src/components/edit/LayerEdit.js +++ b/src/components/edit/LayerEdit.js @@ -73,7 +73,7 @@ const LayerEdit = ({ layer, addLayer, updateLayer, cancelLayer }) => { return null } - const type = layer.layer + const type = layer.layerType || layer.layer const LayerDialog = layerType[type] if (!LayerDialog) { diff --git a/src/components/edit/earthEngine/EarthEngineDialog.js b/src/components/edit/earthEngine/EarthEngineDialog.js index 6be648166..0d78e2c22 100644 --- a/src/components/edit/earthEngine/EarthEngineDialog.js +++ b/src/components/edit/earthEngine/EarthEngineDialog.js @@ -16,8 +16,6 @@ import { } from '../../../constants/layers.js' import { getPeriodFromFilter, - getPeriods, - defaultFilters, translateFilters, } from '../../../util/earthEngine.js' import { incrementDate } from '../../../util/time.js' @@ -59,7 +57,7 @@ const EarthEngineDialog = (props) => { periodType, periodReducer, bands, - filters, // = defaultFilters, + filters, unit, source, sourceUrl, @@ -117,7 +115,7 @@ const EarthEngineDialog = (props) => { setFilter(periodFilter) }, - [periodType, filters, setFilter] + [/* periodType, */ filters, setFilter] ) const noBandSelected = Array.isArray(bands) && (!band || !band.length) @@ -127,41 +125,6 @@ const EarthEngineDialog = (props) => { const hasOrgUnitField = !!orgUnitField && orgUnitField !== NONE - // Load all available periods - /* - useEffect(() => { - let isCancelled = false - - if (periodType) { - getPeriods(datasetId, periodType, filters) - .then((periods) => { - if (!isCancelled) { - setPeriods(periods) - } - }) - .catch((error) => - setError({ - type: 'engine', - message: error.message, - }) - ) - } - - return () => (isCancelled = true) - }, [datasetId, periodType, filters]) - */ - - // Set most recent period by default - /* - useEffect(() => { - if (filter === undefined) { - if (Array.isArray(periods) && periods.length) { - setFilterFromPeriod(periods[0]) - } - } - }, [periods, filter, setFilterFromPeriod]) - */ - // Set default org unit level useEffect(() => { if (!rows) { @@ -289,10 +252,10 @@ const EarthEngineDialog = (props) => { datasetId={datasetId} periodType={periodType} period={period} - // periods={periods} periodReducer={periodReducer} filters={filters} onChange={setFilterFromPeriod} + onError={setError} errorText={ error && error.type === 'period' && error.message } @@ -314,18 +277,24 @@ const EarthEngineDialog = (props) => { EarthEngineDialog.propTypes = { datasetId: PropTypes.string.isRequired, - // layerId: PropTypes.string.isRequired, + layerId: PropTypes.string.isRequired, setBufferRadius: PropTypes.func.isRequired, setFilter: PropTypes.func.isRequired, setOrgUnits: PropTypes.func.isRequired, validateLayer: PropTypes.bool.isRequired, onLayerValidation: PropTypes.func.isRequired, areaRadius: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), - band: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), + band: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), // TODO: Why array? + bands: PropTypes.array, + description: PropTypes.string, filter: PropTypes.array, + filters: PropTypes.array, legend: PropTypes.object, + notice: PropTypes.string, orgUnitField: PropTypes.string, orgUnits: PropTypes.object, + periodReducer: PropTypes.string, + periodType: PropTypes.string, style: PropTypes.shape({ max: PropTypes.number, min: PropTypes.number, diff --git a/src/components/edit/earthEngine/PeriodSelect.js b/src/components/edit/earthEngine/PeriodSelect.js index 61620ceb7..83d32f343 100644 --- a/src/components/edit/earthEngine/PeriodSelect.js +++ b/src/components/edit/earthEngine/PeriodSelect.js @@ -6,14 +6,13 @@ import { SelectField } from '../../core/index.js' import { getPeriods } from '../../../util/earthEngine.js' import styles from './styles/PeriodSelect.module.css' -// http://localhost:8080/api/periodTypes.json const EarthEnginePeriodSelect = ({ periodType, period, datasetId, filters, - // periods, onChange, + onError, errorText, className, }) => { @@ -52,23 +51,22 @@ const EarthEnginePeriodSelect = ({ let isCancelled = false if (periodType) { - getPeriods(datasetId, periodType, filters).then((periods) => { - if (!isCancelled) { - setPeriods(periods) - } - }) - /* + getPeriods(datasetId, periodType, filters) + .then((periods) => { + if (!isCancelled) { + setPeriods(periods) + } + }) .catch((error) => - setError({ + onError({ type: 'engine', message: error.message, }) ) - */ } return () => (isCancelled = true) - }, [datasetId, periodType, filters]) + }, [datasetId, periodType, filters, onError]) // Set most recent period by default useEffect(() => { @@ -123,7 +121,6 @@ EarthEnginePeriodSelect.propTypes = { className: PropTypes.string, errorText: PropTypes.string, period: PropTypes.object, - periods: PropTypes.oneOfType([PropTypes.array, PropTypes.object]), } export default EarthEnginePeriodSelect diff --git a/src/components/edit/earthEngine/PeriodTab.js b/src/components/edit/earthEngine/PeriodTab.js index c05d4cefa..448cd68a8 100644 --- a/src/components/edit/earthEngine/PeriodTab.js +++ b/src/components/edit/earthEngine/PeriodTab.js @@ -13,6 +13,7 @@ const EarthEnginePeriodTab = ({ period, periodReducer, onChange, + onError, errorText, className, }) => { @@ -33,6 +34,7 @@ const EarthEnginePeriodTab = ({ filters={filters} period={period} onChange={onChange} + onError={onError} errorText={errorText} /> )} @@ -43,6 +45,7 @@ const EarthEnginePeriodTab = ({ EarthEnginePeriodTab.propTypes = { periodType: PropTypes.string.isRequired, onChange: PropTypes.func.isRequired, + onError: PropTypes.func.isRequired, className: PropTypes.string, errorText: PropTypes.string, period: PropTypes.object, diff --git a/src/components/layers/download/DataDownloadDialog.js b/src/components/layers/download/DataDownloadDialog.js index e11eea667..84f410432 100644 --- a/src/components/layers/download/DataDownloadDialog.js +++ b/src/components/layers/download/DataDownloadDialog.js @@ -28,6 +28,8 @@ const DataDownloadDialog = ({ layer, onCloseDialog }) => { const aggregations = useSelector((state) => state.aggregations) + const layerType = layer.layerType || layer.layer + const onChangeFormat = (format) => { setError(null) setSelectedFormat(format) @@ -88,7 +90,7 @@ const DataDownloadDialog = ({ layer, onCloseDialog }) => { 'GeoJSON is supported by most GIS software, including QGIS and ArcGIS Desktop.' )} - {layer.layer === EVENT_LAYER && ( + {layerType === EVENT_LAYER && (
<>
diff --git a/src/components/layers/overlays/AddLayerPopover.js b/src/components/layers/overlays/AddLayerPopover.js index 261a04d2c..a478d2d8f 100644 --- a/src/components/layers/overlays/AddLayerPopover.js +++ b/src/components/layers/overlays/AddLayerPopover.js @@ -17,7 +17,10 @@ const AddLayerPopover = ({ }) => { const onLayerSelect = (layer) => { const config = { ...layer } - layer.layer === EXTERNAL_LAYER ? addLayer(config) : editLayer(config) + const layerType = layer.layerType || layer.layer + + layerType === EXTERNAL_LAYER ? addLayer(config) : editLayer(config) + onClose() } diff --git a/src/components/layers/toolbar/LayerToolbarMoreMenu.js b/src/components/layers/toolbar/LayerToolbarMoreMenu.js index 619598f76..9c42328d1 100644 --- a/src/components/layers/toolbar/LayerToolbarMoreMenu.js +++ b/src/components/layers/toolbar/LayerToolbarMoreMenu.js @@ -148,7 +148,8 @@ LayerToolbarMoreMenu.propTypes = { export default connect( ({ dataTable: dataTableOpen, aggregations }, { layer = {} }) => { - const isEarthEngine = layer.layer === EARTH_ENGINE_LAYER + const layerType = layer.layerType || layer.layer + const isEarthEngine = layerType === EARTH_ENGINE_LAYER const hasOrgUnitData = layer.data && (!isEarthEngine || layer.aggregationType?.length > 0) const isLoading = diff --git a/src/components/map/MapApi.js b/src/components/map/MapApi.js index 199a3df55..4a693d8cf 100644 --- a/src/components/map/MapApi.js +++ b/src/components/map/MapApi.js @@ -6,8 +6,6 @@ import MapApi, { } from '@dhis2/maps-gl' import getMapLocale from './mapLocale.js' -console.log('poleOfInaccessibility', poleOfInaccessibility) - // Returns a new map instance const map = (options) => { const glyphs = `${options.baseUrl}/dhis-web-maps/fonts/{fontstack}/{range}.pbf` diff --git a/src/constants/earthEngine.js b/src/constants/earthEngine.js index 5ca2e1076..ae5c3e1d3 100644 --- a/src/constants/earthEngine.js +++ b/src/constants/earthEngine.js @@ -1,14 +1,12 @@ -// import i18n from '@dhis2/d2-i18n' -// import { defaultFilters } from '../util/earthEngine.js' import { EARTH_ENGINE_LAYER } from './layers.js' -console.log('EARTH_ENGINE_LAYER', EARTH_ENGINE_LAYER) - +// TODO: How to deal with translations export const earthEngineLayers = [ { - layer: EARTH_ENGINE_LAYER, // TODO: Remove? - // layerId: 'WorldPop/GP/100m/pop_age_sex_cons_unadj_TOTAL', // TODO: Remove? - img: 'images/population.png', // TODO: Remove? + layerId: 'WorldPop/GP/100m/pop_age_sex_cons_unadj_TOTAL', + layerType: EARTH_ENGINE_LAYER, + // layer: EARTH_ENGINE_LAYER, // TODO: Remove? + img: 'images/population.png', service: 'earthengine', // id: 'earthengine_population', // TODO datasetId: 'WorldPop/GP/100m/pop_age_sex_cons_unadj', @@ -688,7 +686,12 @@ export const earthEngineLayers = () => [ 'https://developers.google.com/earth-engine/datasets/catalog/MODIS_061_MCD12Q1', periodType: 'Yearly', band: 'LC_Type1', - filters: defaultFilters, + filters: ({ id, name, year }) => [{ + type: 'eq', + arguments: ['system:index', String(id)], + name, + year, + }], defaultAggregations: 'percentage', legend: { items: [ diff --git a/src/util/earthEngine.js b/src/util/earthEngine.js index c191f872c..587dfe380 100644 --- a/src/util/earthEngine.js +++ b/src/util/earthEngine.js @@ -155,15 +155,6 @@ export const getTimeRange = async (eeId) => { })) } -export const defaultFilters = ({ id, name, year }) => [ - { - type: 'eq', - arguments: ['system:index', String(id)], - name, - year, - }, -] - export const getPrecision = (values = []) => { if (values.length) { const sortedValues = [...values].sort((a, b) => a - b)