-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathInterpretationForm.js
More file actions
100 lines (93 loc) · 3.36 KB
/
InterpretationForm.js
File metadata and controls
100 lines (93 loc) · 3.36 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { useDataMutation } from '@dhis2/app-runtime'
import { Button, Input } from '@dhis2/ui'
import PropTypes from 'prop-types'
import React, { useRef, useState } from 'react'
import i18n from '../../../locales/index.js'
import { RichTextEditor } from '../../RichText/index.js'
import { MessageEditorContainer, MessageButtonStrip } from '../common/index.js'
export const InterpretationForm = ({
type,
id,
currentUser,
disabled,
showNoTimeDimensionHelpText,
onSave,
}) => {
const [showRichTextEditor, setShowRichTextEditor] = useState(false)
const [interpretationText, setInterpretationText] = useState('')
const saveMutationRef = useRef({
resource: `interpretations/${type}/${id}`,
type: 'create',
data: ({ interpretationText }) => interpretationText,
})
const [save, { loading: saveMutationInProgress }] = useDataMutation(
saveMutationRef.current,
{
onComplete: () => {
setShowRichTextEditor(false)
setInterpretationText('')
onSave()
},
}
)
const inputPlaceholder = i18n.t('Write an interpretation')
return (
<MessageEditorContainer
currentUser={currentUser}
dataTest="interpretation-form"
>
{showRichTextEditor ? (
<>
<RichTextEditor
disabled={saveMutationInProgress}
inputPlaceholder={inputPlaceholder}
onChange={setInterpretationText}
value={interpretationText}
helpText={
showNoTimeDimensionHelpText
? i18n.t(
'Other people viewing this interpretation in the future may see more data.'
)
: undefined
}
/>
<MessageButtonStrip>
<Button
primary
small
loading={saveMutationInProgress}
onClick={() => save({ interpretationText })}
>
{i18n.t('Post interpretation')}
</Button>
<Button
secondary
small
disabled={saveMutationInProgress}
onClick={() => {
setInterpretationText('')
setShowRichTextEditor(false)
}}
>
{i18n.t('Cancel')}
</Button>
</MessageButtonStrip>
</>
) : (
<Input
onFocus={() => setShowRichTextEditor(true)}
placeholder={inputPlaceholder}
disabled={disabled}
/>
)}
</MessageEditorContainer>
)
}
InterpretationForm.propTypes = {
currentUser: PropTypes.object,
disabled: PropTypes.bool,
id: PropTypes.string,
showNoTimeDimensionHelpText: PropTypes.bool,
type: PropTypes.string,
onSave: PropTypes.func,
}