Skip to content

Commit

Permalink
chore: move functions to separate file and add jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jenniferarnesen committed Sep 29, 2023
1 parent 7461316 commit 71c7b2d
Show file tree
Hide file tree
Showing 4 changed files with 843 additions and 89 deletions.
88 changes: 2 additions & 86 deletions src/AppWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@ import { Provider as ReduxProvider } from 'react-redux'
import App from './components/app/App.js'
import OrgUnitsProvider from './components/OrgUnitsProvider.js'
import WindowDimensionsProvider from './components/WindowDimensionsProvider.js'
import { defaultBasemaps } from './constants/basemaps.js'
import { BING_LAYER } from './constants/layers.js'
import {
DEFAULT_SYSTEM_SETTINGS,
SYSTEM_SETTINGS,
getHiddenPeriods,
} from './constants/settings.js'
import store from './store/index.js'
import { USER_DATASTORE_NAMESPACE } from './util/analyticalObject.js'
import { createExternalLayer } from './util/external.js'
import { getDefaultLayerTypes } from './util/getDefaultLayerTypes.js'
import { fetchExternalLayersQuery } from './util/requests.js'
import { appQueries, providerDataTransformation } from './util/app.js'
import './locales/index.js'

log.setLevel(
Expand Down Expand Up @@ -47,81 +38,6 @@ const d2Config = {
],
}

const query = {
currentUser: {
resource: 'me',
params: {
fields: 'id,username,displayName~rename(name),settings[keyAnalysisDisplayProperty]',
},
},
systemSettings: {
resource: 'systemSettings',
params: {
key: SYSTEM_SETTINGS,
},
},
externalMapLayers: fetchExternalLayersQuery,
}

const getBasemapList = (externalMapLayers, systemSettings) => {
const externalBasemaps = externalMapLayers
.filter((layer) => layer.mapLayerPosition === 'BASEMAP')
.map(createExternalLayer)

return defaultBasemaps()
.filter((basemap) =>
!systemSettings.keyBingMapsApiKey
? basemap.config.type !== BING_LAYER
: true
)
.map((basemap) => {
if (basemap.config.type === BING_LAYER) {
basemap.config.apiKey = systemSettings.keyBingMapsApiKey
}
return basemap
})
.concat(externalBasemaps)
}

const getLayerTypes = (externalMapLayers) => {
const externalLayerTypes = externalMapLayers
.filter((layer) => layer.mapLayerPosition !== 'BASEMAP')
.map(createExternalLayer)

return getDefaultLayerTypes().concat(externalLayerTypes)
}

const providerDataTransformation = ({
currentUser,
systemSettings,
externalMapLayers,
}) => {
return {
currentUser: {
id: currentUser.id,
name: currentUser.name,
username: currentUser.username,
},
nameProperty:
currentUser.settings.keyAnalysisDisplayProperty === 'name'
? 'displayName'
: 'displayShortName',
systemSettings: Object.assign(
{},
DEFAULT_SYSTEM_SETTINGS,
systemSettings,
{
hiddenPeriods: getHiddenPeriods(systemSettings),
}
),
basemaps: getBasemapList(
externalMapLayers.externalMapLayers,
systemSettings
),
layerTypes: getLayerTypes(externalMapLayers.externalMapLayers),
}
}

const AppWrapper = () => {
return (
<ReduxProvider store={store}>
Expand All @@ -146,7 +62,7 @@ const AppWrapper = () => {
}
return (
<CachedDataQueryProvider
query={query}
query={appQueries}
dataTransformation={providerDataTransformation}
>
<WindowDimensionsProvider>
Expand Down
4 changes: 1 addition & 3 deletions src/components/interpretations/InterpretationsPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
InterpretationModal,
useCachedDataQuery,
} from '@dhis2/analytics'
import { useD2 } from '@dhis2/app-runtime-adapter-d2'
import PropTypes from 'prop-types'
import React, { useState, useRef, useCallback } from 'react'
import { connect } from 'react-redux'
Expand All @@ -21,7 +20,6 @@ const InterpretationsPanel = ({
const { currentUser } = useCachedDataQuery
const [initialFocus, setInitialFocus] = useState(false)
const interpretationsUnitRef = useRef()
const { d2 } = useD2()

const onInterpretationClick = useCallback(
(interpretationId) => {
Expand Down Expand Up @@ -58,7 +56,7 @@ const InterpretationsPanel = ({
</Drawer>
{interpretationId && (
<InterpretationModal
currentUser={d2.currentUser}
currentUser={currentUser}
onInterpretationUpdate={() =>
interpretationsUnitRef.current.refresh()
}
Expand Down
Loading

0 comments on commit 71c7b2d

Please sign in to comment.