-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(object): add ObjectList and ObjectType components
- Loading branch information
Showing
3 changed files
with
194 additions
and
27 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { FormSpy, useForm } from 'react-final-form' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import React, { useEffect } from 'react' | ||
import i18n from '@dhis2/d2-i18n' | ||
|
||
import { Field } from '../Field/Field' | ||
import { Label } from '../Field/Label' | ||
import { Select } from '../FinalFormComponents/Select' | ||
import { fetchObjects } from '../../reducers/object/thunks' | ||
import { | ||
getObjects, | ||
getObjectsError, | ||
getObjectsLoading, | ||
} from '../../reducers/object/selectors' | ||
|
||
export const OBJECT_DEFAULT_OPTIONS = [] | ||
export const OBJECT_KEY = 'objectList' | ||
export const OBJECT_DEFAULT_VALUE = null | ||
|
||
const objectLabel = i18n.t('Object') | ||
|
||
const RenderComponent = ({ values }) => { | ||
const form = useForm() | ||
const dispatch = useDispatch() | ||
const objectType = values ? values.objectType : null | ||
|
||
useEffect(() => { | ||
if (objectType) { | ||
dispatch(fetchObjects(objectType)).then(({ payload }) => { | ||
const { objects } = payload | ||
|
||
if (objects.length) { | ||
form.change(OBJECT_KEY, objects[0].value) | ||
} | ||
}) | ||
} | ||
}, [objectType]) // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
return null | ||
} | ||
|
||
export const ObjectList = () => { | ||
let component | ||
const objects = useSelector(getObjects) | ||
const loading = useSelector(getObjectsLoading) | ||
const error = useSelector(getObjectsError) | ||
|
||
if (loading) { | ||
component = <ObjectLoading /> | ||
} else if (error) { | ||
component = <ObjectError error={error} /> | ||
} else { | ||
const options = [...OBJECT_DEFAULT_OPTIONS, ...objects] | ||
component = ( | ||
<Field> | ||
<Select | ||
name={OBJECT_KEY} | ||
label={objectLabel} | ||
options={options} | ||
dataTest="input-object" | ||
/> | ||
</Field> | ||
) | ||
} | ||
|
||
return ( | ||
<> | ||
<FormSpy | ||
subscription={{ values: { objectType: true } }} | ||
render={({ values }) => <RenderComponent values={values} />} | ||
/> | ||
|
||
{component} | ||
</> | ||
) | ||
} | ||
|
||
export const ObjectLoading = () => ( | ||
<Field> | ||
<Label>{objectLabel}</Label> | ||
{i18n.t('Loading Object options...')} | ||
</Field> | ||
) | ||
|
||
export const ObjectError = ({ error }) => ( | ||
<Field> | ||
<Label>{objectLabel}</Label> | ||
{i18n.t('Something went wrong when loading the object!')} | ||
<br /> | ||
{error} | ||
</Field> | ||
) |
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,60 @@ | ||
import React from 'react' | ||
import i18n from '@dhis2/d2-i18n' | ||
|
||
import { Field } from '../Field/Field' | ||
import { Select } from '../FinalFormComponents/Select' | ||
|
||
export const OPTION_DATA_SETS = { | ||
value: 'dataSets', | ||
label: i18n.t('Data sets'), | ||
} | ||
|
||
export const OPTION_PROGRAMS = { | ||
value: 'programs', | ||
label: i18n.t('Programs'), | ||
} | ||
|
||
export const OPTION_CATEGORY_COMBOS = { | ||
value: 'categoryCombos', | ||
label: i18n.t('Category combination'), | ||
} | ||
|
||
export const OPTION_DASHBOARDS = { | ||
value: 'dashboards', | ||
label: i18n.t('Dashboard'), | ||
} | ||
|
||
export const OPTION_DATA_ELEMENT_GROUPS = { | ||
value: 'dataElementGroups', | ||
label: i18n.t('Data element groups'), | ||
} | ||
|
||
export const OPTION_OPTION_SETS = { | ||
value: 'optionSets', | ||
label: i18n.t('Option sets'), | ||
} | ||
|
||
export const OBJECT_TYPE_KEY = 'objectType' | ||
export const OBJECT_TYPE_DEFAULT_VALUE = OPTION_DATA_SETS.value | ||
|
||
const objectTypeLabel = i18n.t('Object type') | ||
|
||
export const ObjectType = () => { | ||
return ( | ||
<Field> | ||
<Select | ||
dataTest="input-object-type" | ||
name={OBJECT_TYPE_KEY} | ||
label={objectTypeLabel} | ||
options={[ | ||
OPTION_DATA_SETS, | ||
OPTION_PROGRAMS, | ||
OPTION_CATEGORY_COMBOS, | ||
OPTION_DASHBOARDS, | ||
OPTION_DATA_ELEMENT_GROUPS, | ||
OPTION_OPTION_SETS, | ||
]} | ||
/> | ||
</Field> | ||
) | ||
} |