-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (140 loc) · 4.68 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
// path: XClone/data.js
import { tweetsData } from "./data.js";
import { v4 as uuidv4 } from 'https://jspm.dev/uuid';
// tweetInput.addEventListener("keydown", function(event) {
// const tweetText = tweetInput.value;
// if(event.key === "Enter" && !event.shiftKey) {
// event.preventDefault();
// tweetBtn.click();
// console.log(tweetText);
// }
// })
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.id === "tweet-btn") {
handleTweetBtnClick();
}
})
function handleLikeClick(tweetId) {
const targetTweetObj = tweetsData.filter(function(tweet) {
return tweet.uuid === tweetId;
})[0]
if(targetTweetObj.isLiked === false) {
targetTweetObj.likes += 1;
}
else {
targetTweetObj.likes -= 1;
}
targetTweetObj.isLiked = !targetTweetObj.isLiked;
render();
}
function handleRetweetClick(tweetId) {
const targetTweetObj = tweetsData.filter(function(tweet) {
return tweet.uuid === tweetId;
})[0]
if(targetTweetObj.isRetweeted === false) {
targetTweetObj.retweets += 1;
}
else {
targetTweetObj.retweets -= 1;
}
targetTweetObj.isRetweeted = !targetTweetObj.isRetweeted;
render();
}
function handleReplyClick(replyId) {
const replyBtn = document.querySelector(`#replies-${replyId}`);
replyBtn.classList.toggle("hidden");
}
function handleTweetBtnClick() {
const tweetInput = document.querySelector("#tweet-input");
if(tweetInput.value) {
tweetsData.unshift({
handle: `@aksnaresh`,
profilePic: `images/img.jpg`,
likes: 0,
retweets: 0,
tweetText: `${tweetInput.value}`,
replies: [],
isLiked: false,
isRetweeted: false,
uuid: uuidv4()
})
render();
tweetInput.value = ""
}
}
function tweetText() {
let feedHhtml = ``;
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>
`
})
}
feedHhtml +=
`
<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>
</div>
</div>
</div>
<div class="hidden" id="replies-${tweet.uuid}">
${repliesHTMl}
</div>
</div>
`
})
return feedHhtml;
}
function render() {
document.querySelector("#feed").innerHTML = tweetText();
}
render();