Skip to content

Commit

Permalink
fix: refactor categorycomboselect
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Dec 10, 2024
1 parent 7bb5852 commit a034a76
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
import i18n from '@dhis2/d2-i18n'
import React, { forwardRef } from 'react'
import { ModelSingleSelect } from '../ModelSingleSelect/ModelSingleSelectRefactor'
import type { ModelSingleSelectProps } from '../ModelSingleSelect/ModelSingleSelectRefactor'
import { useInitialOptionQuery } from './useInitialOptionQuery'
import { useOptionsQuery } from './useOptionsQuery'
import React from 'react'
import { PlainResourceQuery } from '../../../types'
import { CategoryCombo } from '../../../types/generated'
import { DisplayableModel } from '../../../types/models'
import {
ModelSingleSelect,
ModelSingleSelectProps,
} from '../ModelSingleSelect/ModelSingleSelectRefactor'

type CategoryComboSelectProps = ModelSingleSelectProps<DisplayableModel> & {
required?: boolean
export const categoryCombosSelectQuery = {
resource: 'categoryCombos',
params: {
filter: 'isDefault:eq:false',
fields: ['id', 'displayName'],
},
} as const satisfies PlainResourceQuery

type CategoryComboSelectProps<TCategoryCombo extends DisplayableModel> = Omit<
ModelSingleSelectProps<TCategoryCombo>,
'query'
> & {
query?: PlainResourceQuery
}
export const CategoryComboSelect = forwardRef(function CategoryComboSelect(
{
onChange,
invalid,
disabled,
placeholder = i18n.t('Category combo'),
required,
selected,
showAllOption,
onBlur,
onFocus,
}: CategoryComboSelectProps,
ref
) {

export const CategoryComboSelect = <
TCategoryCombo extends Partial<CategoryCombo> & DisplayableModel = Pick<
CategoryCombo,
'id' | 'displayName'
>
>({
query,
...modelSingleSelectProps
}: CategoryComboSelectProps<TCategoryCombo>) => {
const resolvedQuery = query ?? categoryCombosSelectQuery
return (
<ModelSingleSelect
required={required}
invalid={invalid}
disabled={disabled}
useInitialOptionQuery={useInitialOptionQuery}
useOptionsQuery={useOptionsQuery}
placeholder={placeholder}
showAllOption={showAllOption}
onChange={onChange}
selected={selected}
onBlur={onBlur}
onFocus={onFocus}
/>
<ModelSingleSelect {...modelSingleSelectProps} query={resolvedQuery} />
)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type ModelSingleSelectProps<TModel extends DisplayableModel> = Omit<
| 'available'
| 'onFilterChange'
| 'onRetryClick'
| 'onEndReached'
| 'showEndLoader'
| 'loading'
| 'error'
Expand Down Expand Up @@ -67,10 +68,8 @@ export const ModelSingleSelect = <TModel extends DisplayableModel>({
lastPage.pager.nextPage ? lastPage.pager.page + 1 : undefined,
getPreviousPageParam: (firstPage) =>
firstPage.pager.prevPage ? firstPage.pager.page - 1 : undefined,
// select: select
// ? (data) => select(data.pages.flatMap((p) => p[modelName]))
// : undefined,
})

const allDataMap = useMemo(() => {
const flatData =
queryResult.data?.pages.flatMap((page) => page[modelName]) ?? []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ export const useRefreshModelSingleSelect = (

return useCallback(
(invalidateFilters?: InvalidateQueryFilters) => {
console.log('invalidate', query)
queryClient.invalidateQueries({
queryKey: [query],
...invalidateFilters,
// ...invalidateFilters,
})
},
[queryClient, query]
Expand Down
1 change: 1 addition & 0 deletions src/lib/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useInfiniteDataQuery'
export { useBoundQueryFn } from './useBoundQueryFn'
export { createBoundQueryFn } from './createBoundQueryFn'
export { mergePlainResourceQueries } from './mergeQuery'

Check failure on line 4 in src/lib/query/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unable to resolve path to module './mergeQuery'

Check failure on line 4 in src/lib/query/index.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module './mergeQuery' or its corresponding type declarations.
6 changes: 5 additions & 1 deletion src/pages/dataElements/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ function useDataElementQuery(id: string) {
dataElement: {
resource: `dataElements/${id}`,
params: {
fields: ['*', 'attributeValues[*]'],
fields: [
'*',
'categoryCombo[id,displayName]',
'attributeValues[*]',
],
},
},
}
Expand Down
30 changes: 15 additions & 15 deletions src/pages/dataElements/fields/CategoryComboField.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import i18n from '@dhis2/d2-i18n'
import React, { useEffect, useRef } from 'react'
import { useForm, useFormState } from 'react-final-form'
import React, { useEffect } from 'react'
import { useField, useForm } from 'react-final-form'
import { useHref } from 'react-router'
import { EditableFieldWrapper } from '../../../components'
import { ModelSingleSelectField } from '../../../components/metadataFormControls/ModelSingleSelect/ModelSingleSelectField'
import { useRefreshModelSingleSelect } from '../../../components/metadataFormControls/ModelSingleSelect/useRefreshSingleSelect'
import {
DEFAULT_CATEGORY_COMBO,
useDefaultCategoryComboQuery,
} from '../../../lib'
import { DEFAULT_CATEGORY_COMBO } from '../../../lib'
import { PlainResourceQuery } from '../../../types'
import classes from './CategoryComboField.module.css'

const query = {
resource: 'categoryCombos',
params: {
filter: 'isDefault:eq:false',
filter: ['isDefault:eq:false'],
fields: ['id', 'displayName'],
},
} satisfies PlainResourceQuery
} as const satisfies PlainResourceQuery

/*
* @TODO: Verify that the api ignores the category combo when it's disabled.
Expand All @@ -30,23 +28,25 @@ const query = {
*/
export function CategoryComboField() {
const refresh = useRefreshModelSingleSelect(query)
const defaultCategoryComboQuery = useDefaultCategoryComboQuery()
const { change } = useForm()
const { values } = useFormState({ subscription: { values: true } })
const domainTypeIsTracker = values.domainType === 'TRACKER'
const {
input: { value: domainTypeValue },
} = useField('domainType')

const domainTypeIsTracker = domainTypeValue === 'TRACKER'
const disabled = domainTypeIsTracker
const newCategoryComboLink = useHref('/categoryCombos/new')

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

return (
<EditableFieldWrapper
dataTest="formfields-categorycombo"
onRefresh={() => refresh}
onRefresh={() => refresh()}
onAddNew={() => window.open(newCategoryComboLink, '_blank')}
>
<div className={classes.categoryComboSelect}>
Expand Down
1 change: 1 addition & 0 deletions src/types/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type QueryParams = {
[key: string]: unknown
}

/* A simple resource-query, without params callback */
export type PlainResourceQuery = {
resource: string
id?: string
Expand Down

0 comments on commit a034a76

Please sign in to comment.