-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
198 lines (165 loc) · 5.23 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { tweetsData } from './data.js'
import { v4 as uuidv4 } from 'https://jspm.dev/uuid';
document.addEventListener('click', function(e){
if(e.target.dataset.like){
handleLikeClick(e.target.dataset.like)
}
else if(e.target.dataset.retweet){
handleRetweetClick(e.target.dataset.retweet)
}
else if(e.target.dataset.reply){
handleReplyClick(e.target.dataset.reply)
}
else if(e.target.dataset.delete){
handleDeleteClick(e.target.dataset.delete)
}
else if(e.target.id === 'tweet-btn'){
handleTweetBtnClick()
} else if (e.target.id === 'comment-btn'){
handleCommentBtnClick(e.target.dataset.comment)
}
})
function handleDeleteClick(tweetId){
let index = tweetsData.findIndex(tweet => tweet.uuid === tweetId)
tweetsData.splice(index, 1)
render()
}
function handleLikeClick(tweetId){
const targetTweetObj = tweetsData.filter(function(tweet){
return tweet.uuid === tweetId
})[0]
if (targetTweetObj.isLiked){
targetTweetObj.likes--
}
else{
targetTweetObj.likes++
}
targetTweetObj.isLiked = !targetTweetObj.isLiked
render()
}
function handleRetweetClick(tweetId){
const targetTweetObj = tweetsData.filter(function(tweet){
return tweet.uuid === tweetId
})[0]
if(targetTweetObj.isRetweeted){
targetTweetObj.retweets--
}
else{
targetTweetObj.retweets++
}
targetTweetObj.isRetweeted = !targetTweetObj.isRetweeted
render()
}
function handleReplyClick(replyId){
document.getElementById(`replies-${replyId}`).classList.toggle('hidden')
}
function handleCommentBtnClick(tweetId){
const commentInput = document.getElementById(tweetId)
if(commentInput.value){
const isComented = tweetsData.filter(function(tweet){
return tweetId == tweet.uuid
})
isComented[0].replies.push({
handle: `Scrimba ✅`,
profilePic: `images/scrimbalogo.png`,
tweetText: commentInput.value,
})
}
render()
commentInput.value = ""
}
function handleTweetBtnClick(){
const tweetInput = document.getElementById('tweet-input')
if(tweetInput.value){
tweetsData.unshift({
handle: `@Scrimba`,
profilePic: `images/scrimbalogo.png`,
likes: 0,
retweets: 0,
tweetText: tweetInput.value,
replies: [],
isLiked: false,
isRetweeted: false,
uuid: uuidv4()
})
render()
tweetInput.value = ''
}
}
function getFeedHtml(){
let feedHtml = ``
tweetsData.forEach(function(tweet){
let likeIconClass = ''
if (tweet.isLiked){
likeIconClass = 'liked'
}
let retweetIconClass = ''
if (tweet.isRetweeted){
retweetIconClass = 'retweeted'
}
let repliesHtml = ''
if(tweet.replies.length > 0){
tweet.replies.forEach(function(reply){
repliesHtml+=`
<div class="tweet-reply">
<div class="tweet-inner">
<img src="${reply.profilePic}" class="profile-pic">
<div>
<p class="handle">${reply.handle}</p>
<p class="tweet-text">${reply.tweetText}</p>
</div>
</div>
</div>
`
})
}
repliesHtml +=
` <div class="tweet-input-area">
<img src="images/scrimbalogo.png" class="profile-pic">
<textarea placeholder="Write your comment" class="comment" id="${tweet.uuid}"></textarea>
</div>
<button data-comment="${tweet.uuid}" id="comment-btn">Comment</button>`
feedHtml += `
<div class="tweet">
<div class="tweet-inner">
<img src="${tweet.profilePic}" class="profile-pic">
<div>
<p class="handle">${tweet.handle}</p>
<p class="tweet-text">${tweet.tweetText}</p>
<div class="tweet-details">
<span class="tweet-detail">
<i class="fa-regular fa-comment-dots"
data-reply="${tweet.uuid}"
></i>
${tweet.replies.length}
</span>
<span class="tweet-detail">
<i class="fa-solid fa-heart ${likeIconClass}"
data-like="${tweet.uuid}"
></i>
${tweet.likes}
</span>
<span class="tweet-detail">
<i class="fa-solid fa-retweet ${retweetIconClass}"
data-retweet="${tweet.uuid}"
></i>
${tweet.retweets}
</span>
<span class="tweet-detail">
<i class="fa-solid fa-trash" data-delete="${tweet.uuid}"></i></i>
</span>
</div>
</div>
</div>
<div class="hidden" id="replies-${tweet.uuid}">
${repliesHtml}
</div>
</div>
`
})
return feedHtml
}
function render(){
document.getElementById('feed').innerHTML = getFeedHtml()
}
render()