Skip to content

Commit

Permalink
fix: include event programs for export [DHIS2-17672] (#2058)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp committed Jul 1, 2024
1 parent 817abaa commit 76074ad
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/components/Inputs/ProgramPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const LISTNAME = 'selectedPrograms'
const FILTERLABEL = i18n.t('Filter programs')
const SELECTEDLABEL = i18n.t('Selected programs')
const ERRORMESSAGE = i18n.t('Something went wrong when loading the programs!')
const RESOURCETYPE = resourceTypes.PROGRAM
const PROGRAM_RESOURCE_TYPE = resourceTypes.PROGRAM
const PROGRAM_WITH_EVENTS_RESOURCE_TYPE = resourceTypes.PROGRAM_WITH_EVENTS

const SINGLE_PROGRAM_VALIDATOR = (selectedPrograms) =>
selectedPrograms.length == 0
Expand All @@ -22,18 +23,27 @@ const SINGLE_PROGRAM_VALIDATOR = (selectedPrograms) =>
const SINGLE_EXACT_PROGRAM_VALIDATOR = (selectedPrograms) =>
!selectedPrograms ? i18n.t('One program must be selected') : undefined

const ProgramPicker = ({ multiSelect, label, show, ...rest }) => {
const ProgramPicker = ({
multiSelect,
label,
show,
includeEvents,
...rest
}) => {
const programValidator = multiSelect
? SINGLE_PROGRAM_VALIDATOR
: SINGLE_EXACT_PROGRAM_VALIDATOR
const validator = composeValidators(hasValue, programValidator)
const resourceType = includeEvents
? PROGRAM_WITH_EVENTS_RESOURCE_TYPE
: PROGRAM_RESOURCE_TYPE

return (
show && (
<div style={{ maxWidth: '480px' }}>
<ResourcePickerField
name={NAME}
resourceType={RESOURCETYPE}
resourceType={resourceType}
errorMessage={ERRORMESSAGE}
listName={LISTNAME}
label={label}
Expand All @@ -54,9 +64,11 @@ ProgramPicker.defaultProps = {
label: LABEL,
multiSelect: false,
show: true,
includeEvents: false,
}

ProgramPicker.propTypes = {
includeEvents: PropTypes.bool,
label: PropTypes.string,
multiSelect: PropTypes.bool,
show: PropTypes.bool,
Expand Down
41 changes: 41 additions & 0 deletions src/components/Inputs/__tests__/ProgramPicker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ReactFinalForm } from '@dhis2/ui'
import React from 'react'
import { render } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect.js'
import { useDataQuery } from '@dhis2/app-runtime'
import { ProgramPicker } from '../ProgramPicker.js'
import {
programQuery,
programWithEventsQuery,
} from '../../ResourcePicker/queries.js'

jest.mock('@dhis2/app-runtime', () => ({
...jest.requireActual('@dhis2/app-runtime'),
// eslint-disable-next-line no-unused-vars
useDataQuery: jest.fn((query, options) => ({ refetch: () => {} })),
}))

const renderWithFormWrapper = (children) =>
render(
<ReactFinalForm.Form onSubmit={() => {}}>
{() => <form>{children}</form>}
</ReactFinalForm.Form>
)

describe('ProgramPicker', () => {
test('it invokes useDataQuery with programs query by default', () => {
renderWithFormWrapper(<ProgramPicker />)
expect(useDataQuery).toHaveBeenCalledWith(
programQuery,
expect.anything()
)
})

test('it invokes useDataQuery with programs and events query if includeEvents is true', () => {
renderWithFormWrapper(<ProgramPicker includeEvents />)
expect(useDataQuery).toHaveBeenCalledWith(
programWithEventsQuery,
expect.anything()
)
})
})
3 changes: 3 additions & 0 deletions src/components/ResourcePicker/ResourcePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
TETypeQuery,
userQuery,
geojsonAttributesQuery,
programWithEventsQuery,
} from './queries.js'
import styles from './ResourcePicker.module.css'
import { resourceTypes } from './resourceTypes.js'
Expand All @@ -26,6 +27,8 @@ const resourceToQuery = (resourceType) => {
return { resourceName: 'dataSets', query: dataSetQuery }
} else if (resourceType == resourceTypes.PROGRAM) {
return { resourceName: 'programs', query: programQuery }
} else if (resourceType == resourceTypes.PROGRAM_WITH_EVENTS) {
return { resourceName: 'programs', query: programWithEventsQuery }
} else if (resourceType == resourceTypes.TETYPE) {
return { resourceName: 'trackedEntityTypes', query: TETypeQuery }
} else if (resourceType == resourceTypes.USER) {
Expand Down
11 changes: 11 additions & 0 deletions src/components/ResourcePicker/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ const programQuery = {
},
}

const programWithEventsQuery = {
programs: {
resource: 'programs',
params: {
fields: 'id,displayName',
paging: 'false',
},
},
}

const TETypeQuery = {
trackedEntityTypes: {
resource: 'trackedEntityTypes',
Expand Down Expand Up @@ -56,6 +66,7 @@ const geojsonAttributesQuery = {
export {
dataSetQuery,
programQuery,
programWithEventsQuery,
TETypeQuery,
userQuery,
geojsonAttributesQuery,
Expand Down
1 change: 1 addition & 0 deletions src/components/ResourcePicker/resourceTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const resourceTypes = {
TETYPE: 3,
USER: 4,
GEOJSON_ATTRIBUTE: 5,
PROGRAM_WITH_EVENTS: 6,
}

export { resourceTypes }
2 changes: 1 addition & 1 deletion src/pages/EventExport/EventExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const EventExport = () => {
<BasicOptions>
<OrgUnitTree multiSelect={false} />
<Inclusion />
<ProgramPicker autoSelectFirst />
<ProgramPicker autoSelectFirst includeEvents />
<ProgramStages
selectedProgram={values.selectedPrograms}
form={form}
Expand Down

0 comments on commit 76074ad

Please sign in to comment.