Skip to content

Commit

Permalink
feat(cc field rules): change cc to default cc when domainType is TRACKER
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Nov 22, 2023
1 parent 4eaed24 commit 911879e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/pages/dataElements/fields/CategoryComboField.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import i18n from '@dhis2/d2-i18n'
import { Field } from '@dhis2/ui'
import React, { useRef } from 'react'
import { useField, useFormState } from 'react-final-form'
import React, { useEffect, useRef } from 'react'
import { useField, useForm, useFormState } from 'react-final-form'
import { useHref } from 'react-router'
import { CategoryComboSelect, EditableFieldWrapper } from '../../../components'
import { useDefaultCategoryComboQuery } from '../../../lib'
import classes from './CategoryComboField.module.css'

const required = (value: { id: string }) => {
Expand All @@ -18,10 +19,15 @@ const required = (value: { id: string }) => {
* implemented
*
* Field rule: Disable when domainType is TRACKER
* Field rule: Set categoryCombo.id to the default category combo when
* domainType is Tracker
*/
export function CategoryComboField() {
const defaultCategoryComboQuery = useDefaultCategoryComboQuery()
const { change } = useForm()
const { values } = useFormState({ subscription: { values: true } })
const disabled = values.domainType === 'TRACKER'
const domainTypeIsTracker = values.domainType === 'TRACKER'
const disabled = domainTypeIsTracker
const validate = disabled ? undefined : required
const newCategoryComboLink = useHref('/categoryCombos/new')
const { input, meta } = useField('categoryCombo', {
Expand All @@ -36,6 +42,12 @@ export function CategoryComboField() {
},
})

useEffect(() => {
if (defaultCategoryComboQuery.data?.id && domainTypeIsTracker) {
change('categoryCombo.id', defaultCategoryComboQuery.data.id)
}
}, [change, defaultCategoryComboQuery.data?.id, domainTypeIsTracker])

return (
<EditableFieldWrapper
dataTest="dataelementsformfields-categorycombo"
Expand Down

0 comments on commit 911879e

Please sign in to comment.