-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(categoryCombo): add validation and errors for categorycombo
- Loading branch information
Showing
9 changed files
with
257 additions
and
24 deletions.
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 |
---|---|---|
|
@@ -33,4 +33,5 @@ export const modelFormSchemas = { | |
identifiable, | ||
attributeValues, | ||
withAttributeValues, | ||
modelReference, | ||
} |
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,153 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import { Field, NoticeBox } from '@dhis2/ui' | ||
import React from 'react' | ||
import { useField } from 'react-final-form' | ||
import { NavLink, useParams } from 'react-router-dom' | ||
import { ModelTransfer } from '../../../components' | ||
import { DisplayableModel } from '../../../types/models' | ||
import css from './CategoryCombo.module.css' | ||
import { CategoryComboFormValues } from './categoryComboSchema' | ||
import { useIdenticalCategoryCombosQuery } from './useIdenticalCategoryCombosQuery' | ||
|
||
type CategoriesValue = Pick<CategoryComboFormValues, 'categories'>['categories'] | ||
|
||
const query = { | ||
resource: 'categories', | ||
params: { | ||
fields: [ | ||
'id', | ||
'displayName', | ||
'categoryOptions~size~rename(categoryOptionsSize)', | ||
], | ||
order: 'displayName:asc', | ||
filter: ['name:ne:default'], | ||
}, | ||
} | ||
|
||
const fieldName = 'categories' | ||
const numberFormat = new Intl.NumberFormat() | ||
|
||
export function CategoriesField() { | ||
const catComboId = useParams()?.id | ||
|
||
const { input, meta } = useField< | ||
Pick<CategoryComboFormValues, 'categories'>['categories'] | ||
>(fieldName, { | ||
multiple: true, | ||
validateFields: [], | ||
}) | ||
|
||
const generatedCocsCount = | ||
input.value?.reduce((acc, cat) => acc * cat.categoryOptionsSize, 1) || 0 | ||
return ( | ||
<div className={css.categoriesFieldWrapper}> | ||
<Field | ||
dataTest="formfields-modeltransfer" | ||
error={meta.invalid} | ||
validationText={(meta.touched && meta.error?.toString()) || ''} | ||
name={fieldName} | ||
> | ||
<ModelTransfer | ||
selected={input.value} | ||
onChange={({ selected }) => { | ||
input.onChange(selected) | ||
input.onBlur() | ||
}} | ||
leftHeader={i18n.t('Available categories')} | ||
rightHeader={i18n.t('Selected categories')} | ||
filterPlaceholder={i18n.t('Filter available categories')} | ||
filterPlaceholderPicked={i18n.t( | ||
'Filter selected categories' | ||
)} | ||
query={query} | ||
maxSelections={200} | ||
/> | ||
</Field> | ||
<div className={css.categoriesNoticesWrapper}> | ||
{!catComboId && generatedCocsCount > 1 && ( | ||
<NoticeBox warning={generatedCocsCount > 250}> | ||
{i18n.t( | ||
'{{count}} category option combinations will be generated.', | ||
{ | ||
count: numberFormat.format(generatedCocsCount), | ||
} | ||
)} | ||
</NoticeBox> | ||
)} | ||
<CategoriesFieldWarnings | ||
catComboId={catComboId} | ||
selectedCategories={input.value} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
const CategoriesFieldWarnings = ({ | ||
selectedCategories, | ||
catComboId, | ||
}: { | ||
selectedCategories: CategoriesValue | ||
catComboId?: string | ||
}) => { | ||
const isMoreThanRecommended = selectedCategories.length > 4 | ||
const identicalCatCombosResult = useIdenticalCategoryCombosQuery({ | ||
categoryComboId: catComboId, | ||
selectedCategories, | ||
enabled: !isMoreThanRecommended, | ||
}) | ||
const isIdenticalCatCombos = | ||
identicalCatCombosResult.isSuccess && | ||
identicalCatCombosResult.data?.pager.total > 0 | ||
|
||
if (!isMoreThanRecommended && !isIdenticalCatCombos) { | ||
return null | ||
} | ||
|
||
return ( | ||
<> | ||
{isIdenticalCatCombos && ( | ||
<IdenticalCategoryComboWarning | ||
categoryCombos={ | ||
identicalCatCombosResult.data.categoryCombos | ||
} | ||
/> | ||
)} | ||
{isMoreThanRecommended && ( | ||
<NoticeBox warning title={i18n.t('More than 4 Categories')}> | ||
{i18n.t( | ||
'A Category combination with more than 4 categories is not recommended.' | ||
)} | ||
</NoticeBox> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
const IdenticalCategoryComboWarning = ({ | ||
categoryCombos, | ||
}: { | ||
categoryCombos: DisplayableModel[] | ||
}) => { | ||
return ( | ||
<NoticeBox warning title={i18n.t('Identical Category Combination')}> | ||
{i18n.t( | ||
`One or more Category combinations with the same categories already exist in the system. | ||
It is strongly discouraged to have more than one Category combination with the same categories.` | ||
)} | ||
<br /> | ||
{i18n.t( | ||
'The following Category combinations have identical categories:' | ||
)} | ||
<ul className={css.identicalCategoriesList}> | ||
{categoryCombos.map((catCombo) => ( | ||
<li key={catCombo.id}> | ||
<NavLink target="_blank" to={`../${catCombo.id}`}> | ||
{catCombo.displayName} | ||
</NavLink> | ||
</li> | ||
))} | ||
</ul> | ||
</NoticeBox> | ||
) | ||
} |
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,21 @@ | ||
.categoriesFieldWrapper { | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: var(--spacers-dp8); | ||
} | ||
|
||
.categoriesNoticesWrapper { | ||
display: flex; | ||
flex-direction: column; | ||
gap: var(--spacers-dp8); | ||
max-width: 570px; | ||
} | ||
|
||
.identicalCategoriesList a { | ||
color: var(--colors-blue600); | ||
} | ||
|
||
.identicalCategoriesList { | ||
margin-inline: 0; | ||
margin-block: var(--spacers-dp8); | ||
} |
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
Empty file.
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
51 changes: 51 additions & 0 deletions
51
src/pages/categoryCombos/form/useIdenticalCategoryCombosQuery.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,51 @@ | ||
import { useQuery, QueryObserverOptions } from 'react-query' | ||
import { useBoundResourceQueryFn } from '../../../lib/query/useBoundQueryFn' | ||
import { PagedResponse } from '../../../types/generated' | ||
import { CategoryComboFormValues } from './categoryComboSchema' | ||
|
||
type CategoriesValue = Pick<CategoryComboFormValues, 'categories'>['categories'] | ||
|
||
export type IdenticalCategoryCombosQueryResult = PagedResponse< | ||
CategoriesValue[number], | ||
'categoryCombos' | ||
> | ||
|
||
type UseIdenticalCategoryCombosQuery = { | ||
categoryComboId?: string | ||
selectedCategories: CategoriesValue | ||
} & Pick< | ||
QueryObserverOptions<IdenticalCategoryCombosQueryResult>, | ||
'enabled' | 'select' | 'cacheTime' | 'staleTime' | ||
> | ||
|
||
export const useIdenticalCategoryCombosQuery = ({ | ||
categoryComboId, | ||
selectedCategories, | ||
...queryOptions | ||
}: UseIdenticalCategoryCombosQuery) => { | ||
const queryFn = useBoundResourceQueryFn() | ||
const notSameCatComboFilter = `id:ne:${categoryComboId}` | ||
const idFilters = selectedCategories.map( | ||
(category) => `categories.id:eq:${category.id}` | ||
) | ||
const lengthFilter = `categories:eq:${selectedCategories.length}` | ||
const filters = [lengthFilter, ...idFilters] | ||
|
||
if (categoryComboId) { | ||
filters.push(notSameCatComboFilter) | ||
} | ||
const useIdenticalCategoryCombosQuery = { | ||
resource: 'categoryCombos', | ||
params: { | ||
fields: ['id', 'displayName'], | ||
filter: filters, | ||
}, | ||
} | ||
|
||
return useQuery({ | ||
staleTime: 60 * 1000, | ||
...queryOptions, | ||
queryKey: [useIdenticalCategoryCombosQuery], | ||
queryFn: queryFn<IdenticalCategoryCombosQueryResult>, | ||
}) | ||
} |
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