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

Dhis2 18142/category option group forms #422

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 8 additions & 20 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-09-30T15:09:50.864Z\n"
"PO-Revision-Date: 2024-09-30T15:09:50.864Z\n"
"POT-Creation-Date: 2024-10-16T10:01:50.562Z\n"
"PO-Revision-Date: 2024-10-16T10:01:50.563Z\n"

msgid "schemas"
msgstr "schemas"
Expand Down Expand Up @@ -120,21 +120,6 @@ msgstr "Failed to load {{label}}"
msgid "Failed to load"
msgstr "Failed to load"

msgid "Download"
msgstr "Download"

msgid "Merge"
msgstr "Merge"

msgid "Delete source data element values"
msgstr "Delete source data element values"

msgid "Last updated"
msgstr "Last updated"

msgid "Discard"
msgstr "Discard"

msgid "Aggregation level(s)"
msgstr "Aggregation level(s)"

Expand Down Expand Up @@ -234,6 +219,9 @@ msgstr "Created"
msgid "Last updated by"
msgstr "Last updated by"

msgid "Last updated"
msgstr "Last updated"

msgid "Id"
msgstr "Id"

Expand All @@ -255,6 +243,9 @@ msgstr "Details"
msgid "Failed to load details"
msgstr "Failed to load details"

msgid "Download"
msgstr "Download"

msgid "Download {{section}}"
msgstr "Download {{section}}"

Expand Down Expand Up @@ -855,9 +846,6 @@ msgstr "Data dimension type"
msgid "This field requires a unique value, please choose another one"
msgstr "This field requires a unique value, please choose another one"

msgid "{{label}} (required)"
msgstr "{{label}} (required)"

msgid "No changes to be saved"
msgstr "No changes to be saved"

Expand Down
57 changes: 57 additions & 0 deletions src/pages/categoryOptionGroups/Edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react'
import { useQuery } from 'react-query'
import { useParams } from 'react-router-dom'
import { DefaultEditFormContents, FormBase } from '../../components'
import { DEFAULT_FIELD_FILTERS, SECTIONS_MAP, useOnSubmitEdit } from '../../lib'
import { useBoundResourceQueryFn } from '../../lib/query/useBoundQueryFn'
import { PickWithFieldFilters } from '../../types/generated'
import { Category } from '../../types/models'
import CategoryOptionGroupFormFields from './form/CategoryOptionGroupFormFields'
import { validate } from './form/categoryOptionGroupSchema'

const fieldFilters = [
...DEFAULT_FIELD_FILTERS,
'name',
'shortName',
'code',
'description',
'categoryOptions[id,displayName]',
'dataDimension',
'dataDimensionType',
'attributeValues[value,attribute[id]]',
Comment on lines +13 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but can we use the constant ATTRIBUTE_VALUES_FIELD_FILTERS. eg.

Suggested change
...DEFAULT_FIELD_FILTERS,
'name',
'shortName',
'code',
'description',
'categoryOptions[id,displayName]',
'dataDimension',
'dataDimensionType',
'attributeValues[value,attribute[id]]',
...DEFAULT_FIELD_FILTERS,
...ATTRIBUTE_VALUES_FIELD_FILTERS,
'name',
'shortName',
'code',
'description',
'categoryOptions[id,displayName]',
'dataDimension',
'dataDimensionType',

] as const

export type CategoryFormValues = PickWithFieldFilters<
Category,
typeof fieldFilters
Comment on lines +24 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update name and Category import to CategoryOptionGroup.

>

export const Component = () => {
const section = SECTIONS_MAP.categoryOptionGroup
const queryFn = useBoundResourceQueryFn()
const modelId = useParams().id as string
const query = {
resource: 'categoryOptionGroups',
id: modelId,
params: {
fields: fieldFilters.concat(),
},
}
const categoryQuery = useQuery({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here; let's keep this categoryOptionGroupQuery and the same for related types.

queryKey: [query],
queryFn: queryFn<CategoryFormValues>,
})

return (
<FormBase
onSubmit={useOnSubmitEdit({ section, modelId })}
section={section}
initialValues={categoryQuery.data}
validate={validate}
>
<DefaultEditFormContents section={section}>
<CategoryOptionGroupFormFields />
</DefaultEditFormContents>
</FormBase>
)
}
3 changes: 3 additions & 0 deletions src/pages/categoryOptionGroups/List.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DefaultSectionList } from '../DefaultSectionList'

export const Component = DefaultSectionList
23 changes: 23 additions & 0 deletions src/pages/categoryOptionGroups/New.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react'
import { FormBase } from '../../components'
import { DefaultNewFormContents } from '../../components/form/DefaultFormContents'
import { SECTIONS_MAP, useOnSubmitNew } from '../../lib'
import CategoryOptionGroupFormFields from './form/CategoryOptionGroupFormFields'
import { initialValues, validate } from './form/categoryOptionGroupSchema'

