Skip to content

Commit

Permalink
fix: dataElementGroup no longer needed to fetch current data values (#…
Browse files Browse the repository at this point in the history
…1503)

* fix: dataElementGroup no longer needed to fetch current data values

* fix: data element placeholder
  • Loading branch information
jenniferarnesen committed Oct 11, 2022
1 parent 742889a commit 9a9602f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 24 deletions.
3 changes: 3 additions & 0 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,9 @@ msgstr "Select associated geometry for selected organisation units"
msgid "Data element"
msgstr "Data element"

msgid "Select data element"
msgstr "Select data element"

msgid "The data element where Earth Engine data will be added"
msgstr "The data element where Earth Engine data will be added"

Expand Down
2 changes: 1 addition & 1 deletion src/pages/EarthEngineImport/EarthEngineImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const query = {
dataElements: {
resource: 'dataElements',
params: {
fields: 'id,displayName~rename(name),categoryCombo[categoryOptionCombos[id,code,displayName~rename(name)]],dataElementGroups',
fields: 'id,displayName~rename(name),categoryCombo[categoryOptionCombos[id,code,displayName~rename(name)]]',
filter: 'domainType:eq:AGGREGATE',
paging: false,
},
Expand Down
1 change: 1 addition & 0 deletions src/pages/EarthEngineImport/components/DataElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const DataElements = () => {
component={SingleSelectFieldFF}
name={DATA_ELEMENT_ID}
label={i18n.t('Data element')}
placeholder={i18n.t('Select data element')}
options={dataElements}
dataTest="input-data-elements"
helpText={i18n.t(
Expand Down
27 changes: 4 additions & 23 deletions src/pages/EarthEngineImport/components/useFetchCurrentValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useConfig } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import { ReactFinalForm } from '@dhis2/ui'
import { useState, useEffect } from 'react'
import { useCachedDataQuery } from '../util/CachedQueryProvider.js'

const { useFormState } = ReactFinalForm

Expand All @@ -30,14 +29,10 @@ const useFetchCurrentValues = () => {
const { values } = useFormState()
const { earthEngineId, organisationUnits, dataElementId, period } = values

const { dataElements } = useCachedDataQuery()
const [currentValues, setCurrentValues] = useState([])
const [error, setError] = useState(null)
const { baseUrl } = useConfig()

const dataElement = dataElements.find((el) => el.id === dataElementId)
const dataElementGroupId = dataElement?.dataElementGroups[0]?.id

useEffect(() => {
const fetchCurrVals = async (url) => {
const { dataValues } = await fetchCurrentValues(url)
Expand All @@ -46,12 +41,7 @@ const useFetchCurrentValues = () => {
setCurrentValues(cv)
}

if (
organisationUnits &&
period &&
dataElementId &&
dataElementGroupId
) {
if (organisationUnits && period && dataElementId) {
setError(null)
const ouQueryParams = organisationUnits
.map(({ id }) => `orgUnit=${id}`)
Expand All @@ -60,20 +50,18 @@ const useFetchCurrentValues = () => {
const hasLevelPrefix = organisationUnits.find((ou) =>
ouIdHelper.hasLevelPrefix(ou.id)
)
let url = `${baseUrl}/api/dataValueSets?dataElementGroup=${dataElementGroupId}&period=${period}&${ouQueryParams}`
let url = `${baseUrl}/api/dataValueSets?dataElement=${dataElementId}&period=${period}&${ouQueryParams}`
if (hasLevelPrefix) {
url = url.concat('&children=true')
}

fetchCurrVals(url)
} else {
const missingDEG =
dataElementGroupId === undefined ? 'Data element group' : null
const missingPeriod = period === undefined ? 'Period' : null
const missingOus = !organisationUnits.length
? 'Organisation units'
: null
const missingParams = [missingDEG, missingPeriod, missingOus]
const missingParams = [missingPeriod, missingOus]
.filter((m) => m !== null)
.join(', ')
setError(
Expand All @@ -82,14 +70,7 @@ const useFetchCurrentValues = () => {
)
)
}
}, [
earthEngineId,
dataElementId,
dataElementGroupId,
period,
organisationUnits,
baseUrl,
])
}, [earthEngineId, dataElementId, period, organisationUnits, baseUrl])

return { currentValues, error }
}
Expand Down

0 comments on commit 9a9602f

Please sign in to comment.