-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLocalesSelect.js
More file actions
50 lines (45 loc) · 1.51 KB
/
LocalesSelect.js
File metadata and controls
50 lines (45 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { useDataQuery } from '@dhis2/app-runtime'
import { SingleSelect, SingleSelectOption } from '@dhis2/ui'
import PropTypes from 'prop-types'
import React from 'react'
import i18n from '../../../locales/index.js'
const query = {
locales: {
resource: 'locales/db',
},
}
export const LocalesSelect = ({ onChange, selected }) => {
const { data, fetching } = useDataQuery(query)
return (
<SingleSelect
prefix={
selected ? i18n.t('Translating to') : i18n.t('Choose a locale')
}
onChange={({ selected }) => onChange(selected)}
loading={fetching}
selected={data && selected ? selected : ''}
dense
>
{data &&
data.locales
// XXX remove duplicates ?! fr_SN - French (Senegal)
.reduce((locales, { locale, name }) => {
if (!locales.find((entry) => entry.locale === locale)) {
locales.push({ locale, name })
}
return locales
}, [])
.map(({ locale, name }) => (
<SingleSelectOption
key={locale}
value={locale}
label={name}
/>
))}
</SingleSelect>
)
}
LocalesSelect.propTypes = {
onChange: PropTypes.func.isRequired,
selected: PropTypes.string,
}