-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTranslationModal.js
More file actions
78 lines (72 loc) · 2.43 KB
/
TranslationModal.js
File metadata and controls
78 lines (72 loc) · 2.43 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
CenteredContent,
CircularLoader,
Modal,
ModalContent,
ModalTitle,
} from '@dhis2/ui'
import PropTypes from 'prop-types'
import React, { useEffect, useState } from 'react'
import i18n from '../../../locales/index.js'
import { TranslationForm } from './TranslationForm.js'
import { TranslationModalActions } from './TranslationModalActions.js'
import { useTranslationsResults } from './useTranslationsResults.js'
export const TranslationModal = ({
objectToTranslate,
fieldsToTranslate,
onClose,
onTranslationSaved,
}) => {
const [translations, setTranslations] = useState([])
const endpointPath = new URL(objectToTranslate.href).pathname
const endpointPathMatch = endpointPath.match(/api\/\d+\/(?<resource>.+)/)
const resource = endpointPathMatch?.groups
? endpointPathMatch.groups.resource
: null
const { translationsData, fetching } = useTranslationsResults({
resource,
})
useEffect(() => {
if (translationsData) {
setTranslations(translationsData)
}
}, [translationsData])
return (
<Modal large position="middle" onClose={onClose}>
<ModalTitle>
{i18n.t('Translate: {{objectName}}', {
objectName: objectToTranslate.name || 'TEXT', // XXX
nsSeparator: '^^',
})}
</ModalTitle>
{fetching ? (
<>
<ModalContent>
<CenteredContent>
<CircularLoader />
</CenteredContent>
</ModalContent>
<TranslationModalActions
onClose={onClose}
saveButtonDisabled={true}
/>
</>
) : (
<TranslationForm
fieldsToTranslate={fieldsToTranslate}
objectToTranslate={objectToTranslate}
translations={translations}
onTranslationSaved={onTranslationSaved}
resource={resource}
onClose={onClose}
/>
)}
</Modal>
)
}
TranslationModal.propTypes = {
fieldsToTranslate: PropTypes.array.isRequired,
objectToTranslate: PropTypes.object.isRequired,
onClose: PropTypes.func.isRequired,
onTranslationSaved: PropTypes.func.isRequired,
}