-
Notifications
You must be signed in to change notification settings - Fork 0
/
Post.js
114 lines (90 loc) · 3.43 KB
/
Post.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React,{ useState, useEffect } from 'react'
import Avatar from "@material-ui/core/Avatar"
import "./Post.css"
import { db, auth } from './firebase';
import firebase from 'firebase';
import {
FaSistrix,
FaTelegramPlane,
FaRegCompass,
FaRegHeart,
} from "react-icons/fa";
import { MdHome } from "react-icons/md";
function Post({ username, postId,user,imageUrl, caption}) {
const [comments, setComments] = useState([]);
const [comment, setComment] = useState([]);
useEffect(() => {
let unsubscribe;
if (postId) {
unsubscribe = db
.collection("posts")
.doc(postId)
.collection("comments")
.orderBy('timestamp', 'asc')
.onSnapshot((snapshot) => {
setComments(snapshot.docs.map((doc) => doc.data()));
})
}
return () => {
unsubscribe();
};
}, [postId]);
const postComment = (event) => {
event.preventDefault();
db.collection("posts").doc(postId).collection("comments").add({
text: comment,
username: user.displayName,
timestamp: firebase.firestore.FieldValue.serverTimestamp()
});
setComment('');
}
return (
<div className="post">
<div className="post_header">
{/* header --avatar +username*/}
<Avatar
className="post-avatar"
alt='Sajid'
src="{username} "
/>
<h3>{username}</h3>
</div>
{/*image*/}
<img className="post_image" src={imageUrl} alt="" />
<span><FaRegHeart className="Image__icons" /></span>
<span><FaRegCompass className="Image__icons" /></span>
<span><FaTelegramPlane className="Image__icons"/></span>
<h3 className="post_text"><strong>{username} </strong>{caption}</h3>
{/*username +caption*/}
<div className="post__comments">
{comments.map((comment) => (
<p>
<strong >
{comment.username}:
</strong> {comment.text}
</p>
))}
</div>
{user && ( // Only display this comment form input if the user has logged in
<form className="post_commentBox">
<input
className="post_input"
type="text"
placeholder="Add a comment"
value={comment}
onChange={(e) => setComment(e.target.value)}>
</input>
<button
className="post_button"
disable={!comment}
type="submit"
onClick={postComment}
>
Comment
</button>
</form>
)}
</div>
)
}
export default Post