scrimba
Frontend Career Path
Essential JavaScript concepts
Twitter Clone
Replies 3: toggle hidden
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

AboutCommentsNotes
Replies 3: toggle hidden
Expand for more info
index.js
run
preview
console
import { tweetsData } from './data.js'
const tweetInput = document.getElementById('tweet-input')
const tweetBtn = document.getElementById('tweet-btn')

tweetBtn.addEventListener('click', function(){
console.log(tweetInput.value)
})

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)
}
})

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()
}



/*
Challenge:
1. Use the uuid stored in 'replyId' to take control
of the div containing that tweet’s replies.
(Check the HTML string below to remind yourself
what id that div will have.)
2. Toggle the CSS class "hidden" on that div.
*/

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>
</div>
</div>
</div>
<div class="hidden" id="replies-${tweet.uuid}">
${repliesHtml}
</div>
</div>
`
})
return feedHtml
}

function render(){
document.getElementById('feed').innerHTML = getFeedHtml()
}

render()

Console
/index.html
-4:49