Fixed incorrect displaying votes

This commit is contained in:
sairaj mote 2022-01-18 16:30:07 +05:30
parent d7ffee6a36
commit 932bf3e6ae

View File

@ -825,6 +825,8 @@
getRef('news_categories_list').append(frag) getRef('news_categories_list').append(frag)
} }
const openedArticles = {}
async function renderArticle(articleID, firstLoad = true) { async function renderArticle(articleID, firstLoad = true) {
const allArticles = await compactIDB.readData('appObjects', 'articlesContent') const allArticles = await compactIDB.readData('appObjects', 'articlesContent')
const { title, published, readTime, contributors } = floGlobals.appObjects.articles[articleID] const { title, published, readTime, contributors } = floGlobals.appObjects.articles[articleID]
@ -841,29 +843,32 @@
}) })
getRef('article_contributors').innerHTML = '' getRef('article_contributors').innerHTML = ''
getRef('article_contributors').append(frag) getRef('article_contributors').append(frag)
totalVotes = floGlobals.appObjects.articles[articleID].votes ? floGlobals.appObjects.articles[articleID].votes : 0 if (!openedArticles.hasOwnProperty(articleID)) {
floCloudAPI.requestGeneralData(`article_${articleID}_votes`, { floCloudAPI.requestGeneralData(`article_${articleID}_votes`, {
lowerVectorClock: floGlobals.appObjects.articles[articleID].lastCountedVC, lowerVectorClock: floGlobals.appObjects.articles[articleID].lastCountedVC,
callback: (allVotes, e) => { callback: (allVotes, e) => {
if (firstLoad) { if (firstLoad) {
let first = true let first = true
for (const vote in allVotes) { for (const vote in allVotes) {
if (first) { if (first) {
first = false first = false
continue continue
}
floGlobals.appObjects.articles[articleID].votes += allVotes[vote].message.voteCount || 1
}
getRef('like_count').textContent = floGlobals.appObjects.articles[articleID].votes
} else {
for (const msg in allVotes) {
animateLikeCount(allVotes[msg].message.voteCount, articleID)
} }
totalVotes += allVotes[vote].message.voteCount || 1
}
getRef('like_count').textContent = getRelativeCount(totalVotes)
} else {
for (const msg in allVotes) {
if (typeof myFloID === 'undefined' || allVotes[msg].senderID !== myFloID)
animateLikeCount(allVotes[msg].message.voteCount)
} }
firstLoad = false
} }
firstLoad = false })
} openedArticles[articleID] = true
}) } else {
getRef('like_count').textContent = floGlobals.appObjects.articles[articleID].votes
}
} }
function renderExplorePage(params) { function renderExplorePage(params) {
@ -1066,7 +1071,6 @@
let tempVoteCount = 0 let tempVoteCount = 0
getRef('upvote_button').addEventListener('mouseup', function () { getRef('upvote_button').addEventListener('mouseup', function () {
if (myFloID) { if (myFloID) {
animateLikeCount(1)
tempVoteCount++; tempVoteCount++;
const animOptions = { const animOptions = {
fill: 'forwards', fill: 'forwards',
@ -1090,24 +1094,25 @@
], animOptions).onfinish = e => e.target.cancel() ], animOptions).onfinish = e => e.target.cancel()
} }
}) })
function animateLikeCount(voteCount = 1) { function animateLikeCount(voteCount = 1, articleID) {
console.log('called')
const animOptions = { const animOptions = {
fill: 'forwards', fill: 'forwards',
duration: 150, duration: 150,
ease: 'easing', ease: 'easing',
} }
totalVotes += voteCount floGlobals.appObjects.articles[articleID].votes += voteCount
getRef('like_count').animate(slideOutUp, animOptions) getRef('like_count').animate(slideOutUp, animOptions)
.onfinish = (e) => { .onfinish = (e) => {
e.target.cancel() e.target.cancel()
} }
const tempCount = document.createElement('div') const tempCount = document.createElement('div')
tempCount.classList.add('temp-count') tempCount.classList.add('temp-count')
tempCount.textContent = getRelativeCount(totalVotes) tempCount.textContent = floGlobals.appObjects.articles[articleID].votes
getRef('like_count').after(tempCount) getRef('like_count').after(tempCount)
tempCount.animate(slideInUp, animOptions) tempCount.animate(slideInUp, animOptions)
.onfinish = () => { .onfinish = () => {
getRef('like_count').textContent = getRelativeCount(totalVotes) getRef('like_count').textContent = floGlobals.appObjects.articles[articleID].votes
tempCount.remove() tempCount.remove()
} }
} }
@ -1125,14 +1130,6 @@
showPopup('sign_in_popup') showPopup('sign_in_popup')
} }
}, 300)) }, 300))
function getRelativeCount(count) {
if (count < 1000)
return count
else if (count < 1000000)
return parseFloat((count / 1000).toFixed(1)) + 'K'
else if (count < 1000000000)
return parseFloat((count / 1000000).toFixed(1)) + 'M'
}
function getSignedIn() { function getSignedIn() {