-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (181 loc) · 5.46 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { tweetsData as OrginalTweetsData } from './data.js'
import { v4 as uuidv4 } from 'https://jspm.dev/uuid';
let tweetsData = OrginalTweetsData
const replyPopUpEl = document.getElementById('reply-pop-up')
let replyId = ''
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.replies){
handleRepliesClick(e.target.dataset.replies)
}
else if(e.target.dataset.reply){
handleReplyClick(e.target.dataset.reply)
}
else if(e.target.dataset.trash){
handleDeleteClick(e.target.dataset.trash)
}
else if(e.target.id === 'tweet-btn'){
handleTweetBtnClick()
}
else if(e.target.id === 'reply-btn'){
handleReplyBtnClick()
}
else if(!(e.target.closest(".reply-pop-up"))){
closeReplyPopUp()
}
})
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 handleRepliesClick(replyId){
document.getElementById(`replies-${replyId}`).classList.toggle('hidden')
}
function handleReplyClick(tweetId){
replyPopUpEl.style.display = 'flex'
replyId = tweetId
}
function handleDeleteClick(tweetId){
tweetsData = tweetsData.filter(tweet => tweet.uuid !== tweetId)
render()
}
//button functions
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 handleReplyBtnClick(){
const replyInput = document.getElementById('reply-input')
const targetTweetObj=tweetsData.find(tweet=> tweet.uuid ===replyId)
if(targetTweetObj && replyInput.value){
targetTweetObj.replies.unshift({
handle:'@Scrimba',
profilePic: 'images/scrimbalogo.png',
tweetText:replyInput.value
})
render()
closeReplyPopUp()
}
}
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>
`
})
}
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-replies="${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-reply replyIcon"
data-reply ="${tweet.uuid}"
></i>
</span>
<span class="tweet-detail">
<i class="fa-solid fa-trash-can"
data-trash ="${tweet.uuid}"
></i>
</span>
</div>
</div>
</div>
<div class="hidden" id="replies-${tweet.uuid}">
${repliesHtml}
</div>
</div>
`
})
return feedHtml
}
function closeReplyPopUp(){
replyPopUpEl.style.display = 'none'
}
function render(){
document.getElementById('feed').innerHTML = getFeedHtml()
}
render()