Skip to content

Commit

Permalink
refactor: restructure the utils
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkrulltott committed Feb 26, 2024
1 parent 0f23372 commit 51ace42
Show file tree
Hide file tree
Showing 27 changed files with 162 additions and 157 deletions.
2 changes: 1 addition & 1 deletion src/actions/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
DIMENSION_ID_LAST_UPDATED,
DIMENSION_ID_CREATED,
} from '../modules/dimensionConstants.js'
import { extractDimensionIdParts } from '../modules/dimensionIds.js'
import {
getDefaultTimeDimensionsMetadata,
getDynamicTimeDimensionsMetadata,
getProgramAsMetadata,
getDefaultOuMetadata,
} from '../modules/metadata.js'
import { PROGRAM_TYPE_WITH_REGISTRATION } from '../modules/programTypes.js'
import { extractDimensionIdParts } from '../modules/utils.js'
import {
OUTPUT_TYPE_EVENT,
OUTPUT_TYPE_TRACKED_ENTITY,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/visualization.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getUiDimensionType } from '../modules/dimensionConstants.js'
import { formatDimensionId } from '../modules/dimensionIds.js'
import {
getDynamicTimeDimensionsMetadata,
getProgramAsMetadata,
} from '../modules/metadata.js'
import { formatDimensionId } from '../modules/utils.js'
import { getDimensionMetadataFromVisualization } from '../modules/visualization.js'
import {
SET_VISUALIZATION,
Expand Down
2 changes: 1 addition & 1 deletion src/api/legendSets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
DIMENSION_TYPE_PROGRAM_ATTRIBUTE,
DIMENSION_TYPE_PROGRAM_INDICATOR,
} from '@dhis2/analytics'
import { extractDimensionIdParts } from '../modules/utils.js'
import { extractDimensionIdParts } from '../modules/dimensionIds.js'

