Skip to content

Commit

Permalink
chore: code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
turban committed Oct 6, 2023
1 parent 2ca7552 commit 1f569e4
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/components/edit/LayerEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
53 changes: 11 additions & 42 deletions src/components/edit/earthEngine/EarthEngineDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import {
} from '../../../constants/layers.js'
import {
getPeriodFromFilter,
getPeriods,
defaultFilters,
translateFilters,
} from '../../../util/earthEngine.js'
import { incrementDate } from '../../../util/time.js'
Expand Down Expand Up @@ -59,7 +57,7 @@ const EarthEngineDialog = (props) => {
periodType,
periodReducer,
bands,
filters, // = defaultFilters,
filters,
unit,

Check failure on line 61 in src/components/edit/earthEngine/EarthEngineDialog.js

View workflow job for this annotation

GitHub Actions / lint

'unit' is missing in props validation
source,

Check failure on line 62 in src/components/edit/earthEngine/EarthEngineDialog.js

View workflow job for this annotation

GitHub Actions / lint

'source' is missing in props validation
sourceUrl,

Check failure on line 63 in src/components/edit/earthEngine/EarthEngineDialog.js

View workflow job for this annotation

GitHub Actions / lint

'sourceUrl' is missing in props validation
Expand Down Expand Up @@ -117,7 +115,7 @@ const EarthEngineDialog = (props) => {

setFilter(periodFilter)
},
[periodType, filters, setFilter]
[/* periodType, */ filters, setFilter]
)

const noBandSelected = Array.isArray(bands) && (!band || !band.length)
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
21 changes: 9 additions & 12 deletions src/components/edit/earthEngine/PeriodSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}) => {
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/components/edit/earthEngine/PeriodTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const EarthEnginePeriodTab = ({
period,
periodReducer,
onChange,
onError,
errorText,
className,
}) => {
Expand All @@ -33,6 +34,7 @@ const EarthEnginePeriodTab = ({
filters={filters}
period={period}
onChange={onChange}
onError={onError}
errorText={errorText}
/>
)}
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/components/layers/download/DataDownloadDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -88,7 +90,7 @@ const DataDownloadDialog = ({ layer, onCloseDialog }) => {
'GeoJSON is supported by most GIS software, including QGIS and ArcGIS Desktop.'
)}
</Help>
{layer.layer === EVENT_LAYER && (
{layerType === EVENT_LAYER && (
<div className={styles.inputContainer}>
<>
<div className={styles.headingDiv}>
Expand Down
5 changes: 4 additions & 1 deletion src/components/layers/overlays/AddLayerPopover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
3 changes: 2 additions & 1 deletion src/components/layers/toolbar/LayerToolbarMoreMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 0 additions & 2 deletions src/components/map/MapApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
19 changes: 11 additions & 8 deletions src/constants/earthEngine.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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: [
Expand Down
9 changes: 0 additions & 9 deletions src/util/earthEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1f569e4

Please sign in to comment.