const section = SECTIONS_MAP.categoryOptionGroup

export const Component = () => {
return (
<FormBase
section={section}
onSubmit={useOnSubmitNew({ section })}
initialValues={initialValues}
validate={validate}
>
<DefaultNewFormContents section={section}>
<CategoryOptionGroupFormFields />
</DefaultNewFormContents>
</FormBase>
)
}
114 changes: 114 additions & 0 deletions src/pages/categoryOptionGroups/form/CategoryOptionGroupFormFields.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import i18n from '@dhis2/d2-i18n'
import { RadioFieldFF, CheckboxFieldFF } from '@dhis2/ui'
import React from 'react'
import { Field } from 'react-final-form'
import {
DefaultIdentifiableFields,
DescriptionField,
HorizontalFieldGroup,
ModelTransferField,
StandardFormField,
StandardFormSection,
StandardFormSectionDescription,
StandardFormSectionTitle,
} from '../../../components'
import { SECTIONS_MAP } from '../../../lib'

function CategoryOptionGroupFormFields() {
const section = SECTIONS_MAP.categoryOptionGroup
return (
<>
<StandardFormSection>
<StandardFormSectionTitle>
{i18n.t('Basic information')}
</StandardFormSectionTitle>
<StandardFormSectionDescription>
{i18n.t('Set up the basic information for this category.')}
</StandardFormSectionDescription>
<DefaultIdentifiableFields />
<DescriptionField
schemaSection={section}
helpText={i18n.t('Explain the purpose of this category.')}
/>
</StandardFormSection>

<StandardFormSection>
<StandardFormSectionTitle>
{i18n.t('Data configuration')}
</StandardFormSectionTitle>
<StandardFormSectionDescription>
{i18n.t(
'Choose how this category will be used to capture and analyze'
)}
</StandardFormSectionDescription>
<StandardFormField>
<HorizontalFieldGroup
label={'Data dimension type (required)'}
>
<Field<string | undefined>
name="dataDimensionType"
component={RadioFieldFF}
label={i18n.t('Disaggregation')}
type="radio"
value={'DISAGGREGATION'}
/>
<Field<string | undefined>
name="dataDimensionType"
component={RadioFieldFF}
label={i18n.t('Attribute')}
type="radio"
value={'ATTRIBUTE'}
/>
</HorizontalFieldGroup>
</StandardFormField>
<StandardFormField>
<Field
name="dataDimension"
type="checkbox"
component={CheckboxFieldFF}
label={i18n.t('Use as data dimension')}
helpText={i18n.t(
'Category will be available to the analytics as another dimension'
)}
/>
</StandardFormField>
</StandardFormSection>

<StandardFormSection>
<StandardFormSectionTitle>
<label htmlFor={'categoryOptions'}>
{i18n.t('Category options')}
</label>
</StandardFormSectionTitle>
<StandardFormSectionDescription>
{i18n.t(
'Choose the category options to include in this category.'
)}
</StandardFormSectionDescription>
<StandardFormField>
<StandardFormField>
<ModelTransferField
name="categoryOptions"
query={{
resource: 'categoryOptions',
params: {
filter: ['isDefault:eq:false'],
},
}}
leftHeader={i18n.t('Available category options')}
rightHeader={i18n.t('Selected category options')}
filterPlaceholder={i18n.t(
'Filter available category options'
)}
filterPlaceholderPicked={i18n.t(
'Filter selected category options'
)}
/>
</StandardFormField>
</StandardFormField>
</StandardFormSection>
</>
)
}

export default CategoryOptionGroupFormFields
27 changes: 27 additions & 0 deletions src/pages/categoryOptionGroups/form/categoryOptionGroupSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { z } from 'zod'
import { getDefaults, createFormValidate, modelFormSchemas } from '../../../lib'
import { CategoryOptionGroup } from '../../../types/generated'

/* Note that this describes what we send to the server,
and not what is stored in the form. */
const { identifiable, referenceCollection, withAttributeValues } =
modelFormSchemas

export const categoryOptionGroupSchema = identifiable
.merge(withAttributeValues)
.extend({
shortName: z.string().trim(),
code: z.string().trim().optional(),
description: z.string().trim().optional(),
dataDimensionType: z
.nativeEnum(CategoryOptionGroup.dataDimensionType)
.default(CategoryOptionGroup.dataDimensionType.DISAGGREGATION),
dataDimension: z.boolean().default(true),
categoryOptions: referenceCollection
.min(1, 'At least one category option is required')
.default([]),
})

export const initialValues = getDefaults(categoryOptionGroupSchema)

export const validate = createFormValidate(categoryOptionGroupSchema)
Loading