-
Notifications
You must be signed in to change notification settings - Fork 2
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
Chisomchima
wants to merge
2
commits into
master
Choose a base branch
from
DHIS2-18142/category-option-group-forms
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+232
−20
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]', | ||
] as const | ||
|
||
export type CategoryFormValues = PickWithFieldFilters< | ||
Category, | ||
typeof fieldFilters | ||
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update name and |
||
> | ||
|
||
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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here; let's keep this |
||
queryKey: [query], | ||
queryFn: queryFn<CategoryFormValues>, | ||
}) | ||
|
||
return ( | ||
<FormBase | ||
onSubmit={useOnSubmitEdit({ section, modelId })} | ||
section={section} | ||
initialValues={categoryQuery.data} | ||
validate={validate} | ||
> | ||
<DefaultEditFormContents section={section}> | ||
<CategoryOptionGroupFormFields /> | ||
</DefaultEditFormContents> | ||
</FormBase> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { DefaultSectionList } from '../DefaultSectionList' | ||
|
||
export const Component = DefaultSectionList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
114
src/pages/categoryOptionGroups/form/CategoryOptionGroupFormFields.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/pages/categoryOptionGroups/form/categoryOptionGroupSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.