-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
231 lines (198 loc) · 6.23 KB
/
main.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { tweetsData } from './data.js'
import { v4 as uuidv4 } from 'https://jspm.dev/uuid';
window.addEventListener('DOMContentLoaded', function() {
const storedData = localStorage.getItem('tweetsData');
if (storedData) {
tweetsData.push(...JSON.parse(storedData));
}
// Set the initial mode
const isDarkMode = localStorage.getItem('isDarkMode') === 'true';
setMode(isDarkMode);
render();
});
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()
}
})
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
saveToLocalStorage();
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
saveToLocalStorage();
render()
}
function handleReplyClick(replyId){
document.getElementById(`replies-${replyId}`).classList.toggle('hidden')
}
function handleReplyFormSubmit(tweetId) {
const replyInput = document.getElementById(`reply-input-${tweetId}`);
const replyText = replyInput.value;
if (replyText) {
const tweet = tweetsData.find((tweet) => tweet.uuid === tweetId);
tweet.replies.unshift({
handle: '@HR',
profilePic: '/profilelogo.png',
tweetText: replyText,
});
saveToLocalStorage();
render();
replyInput.value = '';
}
}
function handleDeleteClick(tweetId) {
const tweetIndex = tweetsData.findIndex(function (tweet) {
return tweet.uuid === tweetId;
});
if (tweetIndex !== -1) {
tweetsData.splice(tweetIndex, 1);
saveToLocalStorage();
render();
}
}
function handleTweetBtnClick(){
const tweetInput = document.getElementById('tweet-input')
if(tweetInput.value){
tweetsData.unshift({
handle: `@HR`,
profilePic: `/profilelogo.png`,
likes: 0,
retweets: 0,
tweetText: tweetInput.value,
replies: [],
isLiked: false,
isRetweeted: false,
uuid: uuidv4()
})
saveToLocalStorage();
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>
`
})
}
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-regular fa-trash-alt delete-button"
data-delete="${tweet.uuid}"
></i>
</span>
</div>
</div>
</div>
<div class="hidden" id="replies-${tweet.uuid}">
${repliesHtml}
<div class="reply-form">
<textarea id="reply-input-${tweet.uuid}" class="reply-input" placeholder="Reply"></textarea>
<button id="reply-btn" class="reply-submit" onclick="handleReplyFormSubmit('${tweet.uuid}')">Reply</button>
</div>
</div>
</div>
`
})
return feedHtml
}
function render(){
document.getElementById('feed').innerHTML = getFeedHtml()
const toggleSwitch = document.getElementById('toggle');
toggleSwitch.addEventListener('change', handleToggleSwitchChange);
// Apply the appropriate class based on the initial state
const isDarkMode = localStorage.getItem('isDarkMode') === 'true';
setMode(isDarkMode);
}
function setMode(isDarkMode) {
const body = document.body;
body.classList.remove('light-mode', 'dark-mode');
body.classList.add(isDarkMode ? 'dark-mode' : 'light-mode');
}
function handleToggleSwitchChange() {
const isDarkMode = this.checked;
localStorage.setItem('isDarkMode', isDarkMode);
setMode(isDarkMode);
}
function saveToLocalStorage() {
localStorage.setItem('tweetsData', JSON.stringify(tweetsData));
}
render()