Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prefix time dimension ids with program id when tracked entity output type #480

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/actions/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,19 @@ export const tSetUiProgram =
(dispatch, getState) => {
const state = getState()
dispatch(acClearUiProgram())
if (sGetUiInputType(state) !== OUTPUT_TYPE_TRACKED_ENTITY) {
const inputType = sGetUiInputType(state)
if (inputType !== OUTPUT_TYPE_TRACKED_ENTITY) {
dispatch(tClearUiProgramRelatedDimensions())
}
program &&
dispatch(
acUpdateUiProgramId(program.id, {
...getProgramAsMetadata(program),
...getDynamicTimeDimensionsMetadata(program, stage),
...getDynamicTimeDimensionsMetadata(
program,
stage,
inputType
),
})
)
stage && dispatch(acUpdateUiProgramStageId(stage.id))
Expand Down
85 changes: 85 additions & 0 deletions src/modules/__tests__/metadata.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { getDynamicTimeDimensionsMetadata } from '../metadata.js'

describe('getDynamicTimeDimensionsMetadata', () => {
it('should return correct dynamic time dimensions metadata when inputType is OUTPUT_TYPE_TRACKED_ENTITY', () => {
const program = {
id: 'programId',
displayEnrollmentDateLabel: 'Custom Enrollment Date',
displayIncidentDateLabel: 'Custom Incident Date',
}
const stage = { displayExecutionDateLabel: 'Stage Execution Date' }
const inputType = 'TRACKED_ENTITY_INSTANCE' // OUTPUT_TYPE_TRACKED_ENTITY

const result = getDynamicTimeDimensionsMetadata(
program,
stage,
inputType
)

const expected = {
'programId.eventDate': {
id: 'programId.eventDate',
dimensionType: 'PERIOD',
name: 'Stage Execution Date',
},
'programId.enrollmentDate': {
id: 'programId.enrollmentDate',
dimensionType: 'PERIOD',
name: 'Custom Enrollment Date',
},
'programId.incidentDate': {
id: 'programId.incidentDate',
dimensionType: 'PERIOD',
name: 'Custom Incident Date',
},
'programId.scheduledDate': {
id: 'programId.scheduledDate',
dimensionType: 'PERIOD',
name: 'Scheduled date',
},
}

expect(result).toEqual(expected)
})

it('should return correct dynamic time dimensions metadata when inputType is not OUTPUT_TYPE_TRACKED_ENTITY', () => {
const program = {
id: 'programId',
displayEnrollmentDateLabel: 'Program Enrollment Date',
displayIncidentDateLabel: 'Custom Incident Date',
}
const stage = { displayExecutionDateLabel: 'Stage Execution Date' }
const inputType = 'OTHER_TYPE'

const result = getDynamicTimeDimensionsMetadata(
program,
stage,
inputType
)

const expected = {
eventDate: {
id: 'eventDate',
dimensionType: 'PERIOD',
name: 'Stage Execution Date',
},
enrollmentDate: {
id: 'enrollmentDate',
dimensionType: 'PERIOD',
name: 'Program Enrollment Date',
},
incidentDate: {
id: 'incidentDate',
dimensionType: 'PERIOD',
name: 'Custom Incident Date',
},
scheduledDate: {
id: 'scheduledDate',
dimensionType: 'PERIOD',
name: 'Scheduled date',
},
}

expect(result).toEqual(expected)
})
})
34 changes: 17 additions & 17 deletions src/modules/__tests__/timeDimensions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('ER > Dimensions > getTimeDimensionName', () => {
}
Object.values(getTimeDimensions()).forEach((dimension) => {
expect(getTimeDimensionName(dimension, program, stage)).toEqual(
dimension.name
dimension.defaultName
)
})
})
Expand All @@ -57,16 +57,16 @@ describe('ER > Dimensions > getTimeDimensionName', () => {

expect(
getTimeDimensionName(eventDateDimension, program, stage)
).toEqual(stage.displayExecutionDateLabel)
).toEqual('le event date')
expect(
getTimeDimensionName(enrollmentDateDimension, program, stage)
).toEqual(enrollmentDateDimension.name)
).toEqual('Enrollment date')
expect(
getTimeDimensionName(incidentDateDimension, program, stage)
).toEqual(incidentDateDimension.name)
).toEqual('Incident date')
expect(
getTimeDimensionName(scheduledDateDimension, program, stage)
).toEqual(scheduledDateDimension.name)
).toEqual('Scheduled date')
})
it('uses displayEnrollmentDateLabel from program for enrollment date', () => {
const program = {
Expand All @@ -85,16 +85,16 @@ describe('ER > Dimensions > getTimeDimensionName', () => {

expect(
getTimeDimensionName(eventDateDimension, program, stage)
).toEqual(eventDateDimension.name)
).toEqual('Event date')
expect(
getTimeDimensionName(enrollmentDateDimension, program, stage)
).toEqual(program.displayEnrollmentDateLabel)
).toEqual('le enrollment date')
expect(
getTimeDimensionName(incidentDateDimension, program, stage)
).toEqual(incidentDateDimension.name)
).toEqual('Incident date')
expect(
getTimeDimensionName(scheduledDateDimension, program, stage)
).toEqual(scheduledDateDimension.name)
).toEqual('Scheduled date')
})
it('uses displayDueDateLabel from stage for scheduled date', () => {
const program = {
Expand All @@ -113,16 +113,16 @@ describe('ER > Dimensions > getTimeDimensionName', () => {

expect(
getTimeDimensionName(eventDateDimension, program, stage)
).toEqual(eventDateDimension.name)
).toEqual('Event date')
expect(
getTimeDimensionName(enrollmentDateDimension, program, stage)
).toEqual(enrollmentDateDimension.name)
).toEqual('Enrollment date')
expect(
getTimeDimensionName(incidentDateDimension, program, stage)
).toEqual(incidentDateDimension.name)
).toEqual('Incident date')
expect(
getTimeDimensionName(scheduledDateDimension, program, stage)
).toEqual(stage.displayDueDateLabel)
).toEqual('le due date')
})
it('uses displayIncidentDateLabel from program for incident date', () => {
const program = {
Expand All @@ -141,16 +141,16 @@ describe('ER > Dimensions > getTimeDimensionName', () => {

expect(
getTimeDimensionName(eventDateDimension, program, stage)
).toEqual(eventDateDimension.name)
).toEqual('Event date')
expect(
getTimeDimensionName(enrollmentDateDimension, program, stage)
).toEqual(enrollmentDateDimension.name)
).toEqual('Enrollment date')
expect(
getTimeDimensionName(incidentDateDimension, program, stage)
).toEqual(program.displayIncidentDateLabel)
).toEqual('le incident date')
expect(
getTimeDimensionName(scheduledDateDimension, program, stage)
).toEqual(scheduledDateDimension.name)
).toEqual('Scheduled date')
})
})