const dataElementsQuery = {
resource: 'dataElements',
Expand Down
2 changes: 1 addition & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
DIMENSION_ID_EVENT_STATUS,
DIMENSION_ID_PROGRAM_STATUS,
} from '../modules/dimensionConstants.js'
import { formatDimensionId } from '../modules/dimensionIds.js'
import {
analyticsGenerationError,
analyticsRequestError,
Expand All @@ -56,7 +57,6 @@ import {
DERIVED_USER_SETTINGS_DISPLAY_NAME_PROPERTY,
USER_SETTINGS_DISPLAY_PROPERTY,
} from '../modules/userSettings.js'
import { formatDimensionId } from '../modules/utils.js'
import {
OUTPUT_TYPE_TRACKED_ENTITY,
getDimensionMetadataFields,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Dialogs/Conditions/ConditionsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
parseConditionsArrayToString,
parseConditionsStringToArray,
} from '../../../modules/conditions.js'
import { extractDimensionIdParts } from '../../../modules/utils.js'
import { extractDimensionIdParts } from '../../../modules/dimensionIds.js'
import {
OUTPUT_TYPE_ENROLLMENT,
OUTPUT_TYPE_TRACKED_ENTITY,
Expand Down
4 changes: 2 additions & 2 deletions src/components/Dialogs/FixedDimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import {
DIMENSION_ID_EVENT_STATUS,
DIMENSION_ID_PROGRAM_STATUS,
} from '../../modules/dimensionConstants.js'
import { removeLastPathSegment, getOuPath } from '../../modules/orgUnit.js'
import {
extractDimensionIdParts,
formatDimensionId,
} from '../../modules/utils.js'
} from '../../modules/dimensionIds.js'
import { removeLastPathSegment, getOuPath } from '../../modules/orgUnit.js'
import {
STATUS_ACTIVE,
STATUS_CANCELLED,
Expand Down
8 changes: 4 additions & 4 deletions src/components/Dialogs/PeriodDimension/PeriodDimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import PropTypes from 'prop-types'
import React, { useState, useMemo } from 'react'
import { useDispatch, useSelector, useStore } from 'react-redux'
import { acSetUiItems } from '../../../actions/ui.js'
import {
extractDimensionIdParts,
formatDimensionId,
} from '../../../modules/dimensionIds.js'
import {
SYSTEM_SETTINGS_HIDE_DAILY_PERIODS,
SYSTEM_SETTINGS_HIDE_WEEKLY_PERIODS,
Expand All @@ -25,10 +29,6 @@ import {
SYSTEM_SETTINGS_HIDE_BIMONTHLY_PERIODS,
} from '../../../modules/systemSettings.js'
import { USER_SETTINGS_UI_LOCALE } from '../../../modules/userSettings.js'
import {
extractDimensionIdParts,
formatDimensionId,
} from '../../../modules/utils.js'
import {
sGetDimensionIdsFromLayout,
sGetUiItemsByDimension,
Expand Down
2 changes: 1 addition & 1 deletion src/components/DndContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
acSetUiDraggingId,
} from '../actions/ui.js'
import { getConditionsTexts } from '../modules/conditions.js'
import { extractDimensionIdParts } from '../modules/utils.js'
import { extractDimensionIdParts } from '../modules/dimensionIds.js'
import { sGetMetadata } from '../reducers/metadata.js'
import {
sGetUiLayout,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Layout/DefaultLayout/DefaultAxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { acSetUiOpenDimensionModal } from '../../../actions/ui.js'
import { getAxisName } from '../../../modules/axis.js'
import { getDimensionsWithSuffix } from '../../../modules/utils.js'
import { getDimensionsWithSuffix } from '../../../modules/getDimensionsWithSuffix.js'
import { sGetMetadata } from '../../../reducers/metadata.js'
import {
sGetUiDraggingId,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Layout/TooltipContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import PropTypes from 'prop-types'
import React from 'react'
import { useSelector } from 'react-redux'
import { DIMENSION_TYPE_STATUS } from '../../modules/dimensionConstants.js'
import { extractDimensionIdParts } from '../../modules/utils.js'
import { extractDimensionIdParts } from '../../modules/dimensionIds.js'
import { sGetMetadata } from '../../reducers/metadata.js'
import { sGetUiInputType, sGetUiItemsByDimension } from '../../reducers/ui.js'
import styles from './styles/Tooltip.module.css'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import PropTypes from 'prop-types'
import React, { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { tSetUiProgram } from '../../../actions/ui.js'
import { extractDimensionIdParts } from '../../../modules/dimensionIds.js'
import { PROGRAM_TYPE_WITH_REGISTRATION } from '../../../modules/programTypes.js'
import { DERIVED_USER_SETTINGS_DISPLAY_NAME_PROPERTY } from '../../../modules/userSettings.js'
import { extractDimensionIdParts } from '../../../modules/utils.js'
import {
OUTPUT_TYPE_ENROLLMENT,
OUTPUT_TYPE_EVENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
} from '@dhis2/analytics'
import { useDataEngine } from '@dhis2/app-runtime'
import { useEffect, useReducer, useCallback, useRef, useMemo } from 'react'
import { extractDimensionIdParts } from '../../../modules/dimensionIds.js'
import { DERIVED_USER_SETTINGS_DISPLAY_NAME_PROPERTY } from '../../../modules/userSettings.js'
import { extractDimensionIdParts } from '../../../modules/utils.js'
import {
OUTPUT_TYPE_EVENT,
OUTPUT_TYPE_ENROLLMENT,
Expand Down
2 changes: 1 addition & 1 deletion src/components/MainSidebar/SelectedDimensionsContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
DIMENSION_TYPES_PROGRAM,
DIMENSION_TYPES_YOURS,
} from '../../modules/dimensionConstants.js'
import { extractDimensionIdParts } from '../../modules/dimensionIds.js'
import { getTimeDimensions } from '../../modules/timeDimensions.js'
import { extractDimensionIdParts } from '../../modules/utils.js'
import { OUTPUT_TYPE_TRACKED_ENTITY } from '../../modules/visualization.js'
import { sGetUiInputType, sGetUiLayout } from '../../reducers/ui.js'

Expand Down
2 changes: 1 addition & 1 deletion src/components/Visualization/analyticsQueryTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
DIMENSION_ID_LAST_UPDATED_BY,
DIMENSION_IDS_TIME,
} from '../../modules/dimensionConstants.js'
import { formatDimensionId } from '../../modules/dimensionIds.js'
import { getRequestOptions } from '../../modules/getRequestOptions.js'
import { formatDimensionId } from '../../modules/utils.js'
import { getHeadersMap } from '../../modules/visualization.js'

const excludedDimensions = [
Expand Down
10 changes: 5 additions & 5 deletions src/components/Visualization/useAnalyticsData.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import { useDataEngine } from '@dhis2/app-runtime'
import { useEffect, useState, useRef, useCallback } from 'react'
import { getBooleanValues, NULL_VALUE } from '../../modules/conditions.js'
import { DIMENSION_ID_PROGRAM_STATUS } from '../../modules/dimensionConstants.js'
import { getMainDimensions } from '../../modules/mainDimensions.js'
import { getProgramDimensions } from '../../modules/programDimensions.js'
import { isAoWithTimeDimension } from '../../modules/timeDimensions.js'
import {
extractDimensionIdParts,
formatDimensionId,
getDimensionsWithSuffix,
} from '../../modules/utils.js'
} from '../../modules/dimensionIds.js'
import { getDimensionsWithSuffix } from '../../modules/getDimensionsWithSuffix.js'
import { getMainDimensions } from '../../modules/mainDimensions.js'
import { getProgramDimensions } from '../../modules/programDimensions.js'
import { isAoWithTimeDimension } from '../../modules/timeDimensions.js'
import {
OUTPUT_TYPE_ENROLLMENT,
OUTPUT_TYPE_EVENT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatDimensionId, extractDimensionIdParts } from '../utils.js'
import { formatDimensionId, extractDimensionIdParts } from '../dimensionIds.js'
import {
OUTPUT_TYPE_TRACKED_ENTITY,
OUTPUT_TYPE_ENROLLMENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
DIMENSION_TYPE_PERIOD,
VALUE_TYPE_TEXT,
} from '@dhis2/analytics'
import { DIMENSION_TYPE_STATUS } from '../../../../modules/dimensionConstants.js'
import { getDimensionsWithSuffix } from '../../../../modules/utils.js'
import { DIMENSION_TYPE_STATUS } from '../dimensionConstants.js'
import { getDimensionsWithSuffix } from '../getDimensionsWithSuffix.js'
import {
OUTPUT_TYPE_ENROLLMENT,
OUTPUT_TYPE_TRACKED_ENTITY,
} from '../../../../modules/visualization.js'
} from '../visualization.js'

describe('getDimensionsWithSuffix for data elements', () => {
const metadata = {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/current.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dimensionCreate, VIS_TYPE_LINE_LIST } from '@dhis2/analytics'
import pick from 'lodash-es/pick'
import { extractDimensionIdParts } from './dimensionIds.js'
import { BASE_FIELD_TYPE } from './fields.js'
import { getAdaptedUiLayoutByType } from './layout.js'
import {
Expand All @@ -10,7 +11,6 @@ import {
OPTION_SHOW_LEGEND_KEY,
} from './options.js'
import { parseUiRepetition } from './ui.js'
import { extractDimensionIdParts } from './utils.js'
import { OUTPUT_TYPE_TRACKED_ENTITY } from './visualization.js'

export const getDefaultFromUi = (current, ui) => {
Expand Down
37 changes: 37 additions & 0 deletions src/modules/dimensionIds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { OUTPUT_TYPE_TRACKED_ENTITY } from './visualization.js'

export const formatDimensionId = ({
dimensionId,
programStageId,
programId,
outputType,
}) => {
return [
outputType === OUTPUT_TYPE_TRACKED_ENTITY ? programId : undefined,
programStageId,
dimensionId,
]
.filter((p) => p)
.join('.')
}

export const extractDimensionIdParts = (id, inputType) => {
let rawStageId
const [dimensionId, part2, part3] = id.split('.').reverse()
let programId = part3
if (part3 || inputType !== OUTPUT_TYPE_TRACKED_ENTITY) {
rawStageId = part2
}
if (inputType === OUTPUT_TYPE_TRACKED_ENTITY && !part3) {
programId = part2
}
const [programStageId, repetitionIndex] = (rawStageId || '').split('[')
return {
dimensionId,
programStageId,
...(programId ? { programId } : {}),
repetitionIndex:
repetitionIndex?.length &&
repetitionIndex.substring(0, repetitionIndex.indexOf(']')),
}
}
88 changes: 88 additions & 0 deletions src/modules/getDimensionsWithSuffix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
DIMENSION_TYPE_DATA_ELEMENT,
DIMENSION_TYPE_ORGANISATION_UNIT,
DIMENSION_TYPE_PERIOD,
} from '@dhis2/analytics'
import { DIMENSION_TYPE_STATUS } from './dimensionConstants.js'
import { extractDimensionIdParts } from './dimensionIds.js'
import {
OUTPUT_TYPE_ENROLLMENT,
OUTPUT_TYPE_TRACKED_ENTITY,
} from './visualization.js'

export const getDimensionsWithSuffix = ({
dimensionIds,
metadata,
inputType,
}) => {
const dimensions = dimensionIds.map((id) => {
const { dimensionId, programStageId, programId } =
extractDimensionIdParts(id, inputType)
const dimension = {
...metadata[id],
dimensionId,
programStageId,
programId,
}
if (!dimension.id) {
dimension.id = id
}
return dimension
})

if (
![OUTPUT_TYPE_ENROLLMENT, OUTPUT_TYPE_TRACKED_ENTITY].includes(
inputType
)
) {
return dimensions
}

return dimensions.map((dimension) => {
if (
[DIMENSION_TYPE_DATA_ELEMENT, DIMENSION_TYPE_PERIOD].includes(
dimension.dimensionType || dimension.dimensionItemType
)
) {
const duplicates = dimensions.filter(
(d) =>
d.dimensionId === dimension.dimensionId &&
d !== dimension &&
((dimension.programId && d.programId) ||
(dimension.programStageId && d.programStageId))
)

if (duplicates.length > 0) {
const sameProgramId = duplicates.find(
(dup) => dup.programId === dimension.programId
)
const thirdPartyDuplicates = duplicates
.filter((dup) => dup.programId !== dimension.programId)
.find((dpid) =>
duplicates.find(
(dup) =>
dup.programStageId !== dpid.programStageId &&
dup.programId === dpid.programId
)
)

if (sameProgramId || thirdPartyDuplicates) {
dimension.suffix = metadata[dimension.programStageId].name
} else {
dimension.suffix = metadata[dimension.programId].name
}
}
} else if (
// always suffix ou and statuses for TE
inputType === OUTPUT_TYPE_TRACKED_ENTITY &&
[DIMENSION_TYPE_ORGANISATION_UNIT, DIMENSION_TYPE_STATUS].includes(
dimension.dimensionType || dimension.dimensionItemType
) &&
dimension.programId
) {
dimension.suffix = metadata[dimension.programId].name
}

return dimension
})
}
2 changes: 1 addition & 1 deletion src/modules/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
USER_ORG_UNIT_GRANDCHILDREN,
} from '@dhis2/analytics'
import i18n from '@dhis2/d2-i18n'
import { formatDimensionId } from './dimensionIds.js'
import { getMainDimensions, getCreatedDimension } from './mainDimensions.js'
import { getProgramDimensions } from './programDimensions.js'
import { getTimeDimensions, getTimeDimensionName } from './timeDimensions.js'
import { formatDimensionId } from './utils.js'
import { OUTPUT_TYPE_TRACKED_ENTITY, getStatusNames } from './visualization.js'

const formatObject = (object) =>
Expand Down
2 changes: 1 addition & 1 deletion src/modules/programDimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
DIMENSION_TYPE_STATUS,
DIMENSION_ID_PROGRAM_STATUS,
} from './dimensionConstants.js'
import { extractDimensionIdParts } from './dimensionIds.js'
import { getDefaultOrgUnitLabel } from './metadata.js'
import { PROGRAM_TYPE_WITHOUT_REGISTRATION } from './programTypes.js'
import { extractDimensionIdParts } from './utils.js'
import {
OUTPUT_TYPE_ENROLLMENT,
OUTPUT_TYPE_EVENT,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/repetition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { layoutGetAllDimensions } from '@dhis2/analytics'
import { formatDimensionId } from './dimensionIds.js'
import { parseCurrentRepetition } from './ui.js'
import { formatDimensionId } from './utils.js'

export const getRepetitionFromVisualisation = (vis) => {
const repetitionByDimension = {}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/tableValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
DIMENSION_ID_PROGRAM_STATUS,
DIMENSION_ID_LAST_UPDATED,
} from './dimensionConstants.js'
import { extractDimensionIdParts } from './utils.js'
import { extractDimensionIdParts } from './dimensionIds.js'
import { headersMap, getStatusNames } from './visualization.js'

const getFormattedCellValue = ({ value, header = {}, visualization = {} }) => {
Expand Down
Loading

0 comments on commit 51ace42

Please sign in to comment.