scrimba
Frontend Career Path
Essential JavaScript concepts
Twitter Clone
Color the icons
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

To see this lesson you need to be logged in. Joining Scrimba is free and gives you access to 20+ courses.Sign up
AboutCommentsNotes
Color the icons
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()
}

function getFeedHtml(){
let feedHtml = ``

tweetsData.forEach(function(tweet){
/*
Challenge:
1. Use an if statement to set the value of
'likeIconClass' to the string 'liked'
if the tweet has been liked.
2. In the like icon tag, add 'likeIconClass'
to the list of classes.
*/
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"
data-like="${tweet.uuid}"
></i>
${tweet.likes}
</span>
<span class="tweet-detail">
<i class="fa-solid fa-retweet"
data-retweet="${tweet.uuid}"
></i>
${tweet.retweets}
</span>
</div>
</div>
</div>
</div>
`
})
return feedHtml
}

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

render()

Console
/index.html
-6:01