-
-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathCommentBlock.tsx
More file actions
86 lines (81 loc) · 2 KB
/
CommentBlock.tsx
File metadata and controls
86 lines (81 loc) · 2 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
import * as React from 'react';
import { CommentInfo } from '../../lib';
interface Props {
updateComment: (commentInfo: CommentInfo, text: string) => void;
removeComment: (lineId: string) => void;
comment: any;
show: boolean;
}
const CommentBlock: React.FC<Props> = ({
updateComment,
removeComment,
comment,
show
}) => {
const [isComment, setIsComment] = React.useState<boolean>(show);
const [text, setText] = React.useState<string>(
comment.body ? comment.body.text : ''
);
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setText(e.target.value);
};
if (!isComment) {
return (
<div className='p-2'>
<div className='form-group mb-2'>
<textarea
onChange={handleChange}
value={text}
className='form-control'
/>
</div>
<button
className='btn btn-primary mr-2'
onClick={() => {
if (!text) {
return removeComment(comment.lineId);
}
updateComment(comment, text);
setIsComment(true);
}}
>
Submit
</button>
<button
className='btn btn-secondary'
onClick={() => {
if (!text) {
return removeComment(comment.lineId);
}
setIsComment(true);
}}
>
Cancel
</button>
</div>
);
}
return (
<div className='p-2'>
<div className='mb-2 bg-light rounded p-2'>
{comment.body.text &&
comment.body.text
.split('\n')
.map((str: string, i: number) => <div key={i}>{str}</div>)}
</div>
<button
onClick={() => setIsComment(false)}
className='btn btn-primary mr-2'
>
Edit
</button>
<button
onClick={() => removeComment(comment.lineId)}
className='btn btn-secondary mr-2'
>
Delete
</button>
</div>
);
};
export default CommentBlock;