-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCommentAddForm.js
More file actions
87 lines (81 loc) · 2.77 KB
/
CommentAddForm.js
File metadata and controls
87 lines (81 loc) · 2.77 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
import { useDataMutation } from '@dhis2/app-runtime'
import { Button } 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,
MessageInput,
} from '../common/index.js'
export const CommentAddForm = ({
interpretationId,
currentUser,
onSave,
focusRef,
}) => {
const [showRichTextEditor, setShowRichTextEditor] = useState(false)
const [commentText, setCommentText] = useState('')
const saveMutationRef = useRef({
resource: `interpretations/${interpretationId}/comments`,
type: 'create',
data: ({ commentText }) => commentText,
})
const [save, { loading }] = useDataMutation(saveMutationRef.current, {
onComplete: () => {
setShowRichTextEditor(false)
setCommentText('')
onSave()
},
})
const inputPlaceholder = i18n.t('Write a reply')
return (
<MessageEditorContainer currentUser={currentUser}>
{showRichTextEditor ? (
<>
<RichTextEditor
inputPlaceholder={inputPlaceholder}
onChange={setCommentText}
value={commentText}
ref={focusRef}
disabled={loading}
/>
<MessageButtonStrip>
<Button
primary
small
onClick={() => save({ commentText })}
loading={loading}
>
{i18n.t('Post reply')}
</Button>
<Button
secondary
small
disabled={loading}
onClick={() => {
setCommentText('')
setShowRichTextEditor(false)
}}
>
{i18n.t('Cancel')}
</Button>
</MessageButtonStrip>
</>
) : (
<MessageInput
onFocus={() => setShowRichTextEditor(true)}
placeholder={inputPlaceholder}
ref={focusRef}
/>
)}
</MessageEditorContainer>
)
}
CommentAddForm.propTypes = {
currentUser: PropTypes.object.isRequired,
focusRef: PropTypes.object.isRequired,
interpretationId: PropTypes.string.isRequired,
onSave: PropTypes.func,
}