-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (86 loc) · 3.53 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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 mainEl = document.querySelector("main");
function renderEachPost(postObject,id) {
const post = `
<section class="post">
<section class="header-post">
<img src="${postObject.avatar}" class="portrait-avatar">
<section class="post-info">
<h4>${postObject.name}</h4>
<p>${postObject.location}</p>
</section>
</section>
<section>
<img src="${postObject.post}" class="post-image">
</section>
<section class="footer-post">
<section class="icons">
<img src="images/icon-heart.png" class="icon heart-icon" onclick="likeThePost(${id})">
<img src="images/icon-comment.png" class="icon comment-icon">
<img src="images/icon-dm.png" class="icon share-icon">
</section>
<h4>${postObject.likes} likes</h4>
<p>${postObject.username}<span class="user-text"> ${postObject.comment}</span></p>
</section>
</section>
`
mainEl.innerHTML += post;
}
// renderEachPost(posts[0],1)
function renderPosts(allPosts) {
// for (i = 0; i < allPosts.length; i++) {
// renderEachPost(allPosts[i],i);
let count = 0;
allPosts.forEach(post => {
renderEachPost(post, count);
count ++;
})
}
function likeThePost(index) {
// Create a likeEl that points to the likes count
const likeEl = document.querySelectorAll(".footer-post h4")[index];
// Create a likeIconEl that points to the like icon
const likeIconEl = document.querySelectorAll(".heart-icon")[index];
// Grab and convert the number of likes
const previousLikes = Number(likeEl.textContent.split(" ")[0]);
// Check if likes exists in class
// At initial run, it will always be not true
if (likeIconEl.classList.contains("liked")) {
likeIconEl.classList.remove("liked"); // Removes it if it exists
likeIconEl.src = "images/icon-heart.png"; // Reverts back to normal heart icon
likeEl.textContent = `${previousLikes - 1} likes`; // Reduce the number of likes
} else {
likeIconEl.classList.add("liked"); // Add it if it exists
likeIconEl.src = "images/icon-heart-hover.png"; // Changed to hover heart icon
likeEl.textContent = `${previousLikes + 1} likes`; // Add to the number of likes
}
}
renderPosts(posts);