Skip to content

Commit

Permalink
feat(object): add ObjectList and ObjectType components
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Jan 22, 2020
1 parent f0a3baf commit 39c0ec5
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 27 deletions.
69 changes: 42 additions & 27 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2019-12-30T04:40:35.460Z\n"
"PO-Revision-Date: 2019-12-30T04:40:35.460Z\n"
"POT-Creation-Date: 2019-12-30T09:20:29.553Z\n"
"PO-Revision-Date: 2019-12-30T09:20:29.553Z\n"

msgid "user is not logged in"
msgstr ""
Expand Down Expand Up @@ -179,6 +179,12 @@ msgstr ""
msgid "Include delete"
msgstr ""

msgid "Include children of organisation unit"
msgstr ""

msgid "Inclusion"
msgstr ""

msgid "Non Null"
msgstr ""

Expand All @@ -200,6 +206,30 @@ msgstr ""
msgid "Merge mode"
msgstr ""

msgid "Loading Object options..."
msgstr ""

msgid "Something went wrong when loading the object!"
msgstr ""

msgid "Programs"
msgstr ""

msgid "Category combination"
msgstr ""

msgid "Dashboard"
msgstr ""

msgid "Data element groups"
msgstr ""

msgid "Option sets"
msgstr ""

msgid "Object type"
msgstr ""

msgid "Organisation unit"
msgstr ""

Expand All @@ -218,7 +248,16 @@ msgstr ""
msgid "Preheat mode"
msgstr ""

msgid "Programs"
msgid "[ All program stages]"
msgstr ""

msgid "Program stages"
msgstr ""

msgid "Loading programStage options..."
msgstr ""

msgid "Something went wrong when loading the programStages!"
msgstr ""

msgid "Loading program options..."
Expand Down Expand Up @@ -329,9 +368,6 @@ msgstr ""
msgid "Category ID scheme"
msgstr ""

msgid "Object type"
msgstr ""

msgid "Data element ID scheme"
msgstr ""

Expand All @@ -353,9 +389,6 @@ msgstr ""
msgid "Include deleted"
msgstr ""

msgid "Inclusion"
msgstr ""

msgid "Inclusion Strategy"
msgstr ""

Expand Down Expand Up @@ -401,27 +434,9 @@ msgstr ""
msgid "Update"
msgstr ""

msgid "Include children of organisation unit"
msgstr ""

msgid "Gzip"
msgstr ""

msgid "Category combination"
msgstr ""

msgid "Dashboard"
msgstr ""

msgid "Data element groups"
msgstr ""

msgid "Option sets"
msgstr ""

msgid "[ All program stages]"
msgstr ""

msgid "Import/Export"
msgstr ""

Expand Down
92 changes: 92 additions & 0 deletions src/components/Inputs/ObjectList.js
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>
)
60 changes: 60 additions & 0 deletions src/components/Inputs/ObjectType.js
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>
)
}

0 comments on commit 39c0ec5

Please sign in to comment.