-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (75 loc) · 2.84 KB
/
index.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
const posts = [
{
name: "Vincent van Gogh",
username: "vincey1853",
location: "Zundert, Netherlands",
avatar: "images/avatar-vangogh.jpg",
post: "images/post-vangogh.jpg",
comment: "just took a few mushrooms lol",
likes: 21,
},
{
name: "Gustave Courbet",
username: "gus1819",
location: "Ornans, France",
avatar: "images/avatar-courbet.jpg",
post: "images/post-courbet.jpg",
comment: "i'm feelin a bit stressed tbh",
likes: 4,
},
{
name: "Joseph Ducreux",
username: "jd1735",
location: "Paris, France",
avatar: "images/avatar-ducreux.jpg",
post: "images/post-ducreux.jpg",
comment:
"gm friends! which coin are YOU stacking up today?? post below and WAGMI!",
likes: 152,
},
];
const likesArray = [];
const postSection = document.querySelector("main");
// loop through and render all the posts
if (postSection) {
posts.forEach((post, index) => {
const postContainer = document.createElement("div");
postContainer.classList.add("container", "post-container");
postContainer.innerHTML = `
<div class="post-info-container">
<img class="user-avatar" src="${post.avatar}" alt="Avatar" />
<div class="post-info">
<p class="bold-text">${post.name}</p>
<p>${post.location}</p>
</div>
</div>
<img class="post-image" src="${post.post}" alt="Post" />
<div class="icon-container">
<img class = "heart-icon"src="images/icon-heart.png" alt="Heart Icon" />
<img src="images/icon-comment.png" alt="Comment Icon" />
<img src="images/icon-dm.png" alt="DM Icon" />
</div>
<div class="content-container">
<p class=" like-number bold-text">${post.likes} likes</p>
<p><span class="bold-text">${post.username}</span> ${post.comment}</p>
</div>
`;
postSection.appendChild(postContainer);
// number of likes for each post
likesArray[index] = post.likes;
// adding click function
const heartIcon = postContainer.querySelector(".heart-icon");
const postImage = postContainer.querySelector(".post-image");
const likeNumber = postContainer.querySelector(".like-number");
heartIcon?.addEventListener("click", () => {
likesArray[index] += 1;
likeNumber.textContent = `${likesArray[index]} likes`;
});
postImage?.addEventListener("click", () => {
likesArray[index] += 1;
const likeNumber = postContainer.querySelector(".like-number");
likeNumber.textContent = `${likesArray[index]} likes`;
console.log(post.likes);
});
});
}