-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCommentUpdateForm.js
More file actions
76 lines (74 loc) · 2.53 KB
/
CommentUpdateForm.js
File metadata and controls
76 lines (74 loc) · 2.53 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
import { useDataMutation } from '@dhis2/app-runtime'
import { Button, spacers, colors } from '@dhis2/ui'
import PropTypes from 'prop-types'
import React, { useState, useRef } from 'react'
import i18n from '../../../locales/index.js'
import { RichTextEditor } from '../../RichText/index.js'
import { MessageEditorContainer, MessageButtonStrip } from '../common/index.js'
export const CommentUpdateForm = ({
interpretationId,
commentId,
currentUser,
text,
close,
onComplete,
}) => {
const [commentText, setCommentText] = useState(text || '')
const updateMutationRef = useRef({
resource: `interpretations/${interpretationId}/comments/${commentId}`,
type: 'update',
partial: false,
data: ({ commentText }) => commentText,
})
const [update, { loading, error }] = useDataMutation(
updateMutationRef.current,
{
onComplete: () => {
onComplete()
close()
},
}
)
const errorText = error ? i18n.t('Could not update comment') : ''
return (
<div className="message">
<MessageEditorContainer currentUser={currentUser}>
<RichTextEditor
inputPlaceholder={i18n.t('Enter comment text')}
onChange={setCommentText}
value={commentText}
disabled={loading}
errorText={errorText}
/>
<MessageButtonStrip>
<Button
loading={loading}
primary
small
onClick={() => update({ commentText })}
>
{i18n.t('Update')}
</Button>
<Button disabled={loading} secondary small onClick={close}>
{i18n.t('Cancel')}
</Button>
</MessageButtonStrip>
</MessageEditorContainer>
<style jsx>{`
.message {
padding: 0 ${spacers.dp8} ${spacers.dp8};
background-color: ${colors.grey100};
border-radius: 5px;
}
`}</style>
</div>
)
}
CommentUpdateForm.propTypes = {
close: PropTypes.func.isRequired,
commentId: PropTypes.string.isRequired,
currentUser: PropTypes.object.isRequired,
interpretationId: PropTypes.string.isRequired,
onComplete: PropTypes.func.isRequired,
text: PropTypes.string,
}