Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(like-button): Fix Like-Issue #22 #23

Merged
merged 2 commits into from
Mar 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 40 additions & 28 deletions src/components/_App_PostList/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,55 @@ class PostsList extends React.Component {

toggleLike(e) {
e.preventDefault()

let element
let hasLiked = false
let newLikeCount
let currentLikeCount
let postId
let newLikeText

if (e.target.tagName === 'I') {
if (e.target.tagName.toLowerCase() === 'i' || e.target.tagName.toLowerCase() === 'span') {
element = e.target.parentNode
} else {
element = e.target
}

element.parentNode.style.pointerEvents = 'none'
// Has liked
hasLiked = element.classList.contains('liked')

// Update counter
currentLikeCount = element.querySelector('span').innerHTML.substr(0, element.querySelector('span').innerHTML.indexOf(' '))
currentLikeCount = parseInt(currentLikeCount)

if (hasLiked) {
newLikeCount = currentLikeCount - 1
hasLiked = false
} else {
newLikeCount = currentLikeCount + 1
hasLiked = true
}

if (newLikeCount === 1) {
newLikeText = '1 Like'
} else {
newLikeText = newLikeCount + ' Likes'
}

// Toggle liked-status
element.parentNode.style.pointerEvents = 'none' // Set PointerEvents to none to prevent multiple requests

let postId = element.parentNode.id
postId = postId.replace('post_id_', '')
postId = element.id.replace('post_id_', '')

element.querySelector('span').innerHTML = newLikeText

if (hasLiked) {
element.classList.add('liked')
} else {
element.classList.remove('liked')
}

// Request
var header = new Headers()
header.append('Authorization', 'Bearer ' + localStorage.getItem('token'))
header.append('Content-Type', 'application/json')
Expand All @@ -170,33 +206,9 @@ class PostsList extends React.Component {
}),
}

let likeCount = parseInt(element.textContent.substr(0, element.textContent.indexOf(' ')))

fetch(process.env.REACT_APP_API_URL + '/api/content/likePost', requestOptions)
.then((response) => response.text())
.then((result) => {
if (result === 'unliked') {
likeCount = likeCount - 1

element.parentNode.classList.remove('liked')

if (likeCount == 1) {
element.textContent = '1 Like'
} else {
element.textContent = likeCount + ' Likes'
}
} else {
likeCount = likeCount + 1

element.parentNode.classList.add('liked')

if (likeCount == 1) {
element.textContent = '1 Like'
} else {
element.textContent = likeCount + ' Likes'
}
}

element.parentNode.style.pointerEvents = 'all'
})
}
Expand Down