Skip to content

Commit

Permalink
refactor: use shared <CustomAttributes />
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Mar 5, 2024
1 parent b6a80c6 commit 562dfb7
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 136 deletions.
1 change: 1 addition & 0 deletions src/components/form/attributes/CustomAttributes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function CustomAttributes() {
const customAttributes = formState.initialValues.attributeValues?.map(
(av) => av.attribute
)
console.log('> <CustomAttribute /> customAttributes:', customAttributes)

return (
<>
Expand Down
1 change: 1 addition & 0 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './ColorAndIconPicker'
export * from './EditableFieldWrapper'
export * from './form'
export { HidePreventUnmount } from './HidePreventUnmount'
export { Loader } from './loading'
export * from './metadataFormControls'
Expand Down
3 changes: 2 additions & 1 deletion src/pages/dataElementGroups/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ function computeInitialValues({
dataElementGroup,
customAttributes
)
console.log('> customAttributes', customAttributes)
console.log('> attributeValues', attributeValues)

return {
id: dataElementGroup.id,
Expand Down Expand Up @@ -150,7 +152,6 @@ function DataElementGroupForm({
submitError={submitError}
>
<DataElementGroupFormFields />
<CustomAttributes />
</DefaultFormContents>
</form>
)}
Expand Down
13 changes: 13 additions & 0 deletions src/pages/dataElementGroups/form/DataElementGroupFormFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
StandardFormSectionTitle,
StandardFormSectionDescription,
StandardFormField,
CustomAttributes,
} from '../../../components'
import {
DefaultIdentifiableFields,
Expand Down Expand Up @@ -54,6 +55,18 @@ export function DataElementGroupFormFields() {
<DataElementsField />
</StandardFormField>
</StandardFormSection>

<StandardFormSection>
<StandardFormSectionTitle>
{i18n.t('Custom attributes')}
</StandardFormSectionTitle>

<StandardFormSectionDescription>
{i18n.t('Custom fields for your DHIS2 instance')}
</StandardFormSectionDescription>

<CustomAttributes />
</StandardFormSection>
</>
)
}
18 changes: 3 additions & 15 deletions src/pages/dataElements/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../../components'
import { useCustomAttributesQuery } from '../../components/form'
import { SCHEMA_SECTIONS, getSectionPath, validate } from '../../lib'
import { getAllAttributeValues } from '../../lib/models/attributes'
import { JsonPatchOperation } from '../../types'
import { Attribute, DataElement } from '../../types/generated'
import { createJsonPatchOperations } from './edit/'
Expand Down Expand Up @@ -49,28 +50,15 @@ function computeInitialValues({
dataElement,
customAttributes,
}: {
dataElementId: string
dataElement: DataElement
customAttributes: Attribute[]
dataElementId: string
}) {
if (!dataElement) {
return {}
}

// We want to have an array in the state with all attributes, not just the
// ones that have a value which is what the endpoint responds with
const attributeValues = customAttributes.map((attribute) => {
const attributeValue = dataElement.attributeValues.find(
(currentAttributeValue) =>
attribute.id === currentAttributeValue.attribute.id
)

if (!attributeValue) {
return { attribute, value: '' }
}

return attributeValue
})
const attributeValues = getAllAttributeValues(dataElement, customAttributes)

return {
id: dataElementId,
Expand Down
118 changes: 0 additions & 118 deletions src/pages/dataElements/fields/CustomAttributes.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/pages/dataElements/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export { AggregationLevelsField } from './AggregationLevelsField'
export { AggregationTypeField } from './AggregationTypeField'
export { CategoryComboField } from './CategoryComboField'
export { ColorAndIconField } from './ColorAndIconField'
export { CustomAttributes } from './CustomAttributes'
export { DomainField } from './DomainField'
export { FieldMaskField } from './FieldMaskField'
export { FormNameField } from './FormNameField'
Expand Down
2 changes: 1 addition & 1 deletion src/pages/dataElements/form/DataElementFormFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
StandardFormSectionTitle,
StandardFormSectionDescription,
StandardFormField,
CustomAttributes,
} from '../../../components'
import {
CodeField,
Expand All @@ -18,7 +19,6 @@ import {
AggregationTypeField,
CategoryComboField,
ColorAndIconField,
CustomAttributes,
DomainField,
FieldMaskField,
FormNameField,
Expand Down

0 comments on commit 562dfb7

Please sign in to comment.