-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPostListItem.js
More file actions
87 lines (78 loc) · 2.38 KB
/
PostListItem.js
File metadata and controls
87 lines (78 loc) · 2.38 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 React, { useState } from "react";
import PropTypes from "prop-types";
import { Link } from "react-router";
import { FormattedMessage } from "react-intl";
import Input from "./Input";
import CommentList from "./CommentList";
import update from "react-addons-update";
// Import Style
import styles from "./PostListItem.css";
function PostListItem(props) {
const [comment, setComment] = useState([]);
const [show, setShow] = useState(false);
const getComment = (a, b) => {
const id = Math.round(Math.random() * 10000);
const obj = { name: a, text: b, id };
setComment([...comment, obj]);
setShow(!show);
};
const updateComment = (id, text) => {
const index = comment.findIndex(value => value.id === id);
const clone = comment;
const edit = update(clone, {
[index]: {
text: { $set: text }
}
});
setComment(edit);
};
const deleteComment = id => {
const newList = comment.filter(val => val.id !== id);
setComment(newList);
};
return (
<div className={styles["single-post"]}>
<h3 className={styles["post-title"]}>
<Link to={`/posts/${props.post.slug}-${props.post.cuid}`}>
{props.post.title}
</Link>
</h3>
<p className={styles["author-name"]}>
<FormattedMessage id="by" /> {props.post.name}
</p>
<p className={styles["post-desc"]}>{props.post.content}</p>
<div className={styles["wrapper-div"]}>
<p className={styles["post-action"]}>
<a href="#" onClick={props.onDelete}>
<FormattedMessage id="deletePost" />
</a>
</p>
<p className={styles["post-action"]}>
<a href="#" onClick={() => setShow(!show)}>
<FormattedMessage id="Add Comment" />
</a>
</p>
</div>
{show && <Input getComment={getComment} />}
{!show && (
<CommentList
updateComment={updateComment}
deleteComment={deleteComment}
arrComment={comment}
/>
)}
<hr className={styles.divider} />
</div>
);
}
PostListItem.propTypes = {
post: PropTypes.shape({
name: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
content: PropTypes.string.isRequired,
slug: PropTypes.string.isRequired,
cuid: PropTypes.string.isRequired
}).isRequired,
onDelete: PropTypes.func.isRequired
};
export default PostListItem;