Skip to content

Commit

Permalink
chore: orgUnitLoader fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jenniferarnesen committed Oct 30, 2024
1 parent eba2e57 commit 4cfb808
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 44 deletions.
30 changes: 14 additions & 16 deletions src/hooks/useLayersLoader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Analytics, useCachedDataQuery } from '@dhis2/analytics'
import { useDataEngine } from '@dhis2/app-runtime'
import { useDataEngine, useConfig } from '@dhis2/app-runtime'
import { useAlert } from '@dhis2/app-service-alerts'
import { useState, useEffect } from 'react'
import { useSelector, useDispatch } from 'react-redux'
Expand All @@ -24,6 +24,7 @@ const loaders = {
}

export const useLayersLoader = () => {
const { baseUrl } = useConfig()
const engine = useDataEngine()
const [analyticsEngine] = useState(() => Analytics.getAnalytics(engine))
const { show: showLoaderAlert } = useAlert(
Expand All @@ -33,15 +34,7 @@ export const useLayersLoader = () => {
const { nameProperty } = useCachedDataQuery()
const { showAlerts } = useLoaderAlerts()

// only load layers that have not yet been loaded
const layers = useSelector((state) =>
state.map.mapViews.filter(
(layer) =>
!layer.isLoading &&
(!layer.isLoaded || (layer.showDataTable && !layer.isExtended))
)
)
console.log('jj useLayersLoader with layers', layers)
const allLayers = useSelector((state) => state.map.mapViews)
const dispatch = useDispatch()

useEffect(() => {
Expand All @@ -51,33 +44,38 @@ export const useLayersLoader = () => {
displayProperty: nameProperty,
engine,
analyticsEngine,
baseUrl,
})

if (result.alerts) {
showAlerts(result.alerts)
}

dispatch(updateLayer(result))
}

layers.forEach((layerConfig) => {
const loader = loaders[layerConfig.layer]
const unloadedLayers = allLayers.filter(
(layer) =>
!layer.isLoading &&
(!layer.isLoaded || (layer.showDataTable && !layer.isExtended))
)

// only load layers that have not yet been loaded
unloadedLayers.forEach((layerConfig) => {
const loader = loaders[layerConfig.layer]
if (!loader) {
showLoaderAlert({ layer: layerConfig.layer })
return
}

dispatch(setLayerLoading(layerConfig.id))
loadLayer(layerConfig, loader)
})
}, [
layers,
allLayers,
dispatch,
nameProperty,
engine,
analyticsEngine,
showAlerts,
showLoaderAlert,
baseUrl,
])
}
6 changes: 3 additions & 3 deletions src/loaders/facilityLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ const facilityLoader = async ({ config, engine, nameProperty, baseUrl }) => {
})
}

const { styledFeatures, legend } = getStyledOrgUnits(
const { styledFeatures, legend } = getStyledOrgUnits({
features,
groupSet,
config,
baseUrl
)
baseUrl,
})

legend.title = name

Expand Down
8 changes: 4 additions & 4 deletions src/loaders/orgUnitLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,19 @@ const orgUnitLoader = async ({ config, engine, nameProperty, baseUrl }) => {

const features = addAssociatedGeometries(mainFeatures, associatedGeometries)

const { styledFeatures, legend } = getStyledOrgUnits(
const { styledFeatures, legend } = getStyledOrgUnits({
features,
groupSet,
config,
baseUrl,
orgUnitLevels.organisationUnitLevels.reduce(
orgUnitLevels: orgUnitLevels.reduce(
(obj, item) => ({
...obj,
[item.level]: item.name,
}),
{}
)
)
),
})

legend.title = name

Expand Down
23 changes: 11 additions & 12 deletions src/util/__tests__/orgUnits.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ describe('getStyledOrgUnits', () => {
name: 'GroupSet1',
organisationUnitGroups: [],
}
const styleParams = { organisationUnitColor: 'red', radiusLow: 10 }
const contextPath = '/contextPath'
const result = getStyledOrgUnits(
const config = { organisationUnitColor: 'red', radiusLow: 10 }
const result = getStyledOrgUnits({
features,
groupSet,
styleParams,
contextPath
)
config,
baseUrl: '/baseUrl',
})

expect(result.styledFeatures).toHaveLength(1)
expect(result.styledFeatures[0]).toMatchObject({
Expand All @@ -49,7 +48,7 @@ describe('getStyledOrgUnits', () => {
]
const groupSet = { name: 'GroupSet1', organisationUnitGroups: [] }

const styleParams = { organisationUnitColor: 'red', radiusLow: 10 }
const config = { organisationUnitColor: 'red', radiusLow: 10 }

const orgUnitLevels = {
1: 'Level1',
Expand All @@ -58,13 +57,13 @@ describe('getStyledOrgUnits', () => {
4: 'Level4',
}

const result = getStyledOrgUnits(
const result = getStyledOrgUnits({
features,
groupSet,
styleParams,
'/contextPath',
orgUnitLevels
)
config,
baseUrl: '/contextPath',
orgUnitLevels,
})

expect(result.styledFeatures).toHaveLength(1)
expect(result.styledFeatures[0]).toMatchObject({
Expand Down
19 changes: 10 additions & 9 deletions src/util/orgUnits.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ export const getOrgUnitGroupLegendItems = (
}
)

/* eslint-disable max-params */
export const getStyledOrgUnits = (
export const getStyledOrgUnits = ({
features = [],
groupSet = {},
{ organisationUnitColor = ORG_UNIT_COLOR, radiusLow = ORG_UNIT_RADIUS },
contextPath,
orgUnitLevels
) => {
config: {
organisationUnitColor = ORG_UNIT_COLOR,
radiusLow = ORG_UNIT_RADIUS,
},
baseUrl,
orgUnitLevels,
}) => {
const {
name,
styleType = orgUnitLevels ? STYLE_TYPE_COLOR : STYLE_TYPE_SYMBOL,
Expand Down Expand Up @@ -140,7 +142,7 @@ export const getStyledOrgUnits = (
if (useColor && color) {
properties.color = hasAdditionalGeometry ? ORG_UNIT_COLOR : color
} else if (symbol) {
properties.iconUrl = `${contextPath}/images/orgunitgroup/${symbol}`
properties.iconUrl = `${baseUrl}/images/orgunitgroup/${symbol}`
}

if (properties.level && levelWeight) {
Expand All @@ -161,7 +163,7 @@ export const getStyledOrgUnits = (
const groupItems = getOrgUnitGroupLegendItems(
organisationUnitGroups,
useColor,
contextPath
baseUrl
)

const facilityItems =
Expand All @@ -183,7 +185,6 @@ export const getStyledOrgUnits = (
},
}
}
/* eslint-enable max-params */

// Converts "LEVEL-x" to newer "LEVEL-uid" format
export const translateOrgUnitLevels = (orgUnits, orgUnitLevels = []) => {
Expand Down

0 comments on commit 4cfb808

Please sign in to comment.