-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathComment.js
More file actions
51 lines (45 loc) · 1.15 KB
/
Comment.js
File metadata and controls
51 lines (45 loc) · 1.15 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
import React, { useState, useEffect } from "react";
import styled from "styled-components";
const Wrapper = styled.div`
margin-top: 10px;
padding: 0 15px;
background: #edf6f7;
`;
const Button = styled.button`
cursor: pointer;
font-size: 12px;
width: 5vw;
height: 20px;
border-radius: 10px;
background: #e1f7fa;
outline: none;
margin-top: 10px;
`;
function Comment(props) {
const [showEdit, setShowEdit] = useState(false);
const [editText, setEditText] = useState("");
useEffect(() => {
setEditText(props.text);
}, [props.text]);
const testFunction = e => {
props.updateComment(props.id, editText);
setEditText("");
setShowEdit(!showEdit);
e.preventDefault;
};
return (
<Wrapper>
<h3>{props.name}</h3>
{!showEdit ? (
<p>{props.text}</p>
) : (
<form onSubmit={e => testFunction(e)}>
<input value={editText} onChange={e => setEditText(e.target.value)} />
</form>
)}
<Button onClick={() => props.deleteComment(props.id)}>Delete</Button>
<Button onClick={() => setShowEdit(!showEdit)}>Edit</Button>
</Wrapper>
);
}
export default Comment;