-
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
feat: data set required and setup fields #474
Changes from all commits
66f3843
9da2cdf
07b12a3
89ae19b
0e4b907
e6148ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ type InitialCategoryComboQueryResult = { | |
categoryCombo: FilteredCategoryCombo | ||
} | ||
|
||
const INITIAL_OPTION_QUERY = { | ||
const INITIAL_CATEGORY_COMBO_QUERY = { | ||
categoryCombo: { | ||
resource: 'categoryCombos', | ||
id: (variables: Record<string, string>) => variables.id, | ||
|
@@ -18,23 +18,26 @@ const INITIAL_OPTION_QUERY = { | |
}, | ||
} | ||
|
||
export function useInitialOptionQuery({ | ||
export function useInitialCategoryComboQuery({ | ||
selected, | ||
onComplete, | ||
}: { | ||
onComplete: (option: SelectOption) => void | ||
selected?: string | ||
}) { | ||
const initialSelected = useRef(selected) | ||
return useDataQuery<InitialCategoryComboQueryResult>(INITIAL_OPTION_QUERY, { | ||
lazy: | ||
!initialSelected.current || | ||
initialSelected.current === DEFAULT_CATEGORY_COMBO.id, | ||
variables: { id: selected }, | ||
onComplete: (data) => { | ||
const categoryCombo = data.categoryCombo | ||
const { id: value, displayName: label } = categoryCombo | ||
onComplete({ value, label }) | ||
}, | ||
}) | ||
return useDataQuery<InitialCategoryComboQueryResult>( | ||
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. again only renaming in this file |
||
INITIAL_CATEGORY_COMBO_QUERY, | ||
{ | ||
lazy: | ||
!initialSelected.current || | ||
initialSelected.current === DEFAULT_CATEGORY_COMBO.id, | ||
variables: { id: selected }, | ||
onComplete: (data) => { | ||
const categoryCombo = data.categoryCombo | ||
const { id: value, displayName: label } = categoryCombo | ||
onComplete({ value, label }) | ||
}, | ||
} | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ export const BaseModelSingleSelect = < | |
const { allModelsMap, allSingleSelectOptions } = useMemo(() => { | ||
const allModelsMap = new Map(available.map((o) => [o.id, o])) | ||
// due to pagination, the selected model might not be in the available list, so add it | ||
if (selected && !allModelsMap.get(selected.id)) { | ||
if (selected && selected.id && !allModelsMap.get(selected.id)) { | ||
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. How can the 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. ah i added it cause i messed everything up by sending selected=undefined. But you are right, prob if i fixed teh types before that it would not have happened |
||
allModelsMap.set(selected.id, selected) | ||
} | ||
const allSingleSelectOptions = Array.from(allModelsMap).map( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ type OptionSetQueryResult = { | |
} | ||
} | ||
|
||
const CATEGORY_COMBOS_QUERY = { | ||
const OPTION_SETS_QUERY = { | ||
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. 🙃 |
||
optionSets: { | ||
resource: 'optionSets', | ||
params: (variables: Record<string, string>) => { | ||
|
@@ -35,15 +35,12 @@ const CATEGORY_COMBOS_QUERY = { | |
|
||
export function useOptionSetsQuery() { | ||
const [loadedOptions, setLoadedOptions] = useState<SelectOption[]>([]) | ||
const queryResult = useDataQuery<OptionSetQueryResult>( | ||
CATEGORY_COMBOS_QUERY, | ||
{ | ||
variables: { | ||
page: 1, | ||
filter: '', | ||
}, | ||
} | ||
) | ||
const queryResult = useDataQuery<OptionSetQueryResult>(OPTION_SETS_QUERY, { | ||
variables: { | ||
page: 1, | ||
filter: '', | ||
}, | ||
}) | ||
const { data } = queryResult | ||
|
||
// Must be done in `useEffect` and not in `onComplete`, as `onComplete` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import React from 'react' | ||
import { ModelSingleSelectField } from '../../../components/metadataFormControls/ModelSingleSelect' | ||
import { DEFAULT_CATEGORY_COMBO } from '../../../lib' | ||
|
||
const CATEGORY_COMBOS_QUERY = { | ||
resource: 'categoryCombos', | ||
params: { | ||
filter: ['dataDimensionType:eq:ATTRIBUTE'], | ||
}, | ||
} | ||
|
||
const DEFAULT_CATEGORY_SELECT_OPTION = { | ||
id: DEFAULT_CATEGORY_COMBO.id, | ||
displayName: DEFAULT_CATEGORY_COMBO.displayName, | ||
} | ||
|
||
export function CategoryComboField() { | ||
return ( | ||
<ModelSingleSelectField | ||
required | ||
name="categoryCombo" | ||
label={i18n.t('{{fieldLabel}} (required)', { | ||
fieldLabel: i18n.t('Category combination'), | ||
})} | ||
query={CATEGORY_COMBOS_QUERY} | ||
transform={(catCombos) => [ | ||
DEFAULT_CATEGORY_SELECT_OPTION, | ||
...catCombos, | ||
]} | ||
/> | ||
) | ||
} |
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.
these are just renaming