Expand Down
20 changes: 16 additions & 4 deletions src/modules/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
import i18n from '@dhis2/d2-i18n'
import { getMainDimensions, getCreatedDimension } from './mainDimensions.js'
import { getProgramDimensions } from './programDimensions.js'
import { getTimeDimensions, getTimeDimensionName } from './timeDimensions.js'
import {
getTimeDimensions,
getTimeDimensionName,
getTimeDimensionMetadataId,
} from './timeDimensions.js'
import { OUTPUT_TYPE_TRACKED_ENTITY, getStatusNames } from './visualization.js'

const formatObject = (object) =>
Expand Down Expand Up @@ -70,10 +74,18 @@ export const getProgramAsMetadata = (program) => ({
[program.id]: program,
})

export const getDynamicTimeDimensionsMetadata = (program, stage) => ({
export const getDynamicTimeDimensionsMetadata = (
program,
stage,
inputType
) => ({
...Object.values(getTimeDimensions()).reduce((acc, dimension) => {
acc[dimension.id] = {
id: dimension.id,
const dimensionId = getTimeDimensionMetadataId(
jenniferarnesen marked this conversation as resolved.
Show resolved Hide resolved
dimension.id,
inputType === OUTPUT_TYPE_TRACKED_ENTITY ? program?.id : null
)
acc[dimensionId] = {
id: dimensionId,
dimensionType: dimension.dimensionType,
name: getTimeDimensionName(dimension, program, stage),
}
Expand Down
15 changes: 9 additions & 6 deletions src/modules/timeDimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,46 @@ export const getTimeDimensions = () => ({
[DIMENSION_ID_EVENT_DATE]: {
id: DIMENSION_ID_EVENT_DATE,
dimensionType: DIMENSION_TYPE_PERIOD,
name: i18n.t('Event date'),
defaultName: i18n.t('Event date'),
nameParentProperty: NAME_PARENT_PROPERTY_STAGE,
nameProperty: 'displayExecutionDateLabel',
},
[DIMENSION_ID_ENROLLMENT_DATE]: {
id: DIMENSION_ID_ENROLLMENT_DATE,
dimensionType: DIMENSION_TYPE_PERIOD,
name: i18n.t('Enrollment date'),
defaultName: i18n.t('Enrollment date'),
nameParentProperty: NAME_PARENT_PROPERTY_PROGRAM,
nameProperty: 'displayEnrollmentDateLabel',
},
[DIMENSION_ID_INCIDENT_DATE]: {
id: DIMENSION_ID_INCIDENT_DATE,
dimensionType: DIMENSION_TYPE_PERIOD,
name: i18n.t('Incident date'),
defaultName: i18n.t('Incident date'),
nameParentProperty: NAME_PARENT_PROPERTY_PROGRAM,
nameProperty: 'displayIncidentDateLabel',
},
[DIMENSION_ID_SCHEDULED_DATE]: {
id: DIMENSION_ID_SCHEDULED_DATE,
dimensionType: DIMENSION_TYPE_PERIOD,
name: i18n.t('Scheduled date'),
defaultName: i18n.t('Scheduled date'),
nameParentProperty: NAME_PARENT_PROPERTY_STAGE,
nameProperty: 'displayDueDateLabel',
},
})

export const getTimeDimensionMetadataId = (dimensionId, prefix) =>
prefix ? `${prefix}.${dimensionId}` : dimensionId

export const getTimeDimensionName = (dimension, program, stage) => {
if (!dimension.nameParentProperty || !program) {
return dimension.name
return dimension.defaultName
}
const name =
dimension.nameParentProperty === NAME_PARENT_PROPERTY_PROGRAM
? program[dimension.nameProperty]
: stage?.[dimension.nameProperty]

return name || dimension.name
return name || dimension.defaultName
}

export const getHiddenTimeDimensions = (inputType, program, stage) => {
Expand Down
35 changes: 26 additions & 9 deletions src/reducers/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ import {
getIsProgramDimensionDisabled,
getProgramDimensions,
} from '../modules/programDimensions.js'
import { getHiddenTimeDimensions } from '../modules/timeDimensions.js'
import {
getHiddenTimeDimensions,
getTimeDimensionMetadataId,
} from '../modules/timeDimensions.js'
import { getAdaptedUiByType, getUiFromVisualization } from '../modules/ui.js'
import { extractDimensionIdParts } from '../modules/utils.js'
import {
OUTPUT_TYPE_EVENT,
OUTPUT_TYPE_TRACKED_ENTITY,
Expand Down Expand Up @@ -489,17 +493,22 @@ export const useProgramDimensions = () => {
const programId = useSelector(sGetUiProgramId)
const programStageId = useSelector(sGetUiProgramStageId)

const getId = (dimensionId) =>
inputType === OUTPUT_TYPE_TRACKED_ENTITY
? getTimeDimensionMetadataId(dimensionId, programId)
: dimensionId

const eventDateDim = useSelector((state) =>
sGetMetadataById(state, DIMENSION_ID_EVENT_DATE)
sGetMetadataById(state, getId(DIMENSION_ID_EVENT_DATE))
)
const enrollmentDateDim = useSelector((state) =>
sGetMetadataById(state, DIMENSION_ID_ENROLLMENT_DATE)
sGetMetadataById(state, getId(DIMENSION_ID_ENROLLMENT_DATE))
)
const incidentDateDim = useSelector((state) =>
sGetMetadataById(state, DIMENSION_ID_INCIDENT_DATE)
sGetMetadataById(state, getId(DIMENSION_ID_INCIDENT_DATE))
)
const scheduledDateDim = useSelector((state) =>
sGetMetadataById(state, DIMENSION_ID_SCHEDULED_DATE)
sGetMetadataById(state, getId(DIMENSION_ID_SCHEDULED_DATE))
)

return useMemo(() => {
Expand All @@ -520,10 +529,18 @@ export const useProgramDimensions = () => {
? [scheduledDateDim]
: []),
incidentDateDim,
].filter(
(dimension) =>
!!dimension && !hiddenTimeDimensions.includes(dimension.id)
)
].filter((dimension) => {
// TODO this filter used to return !!dimension && !hiddenTimeDimensions.includes(dimension.id)
// is there a reason the dimension is checked for existence?
if (!dimension) {
return false
}
const { dimensionId } = extractDimensionIdParts(
dimension.id,
inputType
)
return !hiddenTimeDimensions.includes(dimensionId)
})

const programDimensions = Object.values(
getProgramDimensions(
Expand Down
Loading